Updated serialize_generator

This commit is contained in:
thomashii 2021-05-18 19:32:47 +08:00
parent 63bc91c203
commit b7e0d01844
6 changed files with 37 additions and 40 deletions

View file

@ -1,21 +1,12 @@
name: angel_migration_runner name: angel3_migration_runner
version: 4.0.0 version: 4.0.0-beta.1
description: Command-line based database migration runner for Angel's ORM. description: Command-line based database migration runner for Angel's ORM.
homepage: https://github.com/dukefirehawk/angel homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel3_migration_runner
publish_to: none
environment: environment:
sdk: '>=2.12.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'
dependencies: dependencies:
angel_migration: angel3_migration: ^4.0.0-beta.1
git: angel3_orm: ^4.0.0
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/orm/angel_migration
angel_orm:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/orm/angel_orm
args: ^2.1.0 args: ^2.1.0
charcode: ^1.2.0 charcode: ^1.2.0
postgres: ^2.3.2 postgres: ^2.3.2

View file

@ -1,3 +1,6 @@
# 4.0.1
* Resolved static analysis warnings
# 4.0.0 # 4.0.0
* Migrated to support Dart SDK 2.12.x NNBD * Migrated to support Dart SDK 2.12.x NNBD

View file

@ -1,5 +1,5 @@
# angel3_serialize_generator # angel3_serialize_generator
[![version](https://img.shields.io/badge/pub-v4.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_serialize_generator) [![version](https://img.shields.io/badge/pub-v4.0.1-brightgreen)](https://pub.dartlang.org/packages/angel3_serialize_generator)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)

View file

@ -11,7 +11,7 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
} }
var ctx = await buildContext(element as ClassElement, annotation, buildStep, var ctx = await buildContext(element as ClassElement, annotation, buildStep,
await buildStep.resolver, true); buildStep.resolver, true);
var lib = Library((b) { var lib = Library((b) {
generateClass(ctx, b, annotation); generateClass(ctx, b, annotation);
@ -180,7 +180,7 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
} }
var buf = StringBuffer('return ${ctx.modelClassName}('); var buf = StringBuffer('return ${ctx.modelClassName}(');
int i = 0; var i = 0;
for (var param in ctx.constructorParameters) { for (var param in ctx.constructorParameters) {
if (i++ > 0) buf.write(', '); if (i++ > 0) buf.write(', ');
buf.write(param.name); buf.write(param.name);
@ -240,8 +240,7 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
} }
void generateHashCode(BuildContext? ctx, ClassBuilder clazz) { void generateHashCode(BuildContext? ctx, ClassBuilder clazz) {
clazz clazz.methods.add(Method((method) {
..methods.add(Method((method) {
method method
..name = 'hashCode' ..name = 'hashCode'
..type = MethodType.getter ..type = MethodType.getter

View file

@ -154,12 +154,13 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
// Serialize model classes via `XSerializer.toMap` // Serialize model classes via `XSerializer.toMap`
else if (isModelClass(type)) { else if (isModelClass(type)) {
var rc = ReCase(type.name!); var rc = ReCase(type.getDisplayString(withNullability: true));
serializedRepresentation = serializedRepresentation =
'${serializerToMap(rc, 'model.${field.name}')}'; '${serializerToMap(rc, 'model.${field.name}')}';
} else if (type is InterfaceType) { } else if (type is InterfaceType) {
if (isListOfModelType(type)) { if (isListOfModelType(type)) {
var name = type.typeArguments[0].name!; var name =
type.typeArguments[0].getDisplayString(withNullability: true);
if (name.startsWith('_')) name = name.substring(1); if (name.startsWith('_')) name = name.substring(1);
var rc = ReCase(name); var rc = ReCase(name);
var m = serializerToMap(rc, 'm'); var m = serializerToMap(rc, 'm');
@ -168,7 +169,8 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
?.map((m) => $m) ?.map((m) => $m)
?.toList()'''; ?.toList()''';
} else if (isMapToModelType(type)) { } else if (isMapToModelType(type)) {
var rc = ReCase(type.typeArguments[1].name!); var rc = ReCase(
type.typeArguments[1].getDisplayString(withNullability: true));
serializedRepresentation = serializedRepresentation =
'''model.${field.name}.keys?.fold({}, (map, key) { '''model.${field.name}.keys?.fold({}, (map, key) {
return map..[key] = return map..[key] =
@ -178,7 +180,7 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
serializedRepresentation = ''' serializedRepresentation = '''
model.${field.name} == null ? model.${field.name} == null ?
null null
: ${type.name}.values.indexOf(model.${field.name}) : ${type.getDisplayString(withNullability: true)}.values.indexOf(model.${field.name})
'''; ''';
} else if (const TypeChecker.fromRuntime(Uint8List) } else if (const TypeChecker.fromRuntime(Uint8List)
.isAssignableFromType(type)) { .isAssignableFromType(type)) {
@ -276,20 +278,22 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
// Serialize model classes via `XSerializer.toMap` // Serialize model classes via `XSerializer.toMap`
else if (isModelClass(type)) { else if (isModelClass(type)) {
var rc = ReCase(type.name!); var rc = ReCase(type.getDisplayString(withNullability: true));
deserializedRepresentation = "map['$alias'] != null" deserializedRepresentation = "map['$alias'] != null"
" ? ${rc.pascalCase}Serializer.fromMap(map['$alias'] as Map)" " ? ${rc.pascalCase}Serializer.fromMap(map['$alias'] as Map)"
' : $defaultValue'; ' : $defaultValue';
} else if (type is InterfaceType) { } else if (type is InterfaceType) {
if (isListOfModelType(type)) { if (isListOfModelType(type)) {
var rc = ReCase(type.typeArguments[0].name!); var rc = ReCase(
type.typeArguments[0].getDisplayString(withNullability: true));
deserializedRepresentation = "map['$alias'] is Iterable" deserializedRepresentation = "map['$alias'] is Iterable"
" ? List.unmodifiable(((map['$alias'] as Iterable)" " ? List.unmodifiable(((map['$alias'] as Iterable)"
'.whereType<Map>())' '.whereType<Map>())'
'.map(${rc.pascalCase}Serializer.fromMap))' '.map(${rc.pascalCase}Serializer.fromMap))'
' : $defaultValue'; ' : $defaultValue';
} else if (isMapToModelType(type)) { } else if (isMapToModelType(type)) {
var rc = ReCase(type.typeArguments[1].name!); var rc = ReCase(
type.typeArguments[1].getDisplayString(withNullability: true));
deserializedRepresentation = ''' deserializedRepresentation = '''
map['$alias'] is Map map['$alias'] is Map
? Map.unmodifiable((map['$alias'] as Map).keys.fold({}, (out, key) { ? Map.unmodifiable((map['$alias'] as Map).keys.fold({}, (out, key) {
@ -300,12 +304,12 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
'''; ''';
} else if (type.element.isEnum) { } else if (type.element.isEnum) {
deserializedRepresentation = ''' deserializedRepresentation = '''
map['$alias'] is ${type.name} map['$alias'] is ${type.getDisplayString(withNullability: true)}
? (map['$alias'] as ${type.name}) ? (map['$alias'] as ${type.getDisplayString(withNullability: true)})
: :
( (
map['$alias'] is int map['$alias'] is int
? ${type.name}.values[map['$alias'] as int] ? ${type.getDisplayString(withNullability: true)}.values[map['$alias'] as int]
: $defaultValue : $defaultValue
) )
'''; ''';

View file

@ -1,5 +1,5 @@
name: angel3_serialize_generator name: angel3_serialize_generator
version: 4.0.0 version: 4.0.1
description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling. description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling.
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/serialize/angel_serialize_generator homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/serialize/angel_serialize_generator
environment: environment: