YmlGeneratorConfig constructor
YmlGeneratorConfig(
- PubspecConfig pubspecConfig,
- String configContent,
- String fileName
Implementation
YmlGeneratorConfig(
PubspecConfig pubspecConfig, String configContent, this.fileName) {
final yamlContent = loadYaml(configContent);
if (yamlContent == null) return; // Ignore empty file
yamlContent.forEach((key, value) {
final String baseDirectory =
value['base_directory'] ?? pubspecConfig.baseDirectory;
final String? path = value['path'];
final String? extendsModel = value['extends'];
final bool generateForGenerics =
value['generate_for_generics'] ?? pubspecConfig.generateForGenerics;
final extraImports =
value.containsKey('extra_imports') ? <String>[] : null;
final extraImportsVal = value['extra_imports'];
extraImportsVal?.forEach((e) {
if (e != null) {
extraImports!.add(e.toString());
}
});
final extraAnnotations =
value.containsKey('extra_annotations') ? <String>[] : null;
final extraAnnotationsVal = value['extra_annotations'];
extraAnnotationsVal?.forEach((e) {
if (e != null) {
extraAnnotations!.add(e.toString());
}
});
final description = value['description']?.toString();
final dynamic properties = value['properties'];
final YamlList? converters = value['converters'];
final String? type = value['type'];
if (type == 'custom') {
models.add(CustomModel(
name: key,
path: path,
baseDirectory: baseDirectory,
extraImports: extraImports,
extraAnnotations: extraAnnotations,
));
return;
} else if (type == 'custom_from_to_json') {
models.add(CustomFromToJsonModel(
name: key,
path: path,
baseDirectory: baseDirectory,
extraImports: extraImports,
extraAnnotations: extraAnnotations,
));
return;
} else if (type == 'json_converter') {
models.add(JsonConverterModel(
name: key,
path: path,
baseDirectory: baseDirectory,
extraImports: extraImports,
extraAnnotations: extraAnnotations,
));
return;
}
if (properties == null) {
throw Exception('Properties can not be null. model: $key');
}
if (properties is! YamlMap) {
throw Exception(
'Properties should be a map, right now you are using a ${properties.runtimeType}. model: $key');
}
if (type == 'enum') {
final uppercaseEnums =
(value['uppercase_enums'] ?? pubspecConfig.uppercaseEnums) == true;
final itemType = value['item_type'] == null
? const StringType()
: _parseSimpleType(value['item_type']);
if (itemType is! StringType &&
itemType is! IntegerType &&
itemType is! DoubleType) {
throw Exception(
'item_type should be a string or integer. model: $key');
}
final fields = <EnumField>[];
properties.forEach((propertyKey, propertyValue) {
if (propertyValue != null && propertyValue is! YamlMap) {
throw Exception('$propertyKey should be an object');
}
fields.add(EnumField(
name: uppercaseEnums ? propertyKey.toUpperCase() : propertyKey,
rawName: propertyKey,
value: propertyValue == null
? null
: propertyValue['value'].toString(),
description:
propertyValue == null ? null : propertyValue['description'],
));
});
models.add(EnumModel(
name: key,
path: path,
generateMap: value['generate_map'] == true,
generateExtensions: value['generate_extensions'] == true,
baseDirectory: baseDirectory,
fields: fields,
itemType: itemType,
extraImports: extraImports,
extraAnnotations: extraAnnotations,
description: description,
));
} else {
final staticCreate = (value['static_create'] ?? false) == true;
final disallowNullForDefaults =
value.containsKey('disallow_null_for_defaults')
? (value['disallow_null_for_defaults'] == true)
: pubspecConfig.disallowNullForDefaults;
final fields = <Field>[];
properties.forEach((propertyKey, propertyValue) {
if (propertyValue is YamlMap) {
fields.add(getField(propertyKey, propertyValue,
disallowNullForDefaults: disallowNullForDefaults));
} else if (propertyValue is String) {
fields.add(getSimpleField(name: propertyKey, value: propertyValue));
} else {
throw Exception('$propertyKey should be an object');
}
});
final mappedConverters =
converters?.map((element) => element.toString()).toList();
models.add(ObjectModel(
name: key,
path: path,
extendsModel: extendsModel,
baseDirectory: baseDirectory,
generateForGenerics: generateForGenerics,
fields: fields,
converters: mappedConverters ?? [],
extraImports: extraImports,
extraAnnotations: extraAnnotations,
staticCreate: staticCreate,
equalsAndHashCode: value['equals_and_hash_code'],
explicitToJson: value['explicit_to_json'],
generateToString: value['to_string'],
description: description,
disallowNullForDefaults: disallowNullForDefaults,
));
}
});
}