Fixed lists

This commit is contained in:
Tobe O 2018-03-05 01:18:46 -05:00
parent 9ccc406aa5
commit 779dc080be
8 changed files with 30 additions and 3 deletions

View file

@ -0,0 +1,3 @@
# 2.0.1
* Ensured that `List` is only transformed if
it generically references a `Model`.

View file

@ -53,6 +53,14 @@ bool isModelClass(DartType t) {
}
}
/// Determines if a [DartType] is a `List` with the first type argument being a `Model`.
bool isListModelType(InterfaceType t) {
return t.name == 'List' &&
t.typeArguments.length == 1 &&
isModelClass(t.typeArguments[0]);
}
/// Determines if a [DartType] is a `Map` with the second type argument being a `Model`.
bool isMapToModelType(InterfaceType t) {
return t.name == 'Map' &&

View file

@ -89,7 +89,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
} else if (field.type is InterfaceType) {
var t = field.type as InterfaceType;
if (t.name == 'List' && t.typeArguments.length == 1) {
if (isListModelType(t)) {
var rc = new ReCase(t.typeArguments[0].name);
serializedRepresentation = 'model.${field.name}?.map(${rc
.pascalCase}Serializer.toMap)?.toList()';
@ -157,7 +157,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
} else if (field.type is InterfaceType) {
var t = field.type as InterfaceType;
if (t.name == 'List' && t.typeArguments.length == 1) {
if (isListModelType(t)) {
var rc = new ReCase(t.typeArguments[0].name);
deserializedRepresentation = "map['$alias'] is Iterable"
" ? map['$alias'].map(${rc

View file

@ -1,5 +1,5 @@
name: angel_serialize_generator
version: 2.0.0
version: 2.0.1
description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/serialize

View file

@ -11,6 +11,7 @@ main() {
title: 'Harry Potter and the Deathly Hallows',
description: 'The 7th book.',
pageCount: 759,
notModels: [1.0, 3.0],
updatedAt: new DateTime.now());
var serializedDeathlyHallows = deathlyHallows.toJson();
print('Deathly Hallows: $serializedDeathlyHallows');
@ -46,6 +47,10 @@ main() {
expect(serializedDeathlyHallows.keys, isNot(contains('pageCount')));
});
test('standard list', () {
expect(serializedDeathlyHallows['not_models'], deathlyHallows.notModels);
});
test('heeds @exclude', () {
expect(serializedJkRowling.keys, isNot(contains('secret')));
});
@ -81,6 +86,7 @@ main() {
expect(book.author, deathlyHallows.author);
expect(book.description, deathlyHallows.description);
expect(book.pageCount, deathlyHallows.pageCount);
expect(book.notModels, deathlyHallows.notModels);
expect(book.createdAt, isNull);
expect(book.updatedAt, deathlyHallows.updatedAt);
});

View file

@ -9,4 +9,5 @@ part 'book.serializer.g.dart';
abstract class _Book extends Model {
String author, title, description;
int pageCount;
List<double> notModels;
}

View file

@ -13,6 +13,7 @@ class Book extends _Book {
this.title,
this.description,
this.pageCount,
this.notModels,
this.createdAt,
this.updatedAt});
@ -31,6 +32,9 @@ class Book extends _Book {
@override
final int pageCount;
@override
final List<double> notModels;
@override
final DateTime createdAt;
@ -43,6 +47,7 @@ class Book extends _Book {
String title,
String description,
int pageCount,
List<double> notModels,
DateTime createdAt,
DateTime updatedAt}) {
return new Book(
@ -51,6 +56,7 @@ class Book extends _Book {
title: title ?? this.title,
description: description ?? this.description,
pageCount: pageCount ?? this.pageCount,
notModels: notModels ?? this.notModels,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt);
}

View file

@ -13,6 +13,7 @@ abstract class BookSerializer {
String title,
String description,
int pageCount,
List<double> notModels,
DateTime createdAt,
DateTime updatedAt}) {
return new Book(
@ -21,6 +22,7 @@ abstract class BookSerializer {
title: map['title'],
description: map['description'],
pageCount: map['page_count'],
notModels: map['not_models'],
createdAt: map['created_at'] != null
? DateTime.parse(map['created_at'])
: null,
@ -36,6 +38,7 @@ abstract class BookSerializer {
'title': model.title,
'description': model.description,
'page_count': model.pageCount,
'not_models': model.notModels,
'created_at': model.createdAt?.toIso8601String(),
'updated_at': model.updatedAt?.toIso8601String()
};