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
version: 4.0.0
name: angel3_migration_runner
version: 4.0.0-beta.1
description: Command-line based database migration runner for Angel's ORM.
homepage: https://github.com/dukefirehawk/angel
publish_to: none
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel3_migration_runner
environment:
sdk: '>=2.12.0 <3.0.0'
dependencies:
angel_migration:
git:
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
angel3_migration: ^4.0.0-beta.1
angel3_orm: ^4.0.0
args: ^2.1.0
charcode: ^1.2.0
postgres: ^2.3.2

View file

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

View file

@ -1,5 +1,5 @@
# 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)
[![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,
await buildStep.resolver, true);
buildStep.resolver, true);
var lib = Library((b) {
generateClass(ctx, b, annotation);
@ -180,7 +180,7 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
}
var buf = StringBuffer('return ${ctx.modelClassName}(');
int i = 0;
var i = 0;
for (var param in ctx.constructorParameters) {
if (i++ > 0) buf.write(', ');
buf.write(param.name);
@ -240,18 +240,17 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
}
void generateHashCode(BuildContext? ctx, ClassBuilder clazz) {
clazz
..methods.add(Method((method) {
method
..name = 'hashCode'
..type = MethodType.getter
..returns = refer('int')
..annotations.add(refer('override'))
..body = refer('hashObjects')
.call([literalList(ctx!.fields.map((f) => refer(f.name)))])
.returned
.statement;
}));
clazz.methods.add(Method((method) {
method
..name = 'hashCode'
..type = MethodType.getter
..returns = refer('int')
..annotations.add(refer('override'))
..body = refer('hashObjects')
.call([literalList(ctx!.fields.map((f) => refer(f.name)))])
.returned
.statement;
}));
}
void generateToString(BuildContext? ctx, ClassBuilder clazz) {

View file

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

View file

@ -1,5 +1,5 @@
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.
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/serialize/angel_serialize_generator
environment: