Skip to content

Commit

Permalink
#130: removed unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
jorre127 committed Sep 4, 2023
1 parent bb14a2c commit cc1a73a
Show file tree
Hide file tree
Showing 22 changed files with 140 additions and 508 deletions.
22 changes: 13 additions & 9 deletions example/lib/model/user/person/gender.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 39 additions & 82 deletions lib/config/yml_generator_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,24 @@ class YmlGeneratorConfig {

List<Model> get models => _models;

YmlGeneratorConfig(
PubspecConfig pubspecConfig, String configContent, this.fileName) {
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 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 bool generateForGenerics = value['generate_for_generics'] ?? pubspecConfig.generateForGenerics;

final extraImports =
value.containsKey('extra_imports') ? <String>[] : null;
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 extraAnnotations = value.containsKey('extra_annotations') ? <String>[] : null;
final extraAnnotationsVal = value['extra_annotations'];
extraAnnotationsVal?.forEach((e) {
if (e != null) {
Expand Down Expand Up @@ -93,69 +88,54 @@ class YmlGeneratorConfig {
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');
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 uppercaseEnums = (value['uppercase_enums'] ?? pubspecConfig.uppercaseEnums) == true;

final fields = <EnumField>[];
properties.forEach((propertyKey, propertyValue) {
if (propertyValue != null && propertyValue is! YamlMap) {
throw Exception('$propertyKey should be an object');
}
final propertiesYaml = propertyValue as YamlMap;
final properties = <EnumProperty>[];
propertiesYaml.forEach((propertyKey, propertyValue) {
properties.add(EnumProperty(
value: propertyValue,
name: propertyKey,
));
});
fields.add(EnumField(
name: uppercaseEnums ? propertyKey.toUpperCase() : propertyKey,
rawName: propertyKey,
value: propertyValue == null
? null
: propertyValue['value'].toString(),
description:
propertyValue == null ? null : propertyValue['description'],
enumProperties: properties,
));
});
models.add(EnumModel(
name: key,
path: path,
generateMap: value['generate_map'] == true,
generateExtensions: value['generate_extensions'] == true,
keyProperty: value['key_property'],
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 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));
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();
final mappedConverters = converters?.map((element) => element.toString()).toList();
models.add(ObjectModel(
name: key,
path: path,
Expand All @@ -177,36 +157,25 @@ class YmlGeneratorConfig {
});
}

Field getField(String name, YamlMap property,
{required bool disallowNullForDefaults}) {
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');
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 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 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;
final disallowNull = property.containsKey('disallow_null') ? (property['disallow_null'] == true) : disallowNullForDefaults;
ItemType itemType;

if (type == null) {
Expand Down Expand Up @@ -272,9 +241,7 @@ class YmlGeneratorConfig {
return 'DateTime';
} else if (lowerType == 'int' || lowerType == 'integer') {
return 'int';
} else if (lowerType == 'object' ||
lowerType == 'dynamic' ||
lowerType == 'any') {
} else if (lowerType == 'object' || lowerType == 'dynamic' || lowerType == 'any') {
return 'dynamic';
} else {
return typeName;
Expand All @@ -289,17 +256,15 @@ class YmlGeneratorConfig {
//Maybe a generic
final dartType = DartType(name);
if (dartType.generics.isEmpty) {
throw Exception(
'getPathForName is null: because `$name` was not added to the config file');
throw Exception('getPathForName is null: because `$name` was not added to the config file');
}
final paths = <String>{};
for (final element in dartType.generics) {
paths.addAll(getPathsForName(pubspecConfig, element.toString()));
}
return paths;
} else {
final baseDirectory =
foundModel.baseDirectory ?? pubspecConfig.baseDirectory;
final baseDirectory = foundModel.baseDirectory ?? pubspecConfig.baseDirectory;
final path = foundModel.path;
if (path == null) {
return [baseDirectory];
Expand Down Expand Up @@ -353,26 +318,22 @@ class YmlGeneratorConfig {

Model? getModelByName(ItemType itemType) {
if (itemType is! ObjectType) return null;
final model =
models.firstWhereOrNull((model) => model.name == itemType.name);
final model = models.firstWhereOrNull((model) => model.name == itemType.name);
if (model == null) {
throw Exception(
'getModelByName is null: because `${itemType.name}` was not added to the config file');
throw Exception('getModelByName is null: because `${itemType.name}` was not added to the config file');
}
return model;
}

void checkTypesKnown(final Set<String> names, String type) {
if (!TypeChecker.isKnownDartType(type) && !names.contains(type)) {
throw Exception(
'Could not generate all models. `$type` is not added to the config file, but is extended. These types are known: ${names.join(',')}');
throw Exception('Could not generate all models. `$type` is not added to the config file, but is extended. These types are known: ${names.join(',')}');
}
}

ItemType _parseSimpleType(String type) {
final listRegex = RegExp(r'^\s*[Ll]ist<\s*([a-zA-Z_0-9<>]*)\s*>\s*$');
final mapRegex =
RegExp(r'^\s*[Mm]ap<([a-zA-Z_0-9<>]*)\s*,\s*([a-zA-Z_0-9<>]*)\s*>\s*$');
final mapRegex = RegExp(r'^\s*[Mm]ap<([a-zA-Z_0-9<>]*)\s*,\s*([a-zA-Z_0-9<>]*)\s*>\s*$');

final lowerType = type.toLowerCase();

Expand All @@ -393,21 +354,17 @@ class YmlGeneratorConfig {
return ArrayType(_makeGenericName(arrayType));
} else if (mapRegex.hasMatch(type)) {
final match = mapRegex.firstMatch(type)!;
return MapType(
key: _makeGenericName(match.group(1)!),
valueName: _makeGenericName(match.group(2)!));
return MapType(key: _makeGenericName(match.group(1)!), valueName: _makeGenericName(match.group(2)!));
}
return ObjectType(type);
}

YmlGeneratorConfig.merge(Iterable<YmlGeneratorConfig> configs, String dirName)
: fileName = dirName {
YmlGeneratorConfig.merge(Iterable<YmlGeneratorConfig> configs, String dirName) : fileName = dirName {
final names = <String, YmlGeneratorConfig>{};
for (final config in configs) {
for (final model in config.models) {
if (names.containsKey(model.name)) {
throw Exception(
'Model with same name ${model.name} found in multiple files: ${names[model.name]!.fileName} and ${config.fileName}');
throw Exception('Model with same name ${model.name} found in multiple files: ${names[model.name]!.fileName} and ${config.fileName}');
}
names[model.name] = config;
}
Expand Down
33 changes: 18 additions & 15 deletions lib/model/model/enum_model.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import 'package:model_generator/model/item_type/integer_type.dart';
import 'package:model_generator/model/item_type/item_type.dart';
import 'package:model_generator/model/item_type/string_type.dart';
import 'package:model_generator/model/model/model.dart';

class EnumModel extends Model {
final List<EnumField>? fields;
final ItemType itemType;
final bool generateMap;
final bool generateExtensions;
final String keyProperty;

EnumModel({
required String name,
required this.keyProperty,
String? path,
String? baseDirectory,
this.fields,
this.itemType = const StringType(),
List<String>? extraImports,
List<String>? extraAnnotations,
this.generateMap = false,
this.generateExtensions = false,
String? description,
}) : super(
name: name,
Expand All @@ -31,27 +27,34 @@ class EnumModel extends Model {

class EnumField {
final String name;
final List<EnumProperty> enumProperties;
final String serializedName;
final String? value;
final String? description;

EnumField._({
required this.name,
required this.serializedName,
required this.value,
required this.description,
required this.enumProperties,
});

factory EnumField({
required String name,
required List<EnumProperty> enumProperties,
required String rawName,
String? value,
String? description,
}) =>
EnumField._(
name: name,
enumProperties: enumProperties,
serializedName: rawName,
value: value,
description: description,
);
}

class EnumProperty {
final String value;
final String name;
final ItemType type;

EnumProperty({
required this.value,
required this.name,
}) : type = IntegerType();
}
Loading

0 comments on commit cc1a73a

Please sign in to comment.