Serialize lists of models

This commit is contained in:
Tobe O 2018-02-27 20:41:19 -05:00
parent ea1ee1f9d3
commit b8ec1571d9
3 changed files with 45 additions and 25 deletions

View file

@ -55,8 +55,14 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
ClassBuilder clazz, BuildContext ctx, FileBuilder file) { ClassBuilder clazz, BuildContext ctx, FileBuilder file) {
clazz.methods.add(new Method((method) { clazz.methods.add(new Method((method) {
method method
..static = true
..name = 'toMap' ..name = 'toMap'
..returns = new Reference('Map<String, dynamic>'); ..returns = new Reference('Map<String, dynamic>')
..requiredParameters.add(new Parameter((b) {
b
..name = 'model'
..type = ctx.modelClassType;
}));
var buf = new StringBuffer('return {'); var buf = new StringBuffer('return {');
int i = 0; int i = 0;
@ -70,16 +76,30 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
if (i++ > 0) buf.write(', '); if (i++ > 0) buf.write(', ');
String serializedRepresentation = field.name; String serializedRepresentation = 'model.${field.name}';
// Serialize dates // Serialize dates
if (dateTimeTypeChecker.isAssignableFromType(field.type)) if (dateTimeTypeChecker.isAssignableFromType(field.type))
serializedRepresentation = '${field.name}.toIso8601String()'; serializedRepresentation = 'model.${field.name}.toIso8601String()';
// Serialize model classes via `XSerializer.toMap` // Serialize model classes via `XSerializer.toMap`
else if (isModelClass(field.type)) { else if (isModelClass(field.type)) {
var rc = new ReCase(field.type.name); var rc = new ReCase(field.type.name);
serializedRepresentation = '${rc.pascalCase}Serializer.toMap(${field.name})'; serializedRepresentation =
'${rc.pascalCase}Serializer.toMap(model.${field.name})';
}
else if (field.type is InterfaceType) {
var t = field.type as InterfaceType;
if (t.name == 'List' && t.typeArguments.length == 1) {
var rc = new ReCase(t.typeArguments[0].name);
serializedRepresentation = 'model.${field.name}.map(${rc.pascalCase}Serializer.toMap).toList()';
}
else if (t.name == 'List' && t.typeArguments.length == 2 && isModelClass(t.typeArguments[1])) {
// TODO: Serialize maps
}
} }
buf.write("'$alias': $serializedRepresentation"); buf.write("'$alias': $serializedRepresentation");

View file

@ -7,26 +7,26 @@ part of angel_serialize.test.models.author;
// ************************************************************************** // **************************************************************************
abstract class AuthorSerializer { abstract class AuthorSerializer {
Map<String, dynamic> toMap() { static Map<String, dynamic> toMap(Author model) {
return { return {
'id': id, 'id': model.id,
'name': name, 'name': model.name,
'age': age, 'age': model.age,
'books': books, 'books': model.books.map(BookSerializer.toMap).toList(),
'newest_book': BookSerializer.toMap(newestBook), 'newest_book': BookSerializer.toMap(model.newestBook),
'created_at': createdAt.toIso8601String(), 'created_at': model.createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String() 'updated_at': model.updatedAt.toIso8601String()
}; };
} }
} }
abstract class LibrarySerializer { abstract class LibrarySerializer {
Map<String, dynamic> toMap() { static Map<String, dynamic> toMap(Library model) {
return { return {
'id': id, 'id': model.id,
'collection': collection, 'collection': model.collection,
'created_at': createdAt.toIso8601String(), 'created_at': model.createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String() 'updated_at': model.updatedAt.toIso8601String()
}; };
} }
} }

View file

@ -7,15 +7,15 @@ part of angel_serialize.test.models.book;
// ************************************************************************** // **************************************************************************
abstract class BookSerializer { abstract class BookSerializer {
Map<String, dynamic> toMap() { static Map<String, dynamic> toMap(Book model) {
return { return {
'id': id, 'id': model.id,
'author': author, 'author': model.author,
'title': title, 'title': model.title,
'description': description, 'description': model.description,
'page_count': pageCount, 'page_count': model.pageCount,
'created_at': createdAt.toIso8601String(), 'created_at': model.createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String() 'updated_at': model.updatedAt.toIso8601String()
}; };
} }
} }