From 78f045b07f6c40d051b89e81a4c41ae5a48f8534 Mon Sep 17 00:00:00 2001 From: "Graciliano M. Passos" Date: Tue, 9 Nov 2021 21:08:02 -0300 Subject: [PATCH] ## 1.0.33 - `Entity` & `EntityHandler`: - Added support for enums. - Added `enumToName` and `enumFromName` - reflection_factory: ^1.0.18 --- CHANGELOG.md | 7 ++ bones_api.iml | 6 +- lib/src/bones_api_base.dart | 2 +- lib/src/bones_api_condition.dart | 15 ++- lib/src/bones_api_entity.dart | 111 +++++++++++++++--- lib/src/bones_api_entity_adapter.dart | 19 ++- lib/src/bones_api_utils.dart | 19 +++ pubspec.yaml | 4 +- test/bones_api_entity_test.dart | 45 ++++--- test/bones_api_test.reflection.g.dart | 4 +- test/bones_api_test_adapter.dart | 14 ++- test/bones_api_test_entities.dart | 19 ++- .../bones_api_test_entities.reflection.g.dart | 92 +++++++++++++-- test/bones_api_utils_test.dart | 52 +++++++- 14 files changed, 333 insertions(+), 76 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0473bf..595d49d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.0.33 + +- `Entity` & `EntityHandler`: + - Added support for enums. +- Added `enumToName` and `enumFromName` +- reflection_factory: ^1.0.18 + ## 1.0.32 - `Json`: diff --git a/bones_api.iml b/bones_api.iml index df9fe8c..42daf00 100644 --- a/bones_api.iml +++ b/bones_api.iml @@ -3,11 +3,11 @@ - - - + + + diff --git a/lib/src/bones_api_base.dart b/lib/src/bones_api_base.dart index b45218a..cbd9727 100644 --- a/lib/src/bones_api_base.dart +++ b/lib/src/bones_api_base.dart @@ -16,7 +16,7 @@ import 'bones_api_utils.dart'; /// Root class of an API. abstract class APIRoot { // ignore: constant_identifier_names - static const String VERSION = '1.0.32'; + static const String VERSION = '1.0.33'; static final Map _instances = {}; diff --git a/lib/src/bones_api_condition.dart b/lib/src/bones_api_condition.dart index 30be358..33c33d9 100644 --- a/lib/src/bones_api_condition.dart +++ b/lib/src/bones_api_condition.dart @@ -151,18 +151,23 @@ class ConditionParameter extends ConditionElement { if (myValue is Iterable) { if (otherValue is Iterable) { - var equals = isEqualsIterableDeep(myValue, otherValue); + var equals = isEqualsIterableDeep(myValue, otherValue, + valueEquality: EntityHandler.equalsValuesBasic); return equals; } else { - var contains = myValue.contains(otherValue); + var contains = myValue + .where((v) => EntityHandler.equalsValuesBasic(v, otherValue)) + .isNotEmpty; return contains; } } else if (otherValue is Iterable) { - var contains = otherValue.contains(myValue); + var contains = otherValue + .where((v) => EntityHandler.equalsValuesBasic(v, myValue)) + .isNotEmpty; return contains; } - return myValue == otherValue; + return EntityHandler.equalsValuesBasic(myValue, otherValue); } bool matchesIn(List values, @@ -556,7 +561,7 @@ class ConditionID extends Condition { positionalParameters: positionalParameters, namedParameters: namedParameters); } else { - return id == idValue; + return EntityHandler.equalsValuesBasic(id, idValue); } } diff --git a/lib/src/bones_api_entity.dart b/lib/src/bones_api_entity.dart index a61b903..3e13b8b 100644 --- a/lib/src/bones_api_entity.dart +++ b/lib/src/bones_api_entity.dart @@ -252,12 +252,22 @@ abstract class EntityHandler with FieldsFromMap { var entityRepository = valEntityHandler?.getEntityRepository(type: type.type); - if (entityRepository == null) { - throw StateError("Can't resolve value for type: $type -> $value"); + if (entityRepository != null) { + var retEntity = entityRepository.selectByID(value); + return retEntity.resolveMapped((val) => val as T?); } - var retEntity = entityRepository.selectByID(value); - return retEntity.resolveMapped((val) => val as T?); + try { + var value2 = Json.fromJson(value, type: type.type); + + if (value2 != null) { + return value2 as T?; + } else { + return value as T?; + } + } catch (e) { + throw StateError("ERROR resolving value for type: $type -> $value. $e"); + } } else { return type.parse(value); } @@ -386,23 +396,18 @@ abstract class EntityHandler with FieldsFromMap { } bool equalsValues(Object? value1, Object? value2) { - //if (identical(value1, value2)) return true ; - if (value1 == null) return value2 == null; if (value2 == null) return false; + if (identical(value1, value2) || value1 == value2) return true; - if (value1 is DateTime || value2 is DateTime) { - var t1 = TypeParser.parseInt(value1); - var t2 = TypeParser.parseInt(value2); - return t1 == t2; - } + var equals = equalsValuesDateTime(value1, value2); + if (equals != null) return equals; - var primitive1 = TypeParser.isPrimitiveValue(value1); - var primitive2 = TypeParser.isPrimitiveValue(value2); + equals = equalsValuesPrimitive(value1, value2); + if (equals != null) return equals; - if (primitive1 && primitive2) { - return value1 == value2; - } + equals = equalsValuesEnum(value1, value2); + if (equals != null) return equals; var collection1 = TypeParser.isCollectionValue(value1); var collection2 = TypeParser.isCollectionValue(value2); @@ -411,8 +416,8 @@ abstract class EntityHandler with FieldsFromMap { return isEqualsDeep(value1, value2, valueEquality: equalsValues); } - var maybeEntity1 = !primitive1 && !collection1; - var maybeEntity2 = !primitive2 && !collection2; + var maybeEntity1 = !collection1 && !TypeParser.isPrimitiveValue(value1); + var maybeEntity2 = !collection2 && !TypeParser.isPrimitiveValue(value2); var entityHandler1 = maybeEntity1 ? getEntityHandler(obj: value1) : null; var entityHandler2 = maybeEntity2 ? getEntityHandler(obj: value2) : null; @@ -438,7 +443,75 @@ abstract class EntityHandler with FieldsFromMap { } } - return value1 == value2; + return false; + } + + static bool equalsValuesBasic(Object? value1, Object? value2) { + if (value1 == null) return value2 == null; + if (value2 == null) return false; + if (identical(value1, value2) || value1 == value2) return true; + + var equals = equalsValuesDateTime(value1, value2); + if (equals != null) return equals; + + equals = equalsValuesPrimitive(value1, value2); + if (equals != null) return equals; + + equals = equalsValuesEnum(value1, value2); + if (equals != null) return equals; + + equals = equalsValuesCollection(value1, value2); + if (equals != null) return equals; + + return false; + } + + static bool? equalsValuesDateTime(Object value1, Object value2) { + if (value1 is DateTime || value2 is DateTime) { + var t1 = TypeParser.parseInt(value1); + var t2 = TypeParser.parseInt(value2); + return t1 == t2; + } + + return null; + } + + static bool? equalsValuesPrimitive(Object value1, Object value2) { + var primitive1 = TypeParser.isPrimitiveValue(value1); + var primitive2 = TypeParser.isPrimitiveValue(value2); + + if (primitive1 && primitive2) { + return value1 == value2; + } + + return null; + } + + static bool? equalsValuesEnum(Object value1, Object value2) { + var enum1 = value1 is Enum; + var enum2 = value2 is Enum; + + if (enum1 && enum2) { + return value1 == value2; + } else if (enum1 || enum2) { + var enumName1 = value1 is Enum ? enumToName(value1) : value1.toString(); + var enumName2 = value2 is Enum ? enumToName(value2) : value2.toString(); + + return equalsIgnoreAsciiCase(enumName1, enumName2); + } + + return null; + } + + static bool? equalsValuesCollection(Object value1, Object value2) { + var collection1 = TypeParser.isCollectionValue(value1); + var collection2 = TypeParser.isCollectionValue(value2); + + if (collection1 && collection2) { + return isEqualsDeep(value1, value2, valueEquality: equalsValuesBasic); + } + + return null; } FutureOr createFromMap(Map fields); diff --git a/lib/src/bones_api_entity_adapter.dart b/lib/src/bones_api_entity_adapter.dart index 6573fc5..7cd875c 100644 --- a/lib/src/bones_api_entity_adapter.dart +++ b/lib/src/bones_api_entity_adapter.dart @@ -321,6 +321,23 @@ abstract class SQLAdapter extends SchemeProvider /// If `true` indicates that this adapter SQL uses the `ON CONFLICT` syntax for inserts. bool get sqlAcceptsInsertOnConflict; + Object? valueToSQL(Object? value) { + if (value == null) { + return null; + } else if (value is Enum) { + var enumType = value.runtimeType; + var enumReflection = + ReflectionFactory().getRegisterEnumReflection(enumType); + + var name = enumReflection?.getName(value); + name ??= enumToName(value); + + return name; + } else { + return value; + } + } + FutureOr fieldValueToSQL( EncodingContext context, TableScheme tableScheme, @@ -330,7 +347,7 @@ abstract class SQLAdapter extends SchemeProvider var fieldRef = tableScheme.getFieldsReferencedTables(fieldName); if (fieldRef == null) { - fieldsValues.putIfAbsent(fieldName, () => value); + fieldsValues.putIfAbsent(fieldName, () => valueToSQL(value)); var valueSQL = _conditionSQLGenerator.parameterPlaceholder(fieldName); return valueSQL; } else { diff --git a/lib/src/bones_api_utils.dart b/lib/src/bones_api_utils.dart index 38c5b7a..c1e26ce 100644 --- a/lib/src/bones_api_utils.dart +++ b/lib/src/bones_api_utils.dart @@ -1359,6 +1359,25 @@ bool isEqualsMapDeep(Map? m1, Map? m2, {ValueEquality? valueEquality}) { return true; } +/// Returns an [Enum] name. +String enumToName(Enum enumValue) { + var s = enumValue.toString(); + var idx = s.indexOf('.'); + var name = s.substring(idx + 1); + return name; +} + +/// Returns an [Enum] from [enumValues] that matches [name]. +Enum? enumFromName(String name, Iterable enumValues) { + for (var e in enumValues) { + var n = enumToName(e); + if (n == name) { + return e; + } + } + return null; +} + typedef InstanceInfoExtractor = I Function(O o); /// Tracks an instance with a info relationship. diff --git a/pubspec.yaml b/pubspec.yaml index ec1b3e0..c858133 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: bones_api description: Bones_API - A Powerful API backend framework for Dart. Comes with a built-in HTTP Server, routes handler, entity handler, SQL translator, and DB adapters. -version: 1.0.32 +version: 1.0.33 homepage: https://github.com/Colossus-Services/bones_api environment: @@ -14,7 +14,7 @@ dependencies: async_extension: ^1.0.9 dart_spawner: ^1.0.5 args: ^2.2.0 - reflection_factory: ^1.0.17 + reflection_factory: ^1.0.18 petitparser: ^4.2.0 hotreloader: ^3.0.1 logging: ^1.0.1 diff --git a/test/bones_api_entity_test.dart b/test/bones_api_entity_test.dart index f13aa74..4a5fbdf 100644 --- a/test/bones_api_entity_test.dart +++ b/test/bones_api_entity_test.dart @@ -104,15 +104,18 @@ void main() { }); test('basic', () async { - var user1 = User('joe@mail.com', '123', - Address('NY', 'New York', 'Fifth Avenue', 101), [Role('admin')], + var user1 = User( + 'joe@mail.com', + '123', + Address('NY', 'New York', 'Fifth Avenue', 101), + [Role(RoleType.admin)], level: 10, creationTime: DateTime.utc(2020, 10, 11, 12, 13, 14, 0, 0)); var user2 = User( 'smith@mail.com', 'abc', Address('CA', 'Los Angeles', 'Hollywood Boulevard', 404), - [Role('guest')], + [Role(RoleType.guest)], creationTime: DateTime.utc(2021, 10, 11, 12, 13, 14, 0, 0)); var user1Json = @@ -139,14 +142,17 @@ void main() { }); test('basic', () async { - var user1 = User('joe@mail.com', '123', - Address('NY', 'New York', 'Fifth Avenue', 101), [Role('admin')], + var user1 = User( + 'joe@mail.com', + '123', + Address('NY', 'New York', 'Fifth Avenue', 101), + [Role(RoleType.admin)], creationTime: DateTime.utc(2020, 10, 11, 12, 13, 14, 0, 0)); var user2 = User( 'smith@mail.com', 'abc', Address('CA', 'Los Angeles', 'Hollywood Boulevard', 404), - [Role('guest')], + [Role(RoleType.guest)], creationTime: DateTime.utc(2021, 10, 11, 12, 13, 14, 0, 0)); var reflectionEntityHandler = user1.reflection.entityHandler; @@ -164,14 +170,17 @@ void main() { var user1Time = DateTime.utc(2019, 1, 2, 3, 4, 5); var user2Time = DateTime.utc(2019, 12, 2, 3, 4, 5); - var user1 = User('joe@setl.com', '123', - Address('NY', 'New York', 'Fifth Avenue', 101), [Role('admin')], + var user1 = User( + 'joe@setl.com', + '123', + Address('NY', 'New York', 'Fifth Avenue', 101), + [Role(RoleType.admin)], creationTime: user1Time); var user2 = User( 'smith@setl.com', 'abc', Address('CA', 'Los Angeles', 'Hollywood Boulevard', 404), - [Role('guest')], + [Role(RoleType.guest)], creationTime: user2Time); userRepository.store(user1); @@ -291,7 +300,8 @@ void main() { { var address = Address('NY', 'New York', 'street A', 101); - var user = User('joe@memory.com', '123', address, [Role('admin')], + var user = User( + 'joe@memory.com', '123', address, [Role(RoleType.admin)], creationTime: user1Time); var id = await userSQLRepository.store(user); expect(id, equals(1)); @@ -302,7 +312,8 @@ void main() { { var address = Address('CA', 'Los Angeles', 'street B', 201); - var user = User('smith@memory.com', 'abc', address, [Role('guest')], + var user = User( + 'smith@memory.com', 'abc', address, [Role(RoleType.guest)], creationTime: user2Time); var id = await userSQLRepository.store(user); expect(id, equals(2)); @@ -495,7 +506,7 @@ void main() { 'mary@memory.com', 'xyz', address, - [Role('guest')], + [Role(RoleType.guest)], ); var id = await userAPIRepository.store(user); expect(id, equals(3)); @@ -539,7 +550,7 @@ void main() { expect(user.email, equals('mary2@memory.com')); expect(user.address.state, equals('FL')); - user.roles.add(Role('foo')); + user.roles.add(Role(RoleType.unknown)); expect(await userAPIRepository.store(user), equals(user.id)); @@ -549,10 +560,10 @@ void main() { user2.roles.map((e) => e.toJson()), equals([ {'id': 3, 'type': 'guest', 'enabled': true}, - {'id': 4, 'type': 'foo', 'enabled': true} + {'id': 4, 'type': 'unknown', 'enabled': true} ])); - user2.roles.removeWhere((r) => r.type == 'guest'); + user2.roles.removeWhere((r) => r.type == RoleType.guest); expect(await userAPIRepository.store(user2), equals(user.id)); var user3 = await userAPIRepository.selectByID(user.id); @@ -560,7 +571,7 @@ void main() { expect( user3.roles.map((e) => e.toJson()), equals([ - {'id': 4, 'type': 'foo', 'enabled': true} + {'id': 4, 'type': 'unknown', 'enabled': true} ])); } @@ -614,7 +625,7 @@ void main() { 'bill@memory.com', 'txs', address, - [Role('guest')], + [Role(RoleType.guest)], ); var id = await userAPIRepository.store(user); expect(id, equals(4)); diff --git a/test/bones_api_test.reflection.g.dart b/test/bones_api_test.reflection.g.dart index 7b8077c..b251c21 100644 --- a/test/bones_api_test.reflection.g.dart +++ b/test/bones_api_test.reflection.g.dart @@ -1,6 +1,6 @@ // // GENERATED CODE - DO NOT MODIFY BY HAND! -// BUILDER: reflection_factory/1.0.17 +// BUILDER: reflection_factory/1.0.18 // BUILD COMMAND: dart run build_runner build // @@ -32,7 +32,7 @@ class MyInfoModule$reflection extends ClassReflection { Version get languageVersion => Version.parse('2.13.0'); @override - Version get reflectionFactoryVersion => Version.parse('1.0.17'); + Version get reflectionFactoryVersion => Version.parse('1.0.18'); @override MyInfoModule$reflection withObject([MyInfoModule? obj]) => diff --git a/test/bones_api_test_adapter.dart b/test/bones_api_test_adapter.dart index a73592c..0f5aaa2 100644 --- a/test/bones_api_test_adapter.dart +++ b/test/bones_api_test_adapter.dart @@ -253,7 +253,8 @@ void runAdapterTests( { var address = Address('NY', 'New York', 'street A', 101); - var user = User('joe@$testDomain', '123', address, [Role('admin')], + var user = User( + 'joe@$testDomain', '123', address, [Role(RoleType.admin)], level: 100, creationTime: user1Time); var id = await userAPIRepository.store(user); expect(id, equals(1)); @@ -263,7 +264,8 @@ void runAdapterTests( { var address = Address('CA', 'Los Angeles', 'street B', 201); - var user = User('smith@$testDomain', 'abc', address, [Role('guest')], + var user = User( + 'smith@$testDomain', 'abc', address, [Role(RoleType.guest)], creationTime: user2Time); var id = await userAPIRepository.store(user); expect(id, equals(2)); @@ -458,14 +460,14 @@ void runAdapterTests( expect(user!.email, equals('smith4@$testDomain')); - user.roles.add(Role('foo2')); + user.roles.add(Role(RoleType.unknown)); var ok = await userAPIRepository.store(user); expect(ok, equals(user.id)); var rolesJson2 = [ {'id': 2, 'type': 'guest', 'enabled': true}, - {'id': 3, 'type': 'foo2', 'enabled': true} + {'id': 3, 'type': 'unknown', 'enabled': true} ]; expect( user.roles.map( @@ -479,7 +481,7 @@ void runAdapterTests( (e) => entityByReflection ? e.toJsonFromFields() : e.toJson()), equals(rolesJson2)); - user2.roles.removeWhere((r) => r.type == 'guest'); + user2.roles.removeWhere((r) => r.type == RoleType.guest); var ok2 = await userAPIRepository.store(user2); expect(ok2, equals(user.id)); @@ -488,7 +490,7 @@ void runAdapterTests( expect(user3!.email, equals(user.email)); var rolesJson3 = [ - {'id': 3, 'type': 'foo2', 'enabled': true} + {'id': 3, 'type': 'unknown', 'enabled': true} ]; expect( user3.roles.map( diff --git a/test/bones_api_test_entities.dart b/test/bones_api_test_entities.dart index 749f4f2..d63be62 100644 --- a/test/bones_api_test_entities.dart +++ b/test/bones_api_test_entities.dart @@ -336,20 +336,27 @@ class Address extends Entity { }; } +@EnableReflection() +enum RoleType { + admin, + guest, + unknown, +} + @EnableReflection() class Role extends Entity { int? id; - String type; + RoleType type; bool enabled; Role(this.type, {this.id, this.enabled = true}); - Role.empty() : this(''); + Role.empty() : this(RoleType.unknown); Role.fromMap(Map map) - : this(map.getAsString('type')!, + : this(RoleType$from(map.getAsString('type')) ?? RoleType.unknown, enabled: map.getAsBool('enabled', defaultValue: false)!, id: map.getAsInt('id')); @@ -392,7 +399,7 @@ class Role extends Entity { case 'id': return TypeInfo.tInt; case 'type': - return TypeInfo.tString; + return TypeInfo(RoleType); case 'enabled': return TypeInfo.tBool; default: @@ -410,7 +417,7 @@ class Role extends Entity { } case 'type': { - type = value as String; + type = RoleType$from(value)!; break; } case 'enabled': @@ -427,6 +434,6 @@ class Role extends Entity { Map toJson() => { 'enabled': enabled, if (id != null) 'id': id, - 'type': type, + 'type': type.enumName, }; } diff --git a/test/bones_api_test_entities.reflection.g.dart b/test/bones_api_test_entities.reflection.g.dart index a01cf74..5a47274 100644 --- a/test/bones_api_test_entities.reflection.g.dart +++ b/test/bones_api_test_entities.reflection.g.dart @@ -1,6 +1,6 @@ // // GENERATED CODE - DO NOT MODIFY BY HAND! -// BUILDER: reflection_factory/1.0.17 +// BUILDER: reflection_factory/1.0.18 // BUILD COMMAND: dart run build_runner build // @@ -21,6 +21,9 @@ Role Role$fromJson(Map map) => Role Role$fromJsonEncoded(String jsonEncoded) => Role$reflection.staticInstance.fromJsonEncoded(jsonEncoded); // ignore: non_constant_identifier_names +RoleType? RoleType$from(Object? o) => + RoleType$reflection.staticInstance.from(o); +// ignore: non_constant_identifier_names User User$fromJson(Map map) => User$reflection.staticInstance.fromJson(map); // ignore: non_constant_identifier_names @@ -44,7 +47,7 @@ class Address$reflection extends ClassReflection
{ Version get languageVersion => Version.parse('2.13.0'); @override - Version get reflectionFactoryVersion => Version.parse('1.0.17'); + Version get reflectionFactoryVersion => Version.parse('1.0.18'); @override Address$reflection withObject([Address? obj]) => Address$reflection(obj); @@ -451,7 +454,7 @@ class Role$reflection extends ClassReflection { Version get languageVersion => Version.parse('2.13.0'); @override - Version get reflectionFactoryVersion => Version.parse('1.0.17'); + Version get reflectionFactoryVersion => Version.parse('1.0.18'); @override Role$reflection withObject([Role? obj]) => Role$reflection(obj); @@ -491,11 +494,11 @@ class Role$reflection extends ClassReflection { this, Role, '', - () => (String type, {int? id, bool enabled = true}) => + () => (RoleType type, {int? id, bool enabled = true}) => Role(type, id: id, enabled: enabled), const [ ParameterReflection( - TypeReflection.tString, 'type', false, true, null, null) + TypeReflection(RoleType), 'type', false, true, null, null) ], null, const { @@ -583,11 +586,11 @@ class Role$reflection extends ClassReflection { return FieldReflection( this, Role, - TypeReflection.tString, + TypeReflection(RoleType), 'type', false, (o) => () => o!.type as T, - (o) => (T? v) => o!.type = v as String, + (o) => (T? v) => o!.type = v as RoleType, obj, false, false, @@ -806,6 +809,60 @@ class Role$reflection extends ClassReflection { } } +class RoleType$reflection extends EnumReflection { + RoleType$reflection([RoleType? object]) : super(RoleType, object); + + static bool _registered = false; + @override + void register() { + if (!_registered) { + _registered = true; + super.register(); + _registerSiblingsReflection(); + } + } + + @override + Version get languageVersion => Version.parse('2.13.0'); + + @override + Version get reflectionFactoryVersion => Version.parse('1.0.18'); + + @override + RoleType$reflection withObject([RoleType? obj]) => RoleType$reflection(obj); + + static RoleType$reflection? _withoutObjectInstance; + @override + RoleType$reflection withoutObjectInstance() => _withoutObjectInstance ??= + super.withoutObjectInstance() as RoleType$reflection; + + static RoleType$reflection get staticInstance => + _withoutObjectInstance ??= RoleType$reflection(); + + @override + List get classAnnotations => List.unmodifiable([]); + + @override + List siblingsEnumReflection() => + _siblingsReflection().whereType().toList(); + + @override + List siblingsReflection() => _siblingsReflection(); + + @override + List get fieldsNames => const ['admin', 'guest', 'unknown']; + + @override + Map get valuesByName => const { + 'admin': RoleType.admin, + 'guest': RoleType.guest, + 'unknown': RoleType.unknown, + }; + + @override + List get values => RoleType.values; +} + class User$reflection extends ClassReflection { User$reflection([User? object]) : super(User, object); @@ -823,7 +880,7 @@ class User$reflection extends ClassReflection { Version get languageVersion => Version.parse('2.13.0'); @override - Version get reflectionFactoryVersion => Version.parse('1.0.17'); + Version get reflectionFactoryVersion => Version.parse('1.0.18'); @override User$reflection withObject([User? obj]) => User$reflection(obj); @@ -1280,6 +1337,24 @@ extension Role$reflectionExtension on Role { Object? toJsonFromFields() => reflection.toJsonFromFields(); } +extension RoleType$reflectionExtension on RoleType { + /// Returns a [EnumReflection] for type [RoleType]. (Generated by [ReflectionFactory]) + EnumReflection get reflection => RoleType$reflection(this); + + /// Returns the name of the [RoleType] instance. (Generated by [ReflectionFactory]) + String get enumName => RoleType$reflection(this).name()!; + + /// Returns a JSON for type [RoleType]. (Generated by [ReflectionFactory]) + String? toJson() => reflection.toJson(); + + /// Returns a JSON [Map] for type [RoleType]. (Generated by [ReflectionFactory]) + Map? toJsonMap() => reflection.toJsonMap(); + + /// Returns an encoded JSON [String] for type [RoleType]. (Generated by [ReflectionFactory]) + String toJsonEncoded({bool pretty = false}) => + reflection.toJsonEncoded(pretty: pretty); +} + extension User$reflectionExtension on User { /// Returns a [ClassReflection] for type [User]. (Generated by [ReflectionFactory]) ClassReflection get reflection => User$reflection(this); @@ -1295,6 +1370,7 @@ List _listSiblingsReflection() => [ User$reflection(), Address$reflection(), Role$reflection(), + RoleType$reflection(), ]; List? _siblingsReflectionList; diff --git a/test/bones_api_utils_test.dart b/test/bones_api_utils_test.dart index 56d16f1..830d055 100644 --- a/test/bones_api_utils_test.dart +++ b/test/bones_api_utils_test.dart @@ -48,16 +48,37 @@ void main() { }), equals({'a': 1, 'b': 2, 'foo': '51:x'})); - expect( - Json.toJson(Role('foo')), equals({'type': 'foo', 'enabled': true})); + expect(Json.toJson(Role(RoleType.unknown)), + equals({'type': 'unknown', 'enabled': true})); roleEntityHandler.toString(); - expect(Json.toJson(Role('foo'), removeField: (k) => k == 'enabled'), - equals({'id': null, 'type': 'foo'})); + expect( + Json.toJson(Role(RoleType.admin), removeField: (k) => k == 'enabled'), + equals({'id': null, 'type': 'admin'})); - expect(Json.toJson(Role('foo', enabled: false), removeNullFields: true), - equals({'type': 'foo', 'enabled': false})); + expect( + Json.toJson(Role(RoleType.guest, enabled: false), + removeNullFields: true), + equals({'type': 'guest', 'enabled': false})); + }); + + test('Json.fromJson', () async { + Role$reflection.staticInstance; + + { + var json = Json.toJson(Role(RoleType.guest, enabled: false)); + + expect(Json.fromJson(json), + equals(Role(RoleType.guest, enabled: false))); + } + + { + var json = Json.toJson(Role(RoleType.admin)); + + expect(Json.fromJson(json), + equals(Role(RoleType.admin, enabled: true))); + } }); test('Json.encode', () async { @@ -98,6 +119,25 @@ void main() { equals({'ab': AB(1, 2)})); }); + test('Json.decodeFromBytes', () async { + Role$reflection.staticInstance; + + { + var jsonBytes = + Json.encodeToBytes(Role(RoleType.guest, enabled: false)); + + expect(Json.decodeFromBytes(jsonBytes), + equals(Role(RoleType.guest, enabled: false))); + } + + { + var jsonBytes = Json.encodeToBytes(Role(RoleType.admin)); + + expect(Json.decodeFromBytes(jsonBytes), + equals(Role(RoleType.admin, enabled: true))); + } + }); + test('TypeParser.parseInt', () async { expect(TypeParser.parseInt(10), equals(10)); expect(TypeParser.parseInt('11'), equals(11));