Field.fromElement constructor

Field.fromElement(
  1. FieldElement element
)

gets the field from the FieldElement

Implementation

factory Field.fromElement(FieldElement element) {
  String? checkForKey(Iterable<ElementAnnotation> annotations) {
    if (annotations.isEmpty) {
      return null;
    }

    String? getName(ElementAnnotation element, String field) {
      final reader = ConstantReader(element.computeConstantValue());

      final result = reader.peek(field)?.literalValue as String?;

      return result;
    }

    for (final annotation in annotations) {
      if (annotation.astName == 'JsonKey') {
        return getName(annotation, 'name');
      } else if (annotation.astName == '$Field') {
        return getName(annotation, 'key');
      }
    }
  }

  return Field(
    name: element.name,
    key: checkForKey(element.metadata),
  );
}