From 6aaa9321953a011f88d50f41bbcf735b42d248a8 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 2 Mar 2018 13:38:41 -0500 Subject: [PATCH] Add canSerialize, deserialize to Exclude --- angel_serialize_generator/lib/build_context.dart | 9 ++++++++- angel_serialize_generator/lib/context.dart | 2 +- angel_serialize_generator/lib/serialize.dart | 4 ++-- angel_serialize_generator/test/book_test.dart | 10 ++++++++++ angel_serialize_generator/test/models/author.dart | 4 ++++ angel_serialize_generator/test/models/author.g.dart | 6 ++++++ .../test/models/author.serializer.g.dart | 2 ++ 7 files changed, 33 insertions(+), 4 deletions(-) diff --git a/angel_serialize_generator/lib/build_context.dart b/angel_serialize_generator/lib/build_context.dart index caa972ad..0c8ce626 100644 --- a/angel_serialize_generator/lib/build_context.dart +++ b/angel_serialize_generator/lib/build_context.dart @@ -47,7 +47,14 @@ Future buildContext( // Skip if annotated with @exclude var excludeAnnotation = excludeTypeChecker.firstAnnotationOf(field); - ctx.excluded[field.name] = excludeAnnotation != null; + if (excludeAnnotation != null) { + var cr = new ConstantReader(excludeAnnotation); + + ctx.excluded[field.name] = new Exclude( + canSerialize: cr.read('canSerialize').boolValue, + canDeserialize: cr.read('canDeserialize').boolValue, + ); + } // Check for alias Alias alias; diff --git a/angel_serialize_generator/lib/context.dart b/angel_serialize_generator/lib/context.dart index 6c1aab1f..24011ecb 100644 --- a/angel_serialize_generator/lib/context.dart +++ b/angel_serialize_generator/lib/context.dart @@ -13,7 +13,7 @@ class BuildContext { final Map aliases = {}; /// A map of fields that have been marked as to be excluded from serialization. - final Map excluded = {}; + final Map excluded = {}; /// A map of "synthetic" fields, i.e. `id` and `created_at` injected automatically. final Map shimmed = {}; diff --git a/angel_serialize_generator/lib/serialize.dart b/angel_serialize_generator/lib/serialize.dart index 17f2e000..f1779484 100644 --- a/angel_serialize_generator/lib/serialize.dart +++ b/angel_serialize_generator/lib/serialize.dart @@ -69,7 +69,7 @@ class SerializerGenerator extends GeneratorForAnnotation { // Add named parameters for (var field in ctx.fields) { // Skip excluded fields - if (ctx.excluded[field.name] == true) continue; + if (ctx.excluded[field.name]?.canSerialize == false) continue; var alias = ctx.resolveFieldName(field.name); @@ -129,7 +129,7 @@ class SerializerGenerator extends GeneratorForAnnotation { // Add named parameters for (var field in ctx.fields) { - if (ctx.excluded[field.name] == true) continue; + if (ctx.excluded[field.name]?.canDeserialize == false) continue; var alias = ctx.resolveFieldName(field.name); method.optionalParameters.add(new Parameter((b) { diff --git a/angel_serialize_generator/test/book_test.dart b/angel_serialize_generator/test/book_test.dart index f8d19a84..5a15d081 100644 --- a/angel_serialize_generator/test/book_test.dart +++ b/angel_serialize_generator/test/book_test.dart @@ -50,6 +50,16 @@ main() { expect(serializedJkRowling.keys, isNot(contains('secret'))); }); + test('heeds canDeserialize', () { + var map = new Map.from(serializedJkRowling)..['obscured'] = 'foo'; + var author = AuthorSerializer.fromMap(map); + expect(author.obscured, 'foo'); + }); + + test('heeds canSerialize', () { + expect(serializedJkRowling.keys, isNot(contains('obscured'))); + }); + test('nested @serializable class is serialized', () { expect(serializedJkRowling['newest_book'], deathlyHallowsMap); }); diff --git a/angel_serialize_generator/test/models/author.dart b/angel_serialize_generator/test/models/author.dart index 72984516..95bcf089 100644 --- a/angel_serialize_generator/test/models/author.dart +++ b/angel_serialize_generator/test/models/author.dart @@ -12,8 +12,12 @@ abstract class _Author extends Model { int age; List books; Book newestBook; + @exclude String secret; + + @Exclude(canDeserialize: true) + String obscured; } @serializable diff --git a/angel_serialize_generator/test/models/author.g.dart b/angel_serialize_generator/test/models/author.g.dart index 228cf6a8..263e7bf1 100644 --- a/angel_serialize_generator/test/models/author.g.dart +++ b/angel_serialize_generator/test/models/author.g.dart @@ -14,6 +14,7 @@ class Author extends _Author { this.books, this.newestBook, this.secret, + this.obscured, this.createdAt, this.updatedAt}); @@ -35,6 +36,9 @@ class Author extends _Author { @override final String secret; + @override + final String obscured; + @override final DateTime createdAt; @@ -48,6 +52,7 @@ class Author extends _Author { List books, Book newestBook, String secret, + String obscured, DateTime createdAt, DateTime updatedAt}) { return new Author( @@ -57,6 +62,7 @@ class Author extends _Author { books: books ?? this.books, newestBook: newestBook ?? this.newestBook, secret: secret ?? this.secret, + obscured: obscured ?? this.obscured, createdAt: createdAt ?? this.createdAt, updatedAt: updatedAt ?? this.updatedAt); } diff --git a/angel_serialize_generator/test/models/author.serializer.g.dart b/angel_serialize_generator/test/models/author.serializer.g.dart index 6ac9bf7f..de267d3b 100644 --- a/angel_serialize_generator/test/models/author.serializer.g.dart +++ b/angel_serialize_generator/test/models/author.serializer.g.dart @@ -13,6 +13,7 @@ abstract class AuthorSerializer { int age, List books, Book newestBook, + String obscured, DateTime createdAt, DateTime updatedAt}) { return new Author( @@ -25,6 +26,7 @@ abstract class AuthorSerializer { newestBook: map['newest_book'] != null ? BookSerializer.fromMap(map['newest_book']) : null, + obscured: map['obscured'], createdAt: map['created_at'] != null ? DateTime.parse(map['created_at']) : null,