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
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;

View file

@ -13,7 +13,7 @@ class BuildContext {
final Map<String, String> aliases = {};
/// 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.
final Map<String, bool> shimmed = {};

View file

@ -69,7 +69,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
// 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<Serializable> {
// 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) {

View file

@ -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);
});

View file

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

View file

@ -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<Book> 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);
}

View file

@ -13,6 +13,7 @@ abstract class AuthorSerializer {
int age,
List<Book> 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,