diff --git a/angel_serialize_generator/lib/serialize.dart b/angel_serialize_generator/lib/serialize.dart index 781cc051..ad66bdb1 100644 --- a/angel_serialize_generator/lib/serialize.dart +++ b/angel_serialize_generator/lib/serialize.dart @@ -55,8 +55,14 @@ class SerializerGenerator extends GeneratorForAnnotation { ClassBuilder clazz, BuildContext ctx, FileBuilder file) { clazz.methods.add(new Method((method) { method + ..static = true ..name = 'toMap' - ..returns = new Reference('Map'); + ..returns = new Reference('Map') + ..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 { 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"); diff --git a/angel_serialize_generator/test/models/author.serializer.g.dart b/angel_serialize_generator/test/models/author.serializer.g.dart index 2a3ebcf0..e90f462e 100644 --- a/angel_serialize_generator/test/models/author.serializer.g.dart +++ b/angel_serialize_generator/test/models/author.serializer.g.dart @@ -7,26 +7,26 @@ part of angel_serialize.test.models.author; // ************************************************************************** abstract class AuthorSerializer { - Map toMap() { + static Map 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 toMap() { + static Map 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() }; } } diff --git a/angel_serialize_generator/test/models/book.serializer.g.dart b/angel_serialize_generator/test/models/book.serializer.g.dart index d6e83f55..830254b2 100644 --- a/angel_serialize_generator/test/models/book.serializer.g.dart +++ b/angel_serialize_generator/test/models/book.serializer.g.dart @@ -7,15 +7,15 @@ part of angel_serialize.test.models.book; // ************************************************************************** abstract class BookSerializer { - Map toMap() { + static Map 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() }; } }