diff --git a/angel_serialize_generator/lib/build_context.dart b/angel_serialize_generator/lib/build_context.dart index 49f17d73..caa972ad 100644 --- a/angel_serialize_generator/lib/build_context.dart +++ b/angel_serialize_generator/lib/build_context.dart @@ -11,6 +11,8 @@ import 'context.dart'; const TypeChecker aliasTypeChecker = const TypeChecker.fromRuntime(Alias); +const TypeChecker dateTimeTypeChecker = const TypeChecker.fromRuntime(DateTime); + const TypeChecker excludeTypeChecker = const TypeChecker.fromRuntime(Exclude); const TypeChecker serializableTypeChecker = diff --git a/angel_serialize_generator/lib/serialize.dart b/angel_serialize_generator/lib/serialize.dart index e02d4f95..35c6632c 100644 --- a/angel_serialize_generator/lib/serialize.dart +++ b/angel_serialize_generator/lib/serialize.dart @@ -24,7 +24,7 @@ class SerializerGenerator extends GeneratorForAnnotation { } var lib = new File((b) { - generateClass(ctx, b); + generateClass(serializers.map((s) => s.toIntValue()).toList(), ctx, b); }); var buf = lib.accept(new DartEmitter()); @@ -32,11 +32,54 @@ class SerializerGenerator extends GeneratorForAnnotation { } /// Generate a serializer class. - void generateClass(BuildContext ctx, FileBuilder file) { + void generateClass( + List serializers, BuildContext ctx, FileBuilder file) { file.body.add(new Class((clazz) { clazz ..name = '${ctx.modelClassNameRecase.pascalCase}Serializer' ..abstract = true; + + if (serializers.contains(Serializers.map)) { + // TODO: Generate fromMap + } + + if (serializers.contains(Serializers.map) || + serializers.contains(Serializers.json)) { + generateToMapMethod(clazz, ctx, file); + // TODO: Generate toJson + } + })); + } + + void generateToMapMethod( + ClassBuilder clazz, BuildContext ctx, FileBuilder file) { + clazz.methods.add(new Method((method) { + method + ..name = 'toMap' + ..returns = new Reference('Map'); + + var buf = new StringBuffer('return {'); + int i = 0; + + // Add named parameters + for (var field in ctx.fields) { + // Skip excluded fields + if (ctx.excluded.containsKey(field.name)) continue; + + var alias = ctx.resolveFieldName(field.name); + + if (i++ > 0) buf.write(', '); + + String serializedRepresentation = field.name; + + if (dateTimeTypeChecker.isAssignableFromType(field.type)) + serializedRepresentation = '${field.name}.toIso8601String()'; + + buf.write("'$alias': $serializedRepresentation"); + } + + buf.write('};'); + method.body = new Code(buf.toString()); })); } } diff --git a/angel_serialize_generator/test/models/author.dart b/angel_serialize_generator/test/models/author.dart index 9a6e1716..72984516 100644 --- a/angel_serialize_generator/test/models/author.dart +++ b/angel_serialize_generator/test/models/author.dart @@ -3,8 +3,8 @@ library angel_serialize.test.models.author; import 'package:angel_framework/common.dart'; import 'package:angel_serialize/angel_serialize.dart'; import 'book.dart'; - part 'author.g.dart'; +part 'author.serializer.g.dart'; @serializable abstract class _Author extends Model { diff --git a/angel_serialize_generator/test/models/author.serializer.g.dart b/angel_serialize_generator/test/models/author.serializer.g.dart index cda3d0a7..8f0d0015 100644 --- a/angel_serialize_generator/test/models/author.serializer.g.dart +++ b/angel_serialize_generator/test/models/author.serializer.g.dart @@ -6,6 +6,22 @@ part of angel_serialize.test.models.author; // Generator: SerializerGenerator // ************************************************************************** -abstract class AuthorSerializer {} +abstract class AuthorSerializer { + Map toMap() { + return { + 'id': id, + 'created_at': createdAt.toIso8601String(), + 'updated_at': updatedAt.toIso8601String() + }; + } +} -abstract class LibrarySerializer {} +abstract class LibrarySerializer { + Map toMap() { + return { + 'id': id, + 'created_at': createdAt.toIso8601String(), + 'updated_at': updatedAt.toIso8601String() + }; + } +} diff --git a/angel_serialize_generator/test/models/book.dart b/angel_serialize_generator/test/models/book.dart index 11b66345..bc938f72 100644 --- a/angel_serialize_generator/test/models/book.dart +++ b/angel_serialize_generator/test/models/book.dart @@ -3,6 +3,7 @@ library angel_serialize.test.models.book; import 'package:angel_framework/common.dart'; import 'package:angel_serialize/angel_serialize.dart'; part 'book.g.dart'; +part 'book.serializer.g.dart'; @serializable abstract class _Book extends Model { diff --git a/angel_serialize_generator/test/models/book.serializer.g.dart b/angel_serialize_generator/test/models/book.serializer.g.dart index 013834df..1e069c7e 100644 --- a/angel_serialize_generator/test/models/book.serializer.g.dart +++ b/angel_serialize_generator/test/models/book.serializer.g.dart @@ -6,4 +6,12 @@ part of angel_serialize.test.models.book; // Generator: SerializerGenerator // ************************************************************************** -class BookSerializer extends _Book {} +abstract class BookSerializer { + Map toMap() { + return { + 'id': id, + 'created_at': createdAt.toIso8601String(), + 'updated_at': updatedAt.toIso8601String() + }; + } +}