Fixed lists
This commit is contained in:
parent
9ccc406aa5
commit
779dc080be
8 changed files with 30 additions and 3 deletions
3
angel_serialize_generator/CHANGELOG.md
Normal file
3
angel_serialize_generator/CHANGELOG.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# 2.0.1
|
||||||
|
* Ensured that `List` is only transformed if
|
||||||
|
it generically references a `Model`.
|
|
@ -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`.
|
/// Determines if a [DartType] is a `Map` with the second type argument being a `Model`.
|
||||||
bool isMapToModelType(InterfaceType t) {
|
bool isMapToModelType(InterfaceType t) {
|
||||||
return t.name == 'Map' &&
|
return t.name == 'Map' &&
|
||||||
|
|
|
@ -89,7 +89,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
} else if (field.type is InterfaceType) {
|
} else if (field.type is InterfaceType) {
|
||||||
var t = field.type as 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);
|
var rc = new ReCase(t.typeArguments[0].name);
|
||||||
serializedRepresentation = 'model.${field.name}?.map(${rc
|
serializedRepresentation = 'model.${field.name}?.map(${rc
|
||||||
.pascalCase}Serializer.toMap)?.toList()';
|
.pascalCase}Serializer.toMap)?.toList()';
|
||||||
|
@ -157,7 +157,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
} else if (field.type is InterfaceType) {
|
} else if (field.type is InterfaceType) {
|
||||||
var t = field.type as 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);
|
var rc = new ReCase(t.typeArguments[0].name);
|
||||||
deserializedRepresentation = "map['$alias'] is Iterable"
|
deserializedRepresentation = "map['$alias'] is Iterable"
|
||||||
" ? map['$alias'].map(${rc
|
" ? map['$alias'].map(${rc
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel_serialize_generator
|
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.
|
description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling.
|
||||||
author: Tobe O <thosakwe@gmail.com>
|
author: Tobe O <thosakwe@gmail.com>
|
||||||
homepage: https://github.com/angel-dart/serialize
|
homepage: https://github.com/angel-dart/serialize
|
||||||
|
|
|
@ -11,6 +11,7 @@ main() {
|
||||||
title: 'Harry Potter and the Deathly Hallows',
|
title: 'Harry Potter and the Deathly Hallows',
|
||||||
description: 'The 7th book.',
|
description: 'The 7th book.',
|
||||||
pageCount: 759,
|
pageCount: 759,
|
||||||
|
notModels: [1.0, 3.0],
|
||||||
updatedAt: new DateTime.now());
|
updatedAt: new DateTime.now());
|
||||||
var serializedDeathlyHallows = deathlyHallows.toJson();
|
var serializedDeathlyHallows = deathlyHallows.toJson();
|
||||||
print('Deathly Hallows: $serializedDeathlyHallows');
|
print('Deathly Hallows: $serializedDeathlyHallows');
|
||||||
|
@ -46,6 +47,10 @@ main() {
|
||||||
expect(serializedDeathlyHallows.keys, isNot(contains('pageCount')));
|
expect(serializedDeathlyHallows.keys, isNot(contains('pageCount')));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('standard list', () {
|
||||||
|
expect(serializedDeathlyHallows['not_models'], deathlyHallows.notModels);
|
||||||
|
});
|
||||||
|
|
||||||
test('heeds @exclude', () {
|
test('heeds @exclude', () {
|
||||||
expect(serializedJkRowling.keys, isNot(contains('secret')));
|
expect(serializedJkRowling.keys, isNot(contains('secret')));
|
||||||
});
|
});
|
||||||
|
@ -81,6 +86,7 @@ main() {
|
||||||
expect(book.author, deathlyHallows.author);
|
expect(book.author, deathlyHallows.author);
|
||||||
expect(book.description, deathlyHallows.description);
|
expect(book.description, deathlyHallows.description);
|
||||||
expect(book.pageCount, deathlyHallows.pageCount);
|
expect(book.pageCount, deathlyHallows.pageCount);
|
||||||
|
expect(book.notModels, deathlyHallows.notModels);
|
||||||
expect(book.createdAt, isNull);
|
expect(book.createdAt, isNull);
|
||||||
expect(book.updatedAt, deathlyHallows.updatedAt);
|
expect(book.updatedAt, deathlyHallows.updatedAt);
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,4 +9,5 @@ part 'book.serializer.g.dart';
|
||||||
abstract class _Book extends Model {
|
abstract class _Book extends Model {
|
||||||
String author, title, description;
|
String author, title, description;
|
||||||
int pageCount;
|
int pageCount;
|
||||||
|
List<double> notModels;
|
||||||
}
|
}
|
|
@ -13,6 +13,7 @@ class Book extends _Book {
|
||||||
this.title,
|
this.title,
|
||||||
this.description,
|
this.description,
|
||||||
this.pageCount,
|
this.pageCount,
|
||||||
|
this.notModels,
|
||||||
this.createdAt,
|
this.createdAt,
|
||||||
this.updatedAt});
|
this.updatedAt});
|
||||||
|
|
||||||
|
@ -31,6 +32,9 @@ class Book extends _Book {
|
||||||
@override
|
@override
|
||||||
final int pageCount;
|
final int pageCount;
|
||||||
|
|
||||||
|
@override
|
||||||
|
final List<double> notModels;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
final DateTime createdAt;
|
final DateTime createdAt;
|
||||||
|
|
||||||
|
@ -43,6 +47,7 @@ class Book extends _Book {
|
||||||
String title,
|
String title,
|
||||||
String description,
|
String description,
|
||||||
int pageCount,
|
int pageCount,
|
||||||
|
List<double> notModels,
|
||||||
DateTime createdAt,
|
DateTime createdAt,
|
||||||
DateTime updatedAt}) {
|
DateTime updatedAt}) {
|
||||||
return new Book(
|
return new Book(
|
||||||
|
@ -51,6 +56,7 @@ class Book extends _Book {
|
||||||
title: title ?? this.title,
|
title: title ?? this.title,
|
||||||
description: description ?? this.description,
|
description: description ?? this.description,
|
||||||
pageCount: pageCount ?? this.pageCount,
|
pageCount: pageCount ?? this.pageCount,
|
||||||
|
notModels: notModels ?? this.notModels,
|
||||||
createdAt: createdAt ?? this.createdAt,
|
createdAt: createdAt ?? this.createdAt,
|
||||||
updatedAt: updatedAt ?? this.updatedAt);
|
updatedAt: updatedAt ?? this.updatedAt);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ abstract class BookSerializer {
|
||||||
String title,
|
String title,
|
||||||
String description,
|
String description,
|
||||||
int pageCount,
|
int pageCount,
|
||||||
|
List<double> notModels,
|
||||||
DateTime createdAt,
|
DateTime createdAt,
|
||||||
DateTime updatedAt}) {
|
DateTime updatedAt}) {
|
||||||
return new Book(
|
return new Book(
|
||||||
|
@ -21,6 +22,7 @@ abstract class BookSerializer {
|
||||||
title: map['title'],
|
title: map['title'],
|
||||||
description: map['description'],
|
description: map['description'],
|
||||||
pageCount: map['page_count'],
|
pageCount: map['page_count'],
|
||||||
|
notModels: map['not_models'],
|
||||||
createdAt: map['created_at'] != null
|
createdAt: map['created_at'] != null
|
||||||
? DateTime.parse(map['created_at'])
|
? DateTime.parse(map['created_at'])
|
||||||
: null,
|
: null,
|
||||||
|
@ -36,6 +38,7 @@ abstract class BookSerializer {
|
||||||
'title': model.title,
|
'title': model.title,
|
||||||
'description': model.description,
|
'description': model.description,
|
||||||
'page_count': model.pageCount,
|
'page_count': model.pageCount,
|
||||||
|
'not_models': model.notModels,
|
||||||
'created_at': model.createdAt?.toIso8601String(),
|
'created_at': model.createdAt?.toIso8601String(),
|
||||||
'updated_at': model.updatedAt?.toIso8601String()
|
'updated_at': model.updatedAt?.toIso8601String()
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue