getField method

Field getField(
  1. String name,
  2. YamlMap property, {
  3. required bool disallowNullForDefaults,
})

Implementation

Field getField(String name, YamlMap property,
    {required bool disallowNullForDefaults}) {
  try {
    final required =
        property.containsKey('required') && property['required'] == true;
    final ignored =
        property.containsKey('ignore') && property['ignore'] == true;
    final includeFromJson = !property.containsKey('includeFromJson') ||
        property['includeFromJson'] == true;
    final includeToJson = !property.containsKey('includeToJson') ||
        property['includeToJson'] == true;
    final nonFinal = ignored ||
        property.containsKey('non_final') && property['non_final'] == true;
    final includeIfNull = property.containsKey('include_if_null') &&
        property['include_if_null'] == true;
    final unknownEnumValue = property['unknown_enum_value'];
    final jsonKey = property['jsonKey'] ?? property['jsonkey'];
    final fromJson = property['fromJson'];
    final toJson = property['toJson'];
    final description = property.containsKey('description')
        ? property['description']!.toString()
        : null;
    final type = property['type'] as String?;
    final skipEquality = property['ignore_equality'] == true;
    final defaultValue = property['default_value']?.toString();
    final disallowNull = property.containsKey('disallow_null')
        ? (property['disallow_null'] == true)
        : disallowNullForDefaults;
    ItemType itemType;

    if (type == null) {
      throw Exception('$name has no defined type');
    }
    final lowerType = type.toLowerCase();
    if (lowerType == 'object' ||
        lowerType == 'dynamic' ||
        lowerType == 'any') {
      itemType = DynamicType();
    } else if (type == 'bool' || lowerType == 'boolean') {
      itemType = BooleanType();
    } else if (lowerType == 'string') {
      itemType = StringType();
    } else if (lowerType == 'date' || lowerType == 'datetime') {
      itemType = DateTimeType();
    } else if (lowerType == 'double') {
      itemType = DoubleType();
    } else if (type == 'int' || type == 'integer') {
      itemType = IntegerType();
    } else if (lowerType == 'array') {
      final items = property['items'];
      final arrayType = items['type'];
      itemType = ArrayType(_makeGenericName(arrayType));
    } else if (lowerType == 'map') {
      final items = property['items'];
      final keyType = items['key'];
      final valueType = items['value'];
      itemType = MapType(
        key: _makeGenericName(keyType),
        valueName: _makeGenericName(valueType),
      );
    } else {
      itemType = ObjectType(type);
    }
    return Field(
      name: name,
      type: itemType,
      isRequired: required,
      ignore: ignored,
      includeFromJson: includeFromJson,
      includeToJson: includeToJson,
      jsonKey: jsonKey,
      nonFinal: nonFinal,
      description: description,
      includeIfNull: includeIfNull,
      unknownEnumValue: unknownEnumValue,
      fromJson: fromJson,
      toJson: toJson,
      ignoreEquality: skipEquality,
      defaultValue: defaultValue,
      disallowNull: disallowNull,
    );
  } catch (e) {
    print('Something went wrong with $name:\n\n${e.toString()}');
    rethrow;
  }
}