ocSource static method

String ocSource(
  1. Model model,
  2. UniAPIOptions options
)

Implementation

static String ocSource(Model model, UniAPIOptions options) {
  return CodeTemplate(children: [
    CommentUniAPI(),
    EmptyLine(),
    OCImport(
        fullImportName: '${model.inputFile.pascalName}.h',
        importType: ocImportTypeLocal),
    EmptyLine(),
    OCClassDeclaration(
        className: model.name, hasExtension: true, isInterface: true),
    EmptyLine(),
    ...OCCollectionCloneFunction(fields: model.fields)
        .genCollectionFunction(),
    OCClassImplementation(
        className: model.name,
        injectOCCodeUnit: (depth) {
          final ret = <CodeUnit>[];
          ret.add(OCPredefinedFuncModelList());
          ret.add(EmptyLine());
          ret.add(OCPredefinedFuncDictList());
          ret.add(EmptyLine());
          ret.add(OCPredefinedFuncWrapNil());
          ret.add(EmptyLine());
          ret.add(OCFunction(
              functionName: 'fromMap',
              isClassMethod: true,
              returnType: AstCustomType(model.name),
              params: [Variable(AstMap(keyType: AstString()), 'dict')],
              body: (depth) {
                final ret = <CodeUnit>[
                  OneLine(
                      depth: depth + 1,
                      body:
                          'if ((NSNull *)dict == [NSNull null]) return nil;'),
                  OneLine(
                      depth: depth + 1,
                      body:
                          '${OCReference(AstCustomType(model.name)).build()} result = [[${AstCustomType(model.name).ocType()} alloc] init];'),
                  ...model.fields.map((field) {
                    return OneLine(
                        depth: depth + 1,
                        body:
                            'result.${field.name} = [self wrapNil:${field.type.realType().convertOcJson2Obj(vname: 'dict[@"${field.name}"]')}];');
                  }),
                  OneLine(depth: depth + 1, body: 'return result;'),
                ];
                return ret;
              }));
          ret.add(EmptyLine());
          ret.add(OCFunction(
              functionName: 'toMap',
              isInstanceMethod: true,
              returnType: AstMap(keyType: AstString()),
              body: (depth) {
                final ret = <CodeUnit>[];
                ret.add(OneLine(
                    depth: depth + 1,
                    body:
                        'return [NSDictionary dictionaryWithObjectsAndKeys:'));

                ret.addAll(model.fields.map((field) => OneLine(
                    depth: depth + 2,
                    body:
                        '(self.${field.name} ? ${field.type.convertOcObj2Json('self.${field.name}')} : [NSNull null]), @"${field.name}",')));
                ret.add(OneLine(depth: depth + 2, body: 'nil];'));
                return ret;
              }));
          return ret;
        })
  ]).build();
}