writeGeneratedConverter static method

void writeGeneratedConverter(
  1. ClassElement element,
  2. StructurizeResult structurized,
  3. String constructorName,
  4. SubjectCodeContext codeContext,
)

Implementation

static void writeGeneratedConverter(
    ClassElement element,
    StructurizeResult structurized,
    String constructorName,
    SubjectCodeContext codeContext) {
  var emitter = DartEmitter();
  var converterName = "${element.name}Converter";
  var clazz = Class((builder) {
    builder.name = converterName;

    bool hasGenericTypeVariables = element.thisType.typeArguments.isNotEmpty;
    if (hasGenericTypeVariables) {
      log.severe("""
Generic type variables for models are not supported.
If you wish to use class-level generics, please implement a TreeBaseConverterFactory for your base type.
      """.trim());
    }

    var referencedClassName = codeContext.className(element);
    builder.extend = Reference(
        "$genAlias.DefaultStructureConverter<${codeContext.className(element)}>");

    builder.constructors.add(Constructor((constr) => constr
      ..initializers.add(Code(
          "super(struct: const ${structurized.structure.code(structurized.fieldNames.map((e) => "_\$$e").toList())})"))));

    _defaultProxyMethods(structurized, builder, codeContext, element);

    if (structurized.structure.conformity == StructureConformity.dataclass) {
      _dataclassProxyMethods(builder, codeContext, element, structurized);
    }
  });
  codeContext.codeBuffer.writeln(clazz.accept(emitter));
}