Serialize lists of models
This commit is contained in:
parent
ea1ee1f9d3
commit
b8ec1571d9
3 changed files with 45 additions and 25 deletions
|
@ -55,8 +55,14 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
|
|||
ClassBuilder clazz, BuildContext ctx, FileBuilder file) {
|
||||
clazz.methods.add(new Method((method) {
|
||||
method
|
||||
..static = true
|
||||
..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 {');
|
||||
int i = 0;
|
||||
|
@ -70,16 +76,30 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
|
|||
|
||||
if (i++ > 0) buf.write(', ');
|
||||
|
||||
String serializedRepresentation = field.name;
|
||||
String serializedRepresentation = 'model.${field.name}';
|
||||
|
||||
// Serialize dates
|
||||
if (dateTimeTypeChecker.isAssignableFromType(field.type))
|
||||
serializedRepresentation = '${field.name}.toIso8601String()';
|
||||
serializedRepresentation = 'model.${field.name}.toIso8601String()';
|
||||
|
||||
// Serialize model classes via `XSerializer.toMap`
|
||||
else if (isModelClass(field.type)) {
|
||||
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");
|
||||
|
|
|
@ -7,26 +7,26 @@ part of angel_serialize.test.models.author;
|
|||
// **************************************************************************
|
||||
|
||||
abstract class AuthorSerializer {
|
||||
Map<String, dynamic> toMap() {
|
||||
static Map<String, dynamic> toMap(Author model) {
|
||||
return {
|
||||
'id': id,
|
||||
'name': name,
|
||||
'age': age,
|
||||
'books': books,
|
||||
'newest_book': BookSerializer.toMap(newestBook),
|
||||
'created_at': createdAt.toIso8601String(),
|
||||
'updated_at': updatedAt.toIso8601String()
|
||||
'id': model.id,
|
||||
'name': model.name,
|
||||
'age': model.age,
|
||||
'books': model.books.map(BookSerializer.toMap).toList(),
|
||||
'newest_book': BookSerializer.toMap(model.newestBook),
|
||||
'created_at': model.createdAt.toIso8601String(),
|
||||
'updated_at': model.updatedAt.toIso8601String()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
abstract class LibrarySerializer {
|
||||
Map<String, dynamic> toMap() {
|
||||
static Map<String, dynamic> toMap(Library model) {
|
||||
return {
|
||||
'id': id,
|
||||
'collection': collection,
|
||||
'created_at': createdAt.toIso8601String(),
|
||||
'updated_at': updatedAt.toIso8601String()
|
||||
'id': model.id,
|
||||
'collection': model.collection,
|
||||
'created_at': model.createdAt.toIso8601String(),
|
||||
'updated_at': model.updatedAt.toIso8601String()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,15 +7,15 @@ part of angel_serialize.test.models.book;
|
|||
// **************************************************************************
|
||||
|
||||
abstract class BookSerializer {
|
||||
Map<String, dynamic> toMap() {
|
||||
static Map<String, dynamic> toMap(Book model) {
|
||||
return {
|
||||
'id': id,
|
||||
'author': author,
|
||||
'title': title,
|
||||
'description': description,
|
||||
'page_count': pageCount,
|
||||
'created_at': createdAt.toIso8601String(),
|
||||
'updated_at': updatedAt.toIso8601String()
|
||||
'id': model.id,
|
||||
'author': model.author,
|
||||
'title': model.title,
|
||||
'description': model.description,
|
||||
'page_count': model.pageCount,
|
||||
'created_at': model.createdAt.toIso8601String(),
|
||||
'updated_at': model.updatedAt.toIso8601String()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue