writeGeneratedExtension static method

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

Implementation

static void writeGeneratedExtension(
    ClassElement element,
    StructurizeResult structurized,
    String constructorName,
    SubjectCodeContext codeContext) {
  var emitter = DartEmitter();
  var extensionName = "${element.name}DogsExtension";
  var builderName = "${element.name}Builder";
  var clazz = Extension((builder) {
    builder.name = extensionName;
    var structureType =
        Reference(codeContext.cachedCounter.get(element.thisType));
    builder.on = structureType;

    builder.methods.add(Method((builder) => builder
      ..name = "rebuild"
      ..returns = structureType
      ..requiredParameters.add(Parameter((builder) => builder
        ..name = "f"
        ..type = Reference("Function($builderName b)")))
      ..body = Code("""
        var builder = $builderName(this);
        f(builder);
        return builder.build();
        """)));

    builder.methods.add(Method((builder) => builder
      ..name = "toBuilder"
      ..returns = Reference(builderName)
      ..body = Code("""
        return $builderName(this);
        """)));

    builder.methods.add(Method((builder) => builder
      ..name = "toNative"
      ..returns = Reference("Map<String,dynamic>")
      ..body = Code("""
        return $genAlias.dogs.convertObjectToNative(this, ${codeContext.cachedCounter.get(element.thisType)});
        """)));
  });
  codeContext.codeBuffer.writeln(clazz.accept(emitter));
}