Serialize nested model class

This commit is contained in:
Tobe O 2018-02-27 20:20:42 -05:00
parent 236ef55230
commit ea1ee1f9d3
3 changed files with 6 additions and 2 deletions

View file

@ -6,6 +6,7 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel_serialize/angel_serialize.dart';
import 'package:build/build.dart'; import 'package:build/build.dart';
import 'package:code_builder/code_builder.dart'; import 'package:code_builder/code_builder.dart';
import 'package:recase/recase.dart';
import 'package:source_gen/source_gen.dart' hide LibraryBuilder; import 'package:source_gen/source_gen.dart' hide LibraryBuilder;
import 'build_context.dart'; import 'build_context.dart';
import 'context.dart'; import 'context.dart';
@ -24,6 +25,8 @@ TypeReference convertTypeReference(DartType t) {
} }
bool isModelClass(DartType t) { bool isModelClass(DartType t) {
if (t == null) return false;
if (serializableTypeChecker.hasAnnotationOf(t.element)) if (serializableTypeChecker.hasAnnotationOf(t.element))
return true; return true;

View file

@ -78,7 +78,8 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
// Serialize model classes via `XSerializer.toMap` // Serialize model classes via `XSerializer.toMap`
else if (isModelClass(field.type)) { else if (isModelClass(field.type)) {
var rc = new ReCase(field.type.name);
serializedRepresentation = '${rc.pascalCase}Serializer.toMap(${field.name})';
} }
buf.write("'$alias': $serializedRepresentation"); buf.write("'$alias': $serializedRepresentation");

View file

@ -13,7 +13,7 @@ abstract class AuthorSerializer {
'name': name, 'name': name,
'age': age, 'age': age,
'books': books, 'books': books,
'newest_book': newestBook, 'newest_book': BookSerializer.toMap(newestBook),
'created_at': createdAt.toIso8601String(), 'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String() 'updated_at': updatedAt.toIso8601String()
}; };