This commit is contained in:
Tobe O 2018-06-27 21:58:42 -04:00
parent 7768b906b7
commit 82268579e4
6 changed files with 35 additions and 5 deletions

View file

@ -1,3 +1,7 @@
# 2.0.8+3
* Don't fail on `null` in `toMap`.
* Support self-referencing via `toJson()`.
# 2.0.8+2
* Better discern when custom methods disqualify classes
from `const` protection.

View file

@ -89,6 +89,14 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
String serializedRepresentation = 'model.${field.name}';
String serializerToMap(ReCase rc, String value) {
if (rc.pascalCase == ctx.modelClassName) {
return '($value)?.toJson()';
}
return '${rc.pascalCase}Serializer.toMap($value)';
}
// Serialize dates
if (dateTimeTypeChecker.isAssignableFromType(field.type))
serializedRepresentation = 'model.${field.name}?.toIso8601String()';
@ -97,7 +105,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
else if (isModelClass(field.type)) {
var rc = new ReCase(field.type.name);
serializedRepresentation =
'${rc.pascalCase}Serializer.toMap(model.${field.name})';
'${serializerToMap(rc, 'model.${field.name}')}';
} else if (field.type is InterfaceType) {
var t = field.type as InterfaceType;
@ -109,8 +117,8 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
var rc = new ReCase(t.typeArguments[1].name);
serializedRepresentation =
'''model.${field.name}.keys?.fold({}, (map, key) {
return map..[key] = ${rc.pascalCase}Serializer.toMap(model.${field
.name}[key]);
return map..[key] = ${serializerToMap(
rc, 'model.${field.name}[key]')};
})''';
} else if (t.element.isEnum) {
serializedRepresentation = '''
@ -125,7 +133,10 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
}
buf.write('};');
method.body = new Code(buf.toString());
method.body = new Block.of([
new Code('if (model == null) { return null; }'),
new Code(buf.toString()),
]);
}));
}

View file

@ -1,5 +1,5 @@
name: angel_serialize_generator
version: 2.0.8+2
version: 2.0.8+3
description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/serialize

View file

@ -42,6 +42,9 @@ abstract class AuthorSerializer {
}
static Map<String, dynamic> toMap(Author model) {
if (model == null) {
return null;
}
if (model.name == null) {
throw new FormatException("Missing required field 'name' on Author.");
}
@ -107,6 +110,9 @@ abstract class LibrarySerializer {
}
static Map<String, dynamic> toMap(Library model) {
if (model == null) {
return null;
}
return {
'id': model.id,
'collection': model.collection.keys?.fold({}, (map, key) {
@ -152,6 +158,9 @@ abstract class BookmarkSerializer {
}
static Map<String, dynamic> toMap(Bookmark model) {
if (model == null) {
return null;
}
if (model.page == null) {
throw new FormatException("Missing required field 'page' on Bookmark.");
}

View file

@ -29,6 +29,9 @@ abstract class BookSerializer {
}
static Map<String, dynamic> toMap(Book model) {
if (model == null) {
return null;
}
return {
'id': model.id,
'author': model.author,

View file

@ -16,6 +16,9 @@ abstract class WithEnumSerializer {
}
static Map<String, dynamic> toMap(WithEnum model) {
if (model == null) {
return null;
}
return {
'type':
model.type == null ? null : WithEnumType.values.indexOf(model.type),