From f0cbbe75cd0601d0e642f59d5568e0ac5ff2b40f Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 23 Feb 2023 13:33:34 +0100 Subject: [PATCH 1/5] Added support for enums & updated versions --- .../article/custom_base_directory_obj.dart | 6 ++-- .../article/no_custom_base_directory_obj.dart | 6 ++-- example/lib/model/ogm.dart | 4 +-- example/lib/model/ogm.g.dart | 22 +++++++++--- example/lib/model/status/status.dart | 14 ++++++++ example/lib/model/user/person/gender.dart | 4 +-- example/lib/model/user/person/person.dart | 4 +-- example/lib/model/user/person/person.g.dart | 34 +++---------------- .../user/profile/admin_profile_data.dart | 5 ++- .../model/user/profile/user_profile_data.dart | 5 ++- .../profile/user_profile_data_extended.dart | 6 ++-- example/lib/model/user/project/project.dart | 17 +++++++--- example/lib/model/user/project/project.g.dart | 10 ++++++ example/lib/model/user/testing.dart | 4 +-- example/model_generator/config.yaml | 8 ++--- example/model_generator/enums.yaml | 15 +++++++- example/pubspec.yaml | 8 ++--- lib/config/yml_generator_config.dart | 8 ++++- lib/model/item_type/array_type.dart | 2 +- lib/model/item_type/boolean_type.dart | 2 +- lib/model/item_type/date_time_type.dart | 2 +- lib/model/item_type/double_type.dart | 2 +- lib/model/item_type/dynamic_type.dart | 2 +- lib/model/item_type/integer_type.dart | 2 +- lib/model/item_type/item_type.dart | 2 +- lib/model/item_type/map_type.dart | 2 +- lib/model/item_type/object_type.dart | 2 +- lib/model/item_type/string_type.dart | 2 +- lib/model/model/enum_model.dart | 4 +++ lib/writer/enum_model_writer.dart | 10 ++++-- 30 files changed, 125 insertions(+), 89 deletions(-) create mode 100644 example/lib/model/status/status.dart diff --git a/example/lib/custom_model_directory/article/custom_base_directory_obj.dart b/example/lib/custom_model_directory/article/custom_base_directory_obj.dart index bee091e..2dab8ff 100644 --- a/example/lib/custom_model_directory/article/custom_base_directory_obj.dart +++ b/example/lib/custom_model_directory/article/custom_base_directory_obj.dart @@ -36,9 +36,7 @@ class CustomBaseDirectoryObj { '}'; } -CustomBaseDirectoryObj deserializeCustomBaseDirectoryObj( - Map json) => - CustomBaseDirectoryObj.fromJson(json); +const deserializeCustomBaseDirectoryObj = CustomBaseDirectoryObj.fromJson; Map serializeCustomBaseDirectoryObj( CustomBaseDirectoryObj object) => @@ -46,7 +44,7 @@ Map serializeCustomBaseDirectoryObj( List deserializeCustomBaseDirectoryObjList( List> jsonList) => - jsonList.map((json) => CustomBaseDirectoryObj.fromJson(json)).toList(); + jsonList.map(CustomBaseDirectoryObj.fromJson).toList(); List> serializeCustomBaseDirectoryObjList( List objects) => diff --git a/example/lib/model/article/no_custom_base_directory_obj.dart b/example/lib/model/article/no_custom_base_directory_obj.dart index b6555e5..306bd77 100644 --- a/example/lib/model/article/no_custom_base_directory_obj.dart +++ b/example/lib/model/article/no_custom_base_directory_obj.dart @@ -37,9 +37,7 @@ class NoCustomBaseDirectoryObj { '}'; } -NoCustomBaseDirectoryObj deserializeNoCustomBaseDirectoryObj( - Map json) => - NoCustomBaseDirectoryObj.fromJson(json); +const deserializeNoCustomBaseDirectoryObj = NoCustomBaseDirectoryObj.fromJson; Map serializeNoCustomBaseDirectoryObj( NoCustomBaseDirectoryObj object) => @@ -47,7 +45,7 @@ Map serializeNoCustomBaseDirectoryObj( List deserializeNoCustomBaseDirectoryObjList( List> jsonList) => - jsonList.map((json) => NoCustomBaseDirectoryObj.fromJson(json)).toList(); + jsonList.map(NoCustomBaseDirectoryObj.fromJson).toList(); List> serializeNoCustomBaseDirectoryObjList( List objects) => diff --git a/example/lib/model/ogm.dart b/example/lib/model/ogm.dart index 0c5f31e..1cf58fb 100644 --- a/example/lib/model/ogm.dart +++ b/example/lib/model/ogm.dart @@ -103,12 +103,12 @@ class OGM { '}'; } -OGM deserializeOGM(Map json) => OGM.fromJson(json); +const deserializeOGM = OGM.fromJson; Map serializeOGM(OGM object) => object.toJson(); List deserializeOGMList(List> jsonList) => - jsonList.map((json) => OGM.fromJson(json)).toList(); + jsonList.map(OGM.fromJson).toList(); List> serializeOGMList(List objects) => objects.map((object) => object.toJson()).toList(); diff --git a/example/lib/model/ogm.g.dart b/example/lib/model/ogm.g.dart index dfd9401..922ffc5 100644 --- a/example/lib/model/ogm.g.dart +++ b/example/lib/model/ogm.g.dart @@ -30,9 +30,8 @@ OGM _$OGMFromJson(Map json) { structuredMessage: json['structuredMessage'] as String?, securityRole: json['securityIndicator'] as String?, mutableProperty: json['mutableProperty'] as String?, - dateChange: json['dateChange'] == null - ? null - : DateTime.parse(json['dateChange'] as String), + dateChange: _$JsonConverterFromJson( + json['dateChange'], const DateTimeConverter().fromJson), fields: (json['fields'] as List?) ?.map((e) => (e as List) .map((e) => Testing.fromJson(e as Map)) @@ -63,9 +62,24 @@ Map _$OGMToJson(OGM instance) { writeNotNull('securityIndicator', instance.securityRole); writeNotNull('mutableProperty', instance.mutableProperty); - writeNotNull('dateChange', instance.dateChange?.toIso8601String()); + writeNotNull( + 'dateChange', + _$JsonConverterToJson( + instance.dateChange, const DateTimeConverter().toJson)); writeNotNull('fields', instance.fields?.map((e) => e.map((e) => e.toJson()).toList()).toList()); val['simpleMap'] = instance.simpleMap?.map((k, e) => MapEntry(k, e.toJson())); return val; } + +Value? _$JsonConverterFromJson( + Object? json, + Value? Function(Json json) fromJson, +) => + json == null ? null : fromJson(json as Json); + +Json? _$JsonConverterToJson( + Value? value, + Json? Function(Value value) toJson, +) => + value == null ? null : toJson(value); diff --git a/example/lib/model/status/status.dart b/example/lib/model/status/status.dart new file mode 100644 index 0000000..432b259 --- /dev/null +++ b/example/lib/model/status/status.dart @@ -0,0 +1,14 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +import 'package:json_annotation/json_annotation.dart'; + +enum Status { + @JsonValue(0) + STATUS_0, + @JsonValue(1) + STATUS_1, + @JsonValue(2) + STATUS_2, + @JsonValue(3) + STATUS_3, +} diff --git a/example/lib/model/user/person/gender.dart b/example/lib/model/user/person/gender.dart index d9ed6bf..687ae82 100644 --- a/example/lib/model/user/person/gender.dart +++ b/example/lib/model/user/person/gender.dart @@ -11,13 +11,13 @@ enum Gender { X, @JsonValue('GENDER_X') GENDER_X, - @JsonValue('GENDER_Y') + @JsonValue('null') GENDER_Y, @JsonValue('gender_z') GENDER_Z, @JsonValue('GENDER_abC') GENDER_ABC, - @JsonValue('GENDER_def') + @JsonValue('null') GENDER_DEF, @JsonValue('GENDER_lap') GENDER_LAP, diff --git a/example/lib/model/user/person/person.dart b/example/lib/model/user/person/person.dart index 1c5b051..1606912 100644 --- a/example/lib/model/user/person/person.dart +++ b/example/lib/model/user/person/person.dart @@ -45,12 +45,12 @@ class Person { '}'; } -Person deserializePerson(Map json) => Person.fromJson(json); +const deserializePerson = Person.fromJson; Map serializePerson(Person object) => object.toJson(); List deserializePersonList(List> jsonList) => - jsonList.map((json) => Person.fromJson(json)).toList(); + jsonList.map(Person.fromJson).toList(); List> serializePersonList(List objects) => objects.map((object) => object.toJson()).toList(); diff --git a/example/lib/model/user/person/person.g.dart b/example/lib/model/user/person/person.g.dart index abda18b..970221b 100644 --- a/example/lib/model/user/person/person.g.dart +++ b/example/lib/model/user/person/person.g.dart @@ -14,49 +14,23 @@ Person _$PersonFromJson(Map json) { return Person( firstName: json['firstName'] as String, gender: - _$enumDecode(_$GenderEnumMap, json['gender'], unknownValue: Gender.X), + $enumDecode(_$GenderEnumMap, json['gender'], unknownValue: Gender.X), ); } Map _$PersonToJson(Person instance) => { 'firstName': instance.firstName, - 'gender': _$GenderEnumMap[instance.gender], + 'gender': _$GenderEnumMap[instance.gender]!, }; -K _$enumDecode( - Map enumValues, - Object? source, { - K? unknownValue, -}) { - if (source == null) { - throw ArgumentError( - 'A value must be provided. Supported values: ' - '${enumValues.values.join(', ')}', - ); - } - - return enumValues.entries.singleWhere( - (e) => e.value == source, - orElse: () { - if (unknownValue == null) { - throw ArgumentError( - '`$source` is not one of the supported values: ' - '${enumValues.values.join(', ')}', - ); - } - return MapEntry(unknownValue, enumValues.values.first); - }, - ).key; -} - const _$GenderEnumMap = { Gender.MALE: '_mAl3', Gender.FEMALE: 'femAle', Gender.X: 'X', Gender.GENDER_X: 'GENDER_X', - Gender.GENDER_Y: 'GENDER_Y', + Gender.GENDER_Y: 'null', Gender.GENDER_Z: 'gender_z', Gender.GENDER_ABC: 'GENDER_abC', - Gender.GENDER_DEF: 'GENDER_def', + Gender.GENDER_DEF: 'null', Gender.GENDER_LAP: 'GENDER_lap', }; diff --git a/example/lib/model/user/profile/admin_profile_data.dart b/example/lib/model/user/profile/admin_profile_data.dart index 50f9586..ac5a8af 100644 --- a/example/lib/model/user/profile/admin_profile_data.dart +++ b/example/lib/model/user/profile/admin_profile_data.dart @@ -79,15 +79,14 @@ class AdminProfileData extends UserProfileDataExtended { '}'; } -AdminProfileData deserializeAdminProfileData(Map json) => - AdminProfileData.fromJson(json); +const deserializeAdminProfileData = AdminProfileData.fromJson; Map serializeAdminProfileData(AdminProfileData object) => object.toJson(); List deserializeAdminProfileDataList( List> jsonList) => - jsonList.map((json) => AdminProfileData.fromJson(json)).toList(); + jsonList.map(AdminProfileData.fromJson).toList(); List> serializeAdminProfileDataList( List objects) => diff --git a/example/lib/model/user/profile/user_profile_data.dart b/example/lib/model/user/profile/user_profile_data.dart index 3c94ac8..84b9c2d 100644 --- a/example/lib/model/user/profile/user_profile_data.dart +++ b/example/lib/model/user/profile/user_profile_data.dart @@ -100,15 +100,14 @@ class UserProfileData { '}'; } -UserProfileData deserializeUserProfileData(Map json) => - UserProfileData.fromJson(json); +const deserializeUserProfileData = UserProfileData.fromJson; Map serializeUserProfileData(UserProfileData object) => object.toJson(); List deserializeUserProfileDataList( List> jsonList) => - jsonList.map((json) => UserProfileData.fromJson(json)).toList(); + jsonList.map(UserProfileData.fromJson).toList(); List> serializeUserProfileDataList( List objects) => diff --git a/example/lib/model/user/profile/user_profile_data_extended.dart b/example/lib/model/user/profile/user_profile_data_extended.dart index 0a361fe..c0e77fb 100644 --- a/example/lib/model/user/profile/user_profile_data_extended.dart +++ b/example/lib/model/user/profile/user_profile_data_extended.dart @@ -76,9 +76,7 @@ class UserProfileDataExtended extends UserProfileData { '}'; } -UserProfileDataExtended deserializeUserProfileDataExtended( - Map json) => - UserProfileDataExtended.fromJson(json); +const deserializeUserProfileDataExtended = UserProfileDataExtended.fromJson; Map serializeUserProfileDataExtended( UserProfileDataExtended object) => @@ -86,7 +84,7 @@ Map serializeUserProfileDataExtended( List deserializeUserProfileDataExtendedList( List> jsonList) => - jsonList.map((json) => UserProfileDataExtended.fromJson(json)).toList(); + jsonList.map(UserProfileDataExtended.fromJson).toList(); List> serializeUserProfileDataExtendedList( List objects) => diff --git a/example/lib/model/user/project/project.dart b/example/lib/model/user/project/project.dart index b463530..b9afa89 100644 --- a/example/lib/model/user/project/project.dart +++ b/example/lib/model/user/project/project.dart @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart'; import 'package:json_annotation/json_annotation.dart'; +import 'package:model_generator_example/model/status/status.dart'; part 'project.g.dart'; @@ -16,10 +17,14 @@ class Project { final String name; @JsonKey(name: 'cost', includeIfNull: false) final double? cost; + @JsonKey( + name: 'status', includeIfNull: false, unknownEnumValue: Status.STATUS_0) + final Status? status; const Project({ this.name = 'test', this.cost = 0.2, + this.status, }); factory Project.fromJson(Object? json) => @@ -36,24 +41,26 @@ class Project { other is Project && runtimeType == other.runtimeType && name == other.name && - cost == other.cost; + cost == other.cost && + status == other.status; @override - int get hashCode => name.hashCode ^ cost.hashCode; + int get hashCode => name.hashCode ^ cost.hashCode ^ status.hashCode; @override String toString() => 'Project{' 'name: $name, ' - 'cost: $cost' + 'cost: $cost, ' + 'status: $status' '}'; } -Project deserializeProject(Map json) => Project.fromJson(json); +const deserializeProject = Project.fromJson; Map serializeProject(Project object) => object.toJson(); List deserializeProjectList(List> jsonList) => - jsonList.map((json) => Project.fromJson(json)).toList(); + jsonList.map(Project.fromJson).toList(); List> serializeProjectList(List objects) => objects.map((object) => object.toJson()).toList(); diff --git a/example/lib/model/user/project/project.g.dart b/example/lib/model/user/project/project.g.dart index 7a23d95..bd63dd7 100644 --- a/example/lib/model/user/project/project.g.dart +++ b/example/lib/model/user/project/project.g.dart @@ -9,6 +9,8 @@ part of 'project.dart'; Project _$ProjectFromJson(Map json) => Project( name: json['name'] as String? ?? 'test', cost: (json['cost'] as num?)?.toDouble() ?? 0.2, + status: $enumDecodeNullable(_$StatusEnumMap, json['status'], + unknownValue: Status.STATUS_0), ); Map _$ProjectToJson(Project instance) { @@ -23,5 +25,13 @@ Map _$ProjectToJson(Project instance) { } writeNotNull('cost', instance.cost); + writeNotNull('status', _$StatusEnumMap[instance.status]); return val; } + +const _$StatusEnumMap = { + Status.STATUS_0: 0, + Status.STATUS_1: 1, + Status.STATUS_2: 2, + Status.STATUS_3: 3, +}; diff --git a/example/lib/model/user/testing.dart b/example/lib/model/user/testing.dart index c49e8f9..cc44e3f 100644 --- a/example/lib/model/user/testing.dart +++ b/example/lib/model/user/testing.dart @@ -43,12 +43,12 @@ class Testing { Map toJson() => _$TestingToJson(this); } -Testing deserializeTesting(Map json) => Testing.fromJson(json); +const deserializeTesting = Testing.fromJson; Map serializeTesting(Testing object) => object.toJson(); List deserializeTestingList(List> jsonList) => - jsonList.map((json) => Testing.fromJson(json)).toList(); + jsonList.map(Testing.fromJson).toList(); List> serializeTestingList(List objects) => objects.map((object) => object.toJson()).toList(); diff --git a/example/model_generator/config.yaml b/example/model_generator/config.yaml index 8ebddd5..9acaaa7 100644 --- a/example/model_generator/config.yaml +++ b/example/model_generator/config.yaml @@ -175,8 +175,6 @@ Project: cost: type: double default_value: 0.2 - - - - - + status: + type: Status + unknown_enum_value: STATUS_0 diff --git a/example/model_generator/enums.yaml b/example/model_generator/enums.yaml index 99f74e8..5a0aead 100644 --- a/example/model_generator/enums.yaml +++ b/example/model_generator/enums.yaml @@ -17,4 +17,17 @@ Gender: GENDER_def: value: GENDER_lap: - value: GENDER_lap \ No newline at end of file + value: GENDER_lap +Status: + path: status + type: enum + item_type: int + properties: + status_0: + value: 0 + status_1: + value: 1 + status_2: + value: 2 + status_3: + value: 3 diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 37b109c..0c0b976 100755 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -3,16 +3,16 @@ description: A project used to demo the model generator version: 1.0.0+1 environment: - sdk: ">=2.12.0 <3.0.0" + sdk: ">=2.18.0 <3.0.0" dependencies: flutter: sdk: flutter - json_serializable: ^5.0.1 - provider: ^6.0.0 + json_serializable: ^6.6.1 + provider: ^6.0.5 dev_dependencies: - build_runner: ^2.1.2 + build_runner: ^2.3.3 flutter_test: sdk: flutter model_generator: diff --git a/lib/config/yml_generator_config.dart b/lib/config/yml_generator_config.dart index b5afb08..7a27d31 100644 --- a/lib/config/yml_generator_config.dart +++ b/lib/config/yml_generator_config.dart @@ -99,6 +99,9 @@ class YmlGeneratorConfig { if (type == 'enum') { final uppercaseEnums = (value['uppercase_enums'] ?? pubspecConfig.uppercaseEnums) == true; + final itemType = value['item_type'] == null + ? const StringType() + : _parseSimpleType(value['item_type']); final fields = []; properties.forEach((propertyKey, propertyValue) { @@ -108,7 +111,9 @@ class YmlGeneratorConfig { fields.add(EnumField( name: uppercaseEnums ? propertyKey.toUpperCase() : propertyKey, rawName: propertyKey, - value: propertyValue == null ? null : propertyValue['value'], + value: propertyValue == null + ? null + : propertyValue['value'].toString(), description: propertyValue == null ? null : propertyValue['description'], )); @@ -120,6 +125,7 @@ class YmlGeneratorConfig { generateExtensions: value['generate_extensions'] == true, baseDirectory: baseDirectory, fields: fields, + itemType: itemType, extraImports: extraImports, extraAnnotations: extraAnnotations, description: description, diff --git a/lib/model/item_type/array_type.dart b/lib/model/item_type/array_type.dart index c6edc18..2553843 100644 --- a/lib/model/item_type/array_type.dart +++ b/lib/model/item_type/array_type.dart @@ -1,5 +1,5 @@ import 'item_type.dart'; class ArrayType extends ItemType { - ArrayType(String name) : super(name); + const ArrayType(String name) : super(name); } diff --git a/lib/model/item_type/boolean_type.dart b/lib/model/item_type/boolean_type.dart index 51f3899..d69b06a 100644 --- a/lib/model/item_type/boolean_type.dart +++ b/lib/model/item_type/boolean_type.dart @@ -1,5 +1,5 @@ import 'item_type.dart'; class BooleanType extends ItemType { - BooleanType() : super('bool'); + const BooleanType() : super('bool'); } diff --git a/lib/model/item_type/date_time_type.dart b/lib/model/item_type/date_time_type.dart index 658599b..374daf2 100644 --- a/lib/model/item_type/date_time_type.dart +++ b/lib/model/item_type/date_time_type.dart @@ -1,5 +1,5 @@ import 'item_type.dart'; class DateTimeType extends ItemType { - DateTimeType() : super('DateTime'); + const DateTimeType() : super('DateTime'); } diff --git a/lib/model/item_type/double_type.dart b/lib/model/item_type/double_type.dart index f2b1eaa..8727eff 100644 --- a/lib/model/item_type/double_type.dart +++ b/lib/model/item_type/double_type.dart @@ -1,5 +1,5 @@ import 'item_type.dart'; class DoubleType extends ItemType { - DoubleType() : super('double'); + const DoubleType() : super('double'); } diff --git a/lib/model/item_type/dynamic_type.dart b/lib/model/item_type/dynamic_type.dart index d6c9f3a..ac626fb 100644 --- a/lib/model/item_type/dynamic_type.dart +++ b/lib/model/item_type/dynamic_type.dart @@ -1,5 +1,5 @@ import 'item_type.dart'; class DynamicType extends ItemType { - DynamicType() : super('dynamic'); + const DynamicType() : super('dynamic'); } diff --git a/lib/model/item_type/integer_type.dart b/lib/model/item_type/integer_type.dart index 37019a3..aeeb404 100644 --- a/lib/model/item_type/integer_type.dart +++ b/lib/model/item_type/integer_type.dart @@ -1,5 +1,5 @@ import 'item_type.dart'; class IntegerType extends ItemType { - IntegerType() : super('int'); + const IntegerType() : super('int'); } diff --git a/lib/model/item_type/item_type.dart b/lib/model/item_type/item_type.dart index cf330d9..511e4d8 100644 --- a/lib/model/item_type/item_type.dart +++ b/lib/model/item_type/item_type.dart @@ -1,5 +1,5 @@ abstract class ItemType { final String name; - ItemType(this.name); + const ItemType(this.name); } diff --git a/lib/model/item_type/map_type.dart b/lib/model/item_type/map_type.dart index b6e7dbd..2fcf84d 100644 --- a/lib/model/item_type/map_type.dart +++ b/lib/model/item_type/map_type.dart @@ -3,5 +3,5 @@ import 'item_type.dart'; class MapType extends ItemType { final String valueName; - MapType({required String key, required this.valueName}) : super(key); + const MapType({required String key, required this.valueName}) : super(key); } diff --git a/lib/model/item_type/object_type.dart b/lib/model/item_type/object_type.dart index 456dee2..fd5404b 100644 --- a/lib/model/item_type/object_type.dart +++ b/lib/model/item_type/object_type.dart @@ -1,5 +1,5 @@ import 'item_type.dart'; class ObjectType extends ItemType { - ObjectType(String name) : super(name); + const ObjectType(String name) : super(name); } diff --git a/lib/model/item_type/string_type.dart b/lib/model/item_type/string_type.dart index 0b2d5d1..f1266ca 100644 --- a/lib/model/item_type/string_type.dart +++ b/lib/model/item_type/string_type.dart @@ -1,5 +1,5 @@ import 'item_type.dart'; class StringType extends ItemType { - StringType() : super('String'); + const StringType() : super('String'); } diff --git a/lib/model/model/enum_model.dart b/lib/model/model/enum_model.dart index 079ff54..ebea3c1 100644 --- a/lib/model/model/enum_model.dart +++ b/lib/model/model/enum_model.dart @@ -1,7 +1,10 @@ +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? fields; + final ItemType itemType; final bool generateMap; final bool generateExtensions; @@ -10,6 +13,7 @@ class EnumModel extends Model { String? path, String? baseDirectory, this.fields, + this.itemType = const StringType(), List? extraImports, List? extraAnnotations, this.generateMap = false, diff --git a/lib/writer/enum_model_writer.dart b/lib/writer/enum_model_writer.dart index e3307cd..99874ec 100644 --- a/lib/writer/enum_model_writer.dart +++ b/lib/writer/enum_model_writer.dart @@ -1,3 +1,4 @@ +import 'package:model_generator/model/item_type/string_type.dart'; import 'package:model_generator/model/model/enum_model.dart'; import 'package:model_generator/writer/object_model_writer.dart'; @@ -27,9 +28,12 @@ class EnumModelWriter { if (description != null) { sb.writeln(' ///$description'); } - sb - ..writeln(" @JsonValue('$jsonValue')") - ..writeln(' ${key.name},'); + if (jsonModel.itemType is StringType) { + sb.writeln(" @JsonValue('$jsonValue')"); + } else { + sb.writeln(" @JsonValue($jsonValue)"); + } + sb.writeln(' ${key.name},'); }); sb.writeln('}'); From 3625616b9bbe84077e2660844f7095ee08114be5 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 23 Feb 2023 13:38:38 +0100 Subject: [PATCH 2/5] Added extra tests --- CHANGELOG.md | 3 +++ pubspec.yaml | 2 +- .../normal_with_int_type.txt | 11 +++++++++ test/writer/enum_model_writer_test.dart | 24 +++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test/writer/enum_model_writer/normal_with_int_type.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c6e77f..ffb1fe6 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # Changelog +## [6.2.0] - 2023-02-23 +- Better enum support. Right now, String, int, double can be used to map a value to an enum. + ## [6.1.0] - 2022-10-14 - Support passing a directory instead of a file. All .yaml files inside this folder (recursively) will be used to build the final model data. diff --git a/pubspec.yaml b/pubspec.yaml index f30ac71..30fd54b 100755 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: model_generator description: Dart tool to automaticly generate models from a yml file to speed up your development flow. -version: 6.1.0 +version: 6.2.0 homepage: https://github.com/icapps/flutter-model-generator environment: diff --git a/test/writer/enum_model_writer/normal_with_int_type.txt b/test/writer/enum_model_writer/normal_with_int_type.txt new file mode 100644 index 0000000..e69141c --- /dev/null +++ b/test/writer/enum_model_writer/normal_with_int_type.txt @@ -0,0 +1,11 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +import 'package:json_annotation/json_annotation.dart'; + +enum MyEnumModel { + ///A good description of this field + @JsonValue(1) + MY_VALUE_1, + @JsonValue(2) + MY_VALUE_2, +} diff --git a/test/writer/enum_model_writer_test.dart b/test/writer/enum_model_writer_test.dart index cfc9d69..c31e41f 100644 --- a/test/writer/enum_model_writer_test.dart +++ b/test/writer/enum_model_writer_test.dart @@ -1,3 +1,4 @@ +import 'package:model_generator/model/item_type/integer_type.dart'; import 'package:test/test.dart'; import 'package:model_generator/model/model/enum_model.dart'; @@ -27,6 +28,29 @@ void main() { WriterTestHelper.testEnumModelWriter(model, 'normal'); }); + test('Normal EnumModel with int type', () { + final model = EnumModel( + name: 'MyEnumModel', + path: 'path_to_my_model', + baseDirectory: 'base_dir', + itemType: IntegerType(), + fields: [ + EnumField( + name: 'MY_VALUE_1', + rawName: 'MY_VALUE_1', + value: '1', + description: 'A good description of this field', + ), + EnumField( + name: 'MY_VALUE_2', + rawName: 'MY_VALUE_2', + value: '2', + ), + ], + ); + WriterTestHelper.testEnumModelWriter(model, 'normal_with_int_type'); + }); + test('Normal EnumModel custom value', () { final model = EnumModel( name: 'MyEnumModel', From afe62011169e6289182ed5859354e92cced5bb4d Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 23 Feb 2023 13:40:02 +0100 Subject: [PATCH 3/5] Check that the types are only allowed --- lib/config/yml_generator_config.dart | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/config/yml_generator_config.dart b/lib/config/yml_generator_config.dart index 7a27d31..cb462a6 100644 --- a/lib/config/yml_generator_config.dart +++ b/lib/config/yml_generator_config.dart @@ -103,6 +103,13 @@ class YmlGeneratorConfig { ? 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 = []; properties.forEach((propertyKey, propertyValue) { if (propertyValue != null && propertyValue is! YamlMap) { From 878ae3dc88e63d71afb7af90d1f90fc95578e74e Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 23 Feb 2023 13:41:17 +0100 Subject: [PATCH 4/5] Added extra test --- .../normal_with_double_type.txt | 11 +++++++++ test/writer/enum_model_writer_test.dart | 23 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 test/writer/enum_model_writer/normal_with_double_type.txt diff --git a/test/writer/enum_model_writer/normal_with_double_type.txt b/test/writer/enum_model_writer/normal_with_double_type.txt new file mode 100644 index 0000000..fb99b3d --- /dev/null +++ b/test/writer/enum_model_writer/normal_with_double_type.txt @@ -0,0 +1,11 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +import 'package:json_annotation/json_annotation.dart'; + +enum MyEnumModel { + ///A good description of this field + @JsonValue(1) + MY_VALUE_1, + @JsonValue(2.2) + MY_VALUE_2, +} diff --git a/test/writer/enum_model_writer_test.dart b/test/writer/enum_model_writer_test.dart index c31e41f..18c9c46 100644 --- a/test/writer/enum_model_writer_test.dart +++ b/test/writer/enum_model_writer_test.dart @@ -1,3 +1,4 @@ +import 'package:model_generator/model/item_type/double_type.dart'; import 'package:model_generator/model/item_type/integer_type.dart'; import 'package:test/test.dart'; import 'package:model_generator/model/model/enum_model.dart'; @@ -50,6 +51,28 @@ void main() { ); WriterTestHelper.testEnumModelWriter(model, 'normal_with_int_type'); }); + test('Normal EnumModel with double type', () { + final model = EnumModel( + name: 'MyEnumModel', + path: 'path_to_my_model', + baseDirectory: 'base_dir', + itemType: DoubleType(), + fields: [ + EnumField( + name: 'MY_VALUE_1', + rawName: 'MY_VALUE_1', + value: '1', + description: 'A good description of this field', + ), + EnumField( + name: 'MY_VALUE_2', + rawName: 'MY_VALUE_2', + value: '2.2', + ), + ], + ); + WriterTestHelper.testEnumModelWriter(model, 'normal_with_double_type'); + }); test('Normal EnumModel custom value', () { final model = EnumModel( From e647c5ef91482af281e171a31f5496bff82a3da1 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Tue, 28 Feb 2023 15:43:53 +0100 Subject: [PATCH 5/5] Fixed pr comments --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 90a1614..c803577 100755 --- a/README.md +++ b/README.md @@ -378,7 +378,7 @@ Items post-fixed with `?` will be marked optional. ## Enum support -Add enums with custom values +Add enums with custom values (can be mapped to String,double,int) ```yaml Gender: