structurizeBuilt function

Future<IRStructure> structurizeBuilt(
  1. SubjectCodeContext codeContext,
  2. SubjectGenContext<Element> context,
  3. ClassElement element
)

Implementation

Future<IRStructure> structurizeBuilt(SubjectCodeContext codeContext, SubjectGenContext context, ClassElement element) async {
  var counter = codeContext.cachedCounter;
  var builtInterfaceImpl = element.thisType.asInstanceOf(builtInterface)!;
  var builderElement = builtInterfaceImpl.typeArguments[1].element! as ClassElement;

  var getters = builderElement.accessors.where((element) => element.isGetter && element.isPublic && !element.isStatic).toList();

  var fields = <IRStructureField>[];
  for (var builderGetter in getters) {
    var fieldGetter = element.getGetter(builderGetter.name)!;
    var fieldName = fieldGetter.name;
    var fieldType = fieldGetter.returnType;
    var serialType = await getSerialType(fieldType, context);
    var iterableType = await getIterableType(fieldType, context);

    var optional = fieldType.nullabilitySuffix == NullabilitySuffix.question;
    if (fieldType is DynamicType) optional = true;

    var builtValueFieldAnnotation = fieldGetter.metadata.whereTypeChecker(builtValueFieldChecker).firstOrNull?.computeConstantValue();

    var propertyName = fieldName;
    if (builtValueFieldAnnotation != null) {
      var reader = ConstantReader(builtValueFieldAnnotation);
      var wireName = reader.read("wireName");
      if (!wireName.isNull) propertyName = wireName.stringValue;
    }

    fields.add(IRStructureField(
        fieldName,
        counter.get(fieldType),
        getTypeTree(fieldType).code(counter),
        "null",
        counter.get(serialType),
        iterableType,
        propertyName,
        optional,
        !isDogPrimitiveType(serialType),
        getRetainedAnnotationSourceArray(fieldGetter, counter),
        lyell.mapChecker.isAssignableFromType(fieldType)
    ));
  }

  return IRStructure(
    codeContext.className(element),
    StructureConformity.basic,
    element.name, fields, getRetainedAnnotationSourceArray(element, counter),
  );
}