dartCode static method

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

Implementation

static String dartCode(Model model, UniAPIOptions options) {
  return CodeTemplate(children: [
    CommentUniAPI(),
    EmptyLine(),
    DartCustomNestedImports(model.inputFile, options, fields: model.fields),
    EmptyLine(),
    if (model.codeComments.isNotEmpty)
      Comment(
          comments: [...model.codeComments],
          commentType: CommentType.commentTripleBackSlash),
    DartClass(
        className: model.name,
        fields: model.fields,
        injectedJavaCodes: (depth) {
          final ret = <CodeUnit>[];

          ret.add(DartFunction(
              depth: depth + 1,
              functionName: 'encode',
              returnType: const AstObject(),
              body: (depth) {
                final funcEncode = <CodeUnit>[];
                funcEncode.add(OneLine(
                    depth: depth + 1,
                    body:
                        'final Map<String, dynamic> ret = <String, dynamic>{};'));
                for (final field in model.fields) {
                  funcEncode.add(OneLine(
                      depth: depth + 1,
                      body: "ret['${field.name}'] = ",
                      hasNewline: false));

                  funcEncode.add(OneLine(body: '${unpackField(field)};'));
                }
                funcEncode
                    .add(OneLine(depth: depth + 1, body: 'return ret;'));

                return funcEncode;
              }));

          ret.add(EmptyLine());

          ret.add(DartFunction(
              depth: depth + 1,
              functionName: 'decode',
              isStatic: true,
              returnType: convertAstType(model.name)!,
              params: [Variable(const AstObject(), 'message')],
              body: (depth) {
                final funcDecode = <CodeUnit>[];
                if (!kEnableNullSafety) {
                  funcDecode.add(OneLine(
                      depth: depth + 1,
                      body: 'if (message == null) return null;'));
                }
                funcDecode.add(OneLine(
                    depth: depth + 1,
                    body:
                        'final Map<dynamic, dynamic> rawMap = message as Map<dynamic, dynamic>;'));
                funcDecode.add(OneLine(
                    depth: depth + 1,
                    body:
                        'final Map<String, dynamic> map = Map.from(rawMap);'));
                funcDecode.add(OneLine(
                    depth: depth + 1, body: 'return ${model.name}()'));
                for (var index = 0; index < model.fields.length; index++) {
                  final field = model.fields[index];
                  funcDecode.add(OneLine(
                      depth: depth + 2,
                      body: '..${field.name} = ',
                      hasNewline: false));

                  funcDecode
                      .add(OneLine(body: "map['${field.name}'] == null"));
                  final nullValueJudgingConditions =
                      field.type.realType() is AstCustomType &&
                          (kEnableNullSafety == false ||
                              field.type.realType().maybeNull == true);
                  funcDecode.add(OneLine(
                      depth: depth + 3,
                      body:
                          '? ${nullValueJudgingConditions == true ? null : field.type.realType().dartDefault()}'));
                  funcDecode.add(OneLine(
                      depth: depth + 3,
                      body:
                          ': ${field.type.realType().convertDartJson2Obj(vname: "map['${field.name}']")}'));
                }
                funcDecode.add(OneLine(depth: depth + 2, body: ';'));
                return funcDecode;
              }));

          return ret;
        })
  ]).build();
}