Add canSerialize, deserialize to Exclude

This commit is contained in:
Tobe O 2018-03-02 13:38:41 -05:00
parent eb8d6c1328
commit 6aaa932195
7 changed files with 33 additions and 4 deletions

View file

@ -47,7 +47,14 @@ Future<BuildContext> buildContext(
// Skip if annotated with @exclude // Skip if annotated with @exclude
var excludeAnnotation = excludeTypeChecker.firstAnnotationOf(field); 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 // Check for alias
Alias alias; Alias alias;

View file

@ -13,7 +13,7 @@ class BuildContext {
final Map<String, String> aliases = {}; final Map<String, String> aliases = {};
/// A map of fields that have been marked as to be excluded from serialization. /// A map of fields that have been marked as to be excluded from serialization.
final Map<String, bool> excluded = {}; final Map<String, Exclude> excluded = {};
/// A map of "synthetic" fields, i.e. `id` and `created_at` injected automatically. /// A map of "synthetic" fields, i.e. `id` and `created_at` injected automatically.
final Map<String, bool> shimmed = {}; final Map<String, bool> shimmed = {};

View file

@ -69,7 +69,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
// Add named parameters // Add named parameters
for (var field in ctx.fields) { for (var field in ctx.fields) {
// Skip excluded 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); var alias = ctx.resolveFieldName(field.name);
@ -129,7 +129,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
// Add named parameters // Add named parameters
for (var field in ctx.fields) { 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); var alias = ctx.resolveFieldName(field.name);
method.optionalParameters.add(new Parameter((b) { method.optionalParameters.add(new Parameter((b) {

View file

@ -50,6 +50,16 @@ main() {
expect(serializedJkRowling.keys, isNot(contains('secret'))); 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', () { test('nested @serializable class is serialized', () {
expect(serializedJkRowling['newest_book'], deathlyHallowsMap); expect(serializedJkRowling['newest_book'], deathlyHallowsMap);
}); });

View file

@ -12,8 +12,12 @@ abstract class _Author extends Model {
int age; int age;
List<Book> books; List<Book> books;
Book newestBook; Book newestBook;
@exclude @exclude
String secret; String secret;
@Exclude(canDeserialize: true)
String obscured;
} }
@serializable @serializable

View file

@ -14,6 +14,7 @@ class Author extends _Author {
this.books, this.books,
this.newestBook, this.newestBook,
this.secret, this.secret,
this.obscured,
this.createdAt, this.createdAt,
this.updatedAt}); this.updatedAt});
@ -35,6 +36,9 @@ class Author extends _Author {
@override @override
final String secret; final String secret;
@override
final String obscured;
@override @override
final DateTime createdAt; final DateTime createdAt;
@ -48,6 +52,7 @@ class Author extends _Author {
List<Book> books, List<Book> books,
Book newestBook, Book newestBook,
String secret, String secret,
String obscured,
DateTime createdAt, DateTime createdAt,
DateTime updatedAt}) { DateTime updatedAt}) {
return new Author( return new Author(
@ -57,6 +62,7 @@ class Author extends _Author {
books: books ?? this.books, books: books ?? this.books,
newestBook: newestBook ?? this.newestBook, newestBook: newestBook ?? this.newestBook,
secret: secret ?? this.secret, secret: secret ?? this.secret,
obscured: obscured ?? this.obscured,
createdAt: createdAt ?? this.createdAt, createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt); updatedAt: updatedAt ?? this.updatedAt);
} }

View file

@ -13,6 +13,7 @@ abstract class AuthorSerializer {
int age, int age,
List<Book> books, List<Book> books,
Book newestBook, Book newestBook,
String obscured,
DateTime createdAt, DateTime createdAt,
DateTime updatedAt}) { DateTime updatedAt}) {
return new Author( return new Author(
@ -25,6 +26,7 @@ abstract class AuthorSerializer {
newestBook: map['newest_book'] != null newestBook: map['newest_book'] != null
? BookSerializer.fromMap(map['newest_book']) ? BookSerializer.fromMap(map['newest_book'])
: null, : null,
obscured: map['obscured'],
createdAt: map['created_at'] != null createdAt: map['created_at'] != null
? DateTime.parse(map['created_at']) ? DateTime.parse(map['created_at'])
: null, : null,