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 {
    if (property.containsKey('required')) {
      throw ArgumentError(
          'required is removed, follow the migration to version 7.0.0');
    }
    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 optional = type.endsWith('?');
    final typeString = optional ? type.substring(0, type.length - 1) : type;

    itemType = _parseSimpleType(typeString);
    return Field(
      name: name,
      type: itemType,
      isRequired: !optional,
      ignore: ignored,
      includeFromJson: includeFromJson,
      includeToJson: includeToJson,
      jsonKey: jsonKey,
      nonFinal: nonFinal,
      description: description,
      includeIfNull: optional ? includeIfNull : true,
      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;
  }
}