From ea1ee1f9d3f501734d1fdf7e029c1a5b73efa839 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Tue, 27 Feb 2018 20:20:42 -0500 Subject: [PATCH] Serialize nested model class --- angel_serialize_generator/lib/angel_serialize_generator.dart | 3 +++ angel_serialize_generator/lib/serialize.dart | 3 ++- angel_serialize_generator/test/models/author.serializer.g.dart | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/angel_serialize_generator/lib/angel_serialize_generator.dart b/angel_serialize_generator/lib/angel_serialize_generator.dart index 2c42d9c3..11d26be0 100644 --- a/angel_serialize_generator/lib/angel_serialize_generator.dart +++ b/angel_serialize_generator/lib/angel_serialize_generator.dart @@ -6,6 +6,7 @@ import 'package:analyzer/dart/element/type.dart'; import 'package:angel_serialize/angel_serialize.dart'; import 'package:build/build.dart'; import 'package:code_builder/code_builder.dart'; +import 'package:recase/recase.dart'; import 'package:source_gen/source_gen.dart' hide LibraryBuilder; import 'build_context.dart'; import 'context.dart'; @@ -24,6 +25,8 @@ TypeReference convertTypeReference(DartType t) { } bool isModelClass(DartType t) { + if (t == null) return false; + if (serializableTypeChecker.hasAnnotationOf(t.element)) return true; diff --git a/angel_serialize_generator/lib/serialize.dart b/angel_serialize_generator/lib/serialize.dart index fa4b4695..781cc051 100644 --- a/angel_serialize_generator/lib/serialize.dart +++ b/angel_serialize_generator/lib/serialize.dart @@ -78,7 +78,8 @@ class SerializerGenerator extends GeneratorForAnnotation { // 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})'; } 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 7a4945ae..2a3ebcf0 100644 --- a/angel_serialize_generator/test/models/author.serializer.g.dart +++ b/angel_serialize_generator/test/models/author.serializer.g.dart @@ -13,7 +13,7 @@ abstract class AuthorSerializer { 'name': name, 'age': age, 'books': books, - 'newest_book': newestBook, + 'newest_book': BookSerializer.toMap(newestBook), 'created_at': createdAt.toIso8601String(), 'updated_at': updatedAt.toIso8601String() };