Serialize maps of models

This commit is contained in:
Tobe O 2018-02-27 20:43:43 -05:00
parent b8ec1571d9
commit 92a6ea8c30
2 changed files with 8 additions and 3 deletions

View file

@ -97,8 +97,11 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
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
else if (t.name == 'Map' && t.typeArguments.length == 2 && isModelClass(t.typeArguments[1])) {
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]);
})''';
}
}

View file

@ -24,7 +24,9 @@ abstract class LibrarySerializer {
static Map<String, dynamic> toMap(Library model) {
return {
'id': model.id,
'collection': model.collection,
'collection': model.collection.keys.fold({}, (map, key) {
return map..[key] = BookSerializer.toMap(model.collection[key]);
}),
'created_at': model.createdAt.toIso8601String(),
'updated_at': model.updatedAt.toIso8601String()
};