diff --git a/.idea/serialize.iml b/.idea/serialize.iml index 17e06bc2..67079d9b 100644 --- a/.idea/serialize.iml +++ b/.idea/serialize.iml @@ -2,10 +2,12 @@ + + diff --git a/angel_serialize_generator/CHANGELOG.md b/angel_serialize_generator/CHANGELOG.md index a1cd823b..dd455c0f 100644 --- a/angel_serialize_generator/CHANGELOG.md +++ b/angel_serialize_generator/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.8 +* Generate a `fromMap` with typecasting, for Dart 2's sake. + # 2.0.7 * Create unmodifiable Lists and Maps. * Support `@required` on fields. diff --git a/angel_serialize_generator/lib/angel_serialize_generator.dart b/angel_serialize_generator/lib/angel_serialize_generator.dart index d7e32214..486fd071 100644 --- a/angel_serialize_generator/lib/angel_serialize_generator.dart +++ b/angel_serialize_generator/lib/angel_serialize_generator.dart @@ -79,3 +79,16 @@ bool isMapToModelType(InterfaceType t) { t.typeArguments.length == 2 && isModelClass(t.typeArguments[1]); } + +/// Compute a [String] representation of a [type]. +String typeToString(DartType type) { + if (type is InterfaceType) { + if (type.typeArguments.isEmpty) return type.name; + return type.name + + '<' + + type.typeArguments.map(typeToString).join(', ') + + '>'; + } else { + return type.name; + } +} diff --git a/angel_serialize_generator/lib/serialize.dart b/angel_serialize_generator/lib/serialize.dart index b8be7f82..cbd1b229 100644 --- a/angel_serialize_generator/lib/serialize.dart +++ b/angel_serialize_generator/lib/serialize.dart @@ -172,12 +172,13 @@ class SerializerGenerator extends GeneratorForAnnotation { if (i++ > 0) buf.write(', '); - String deserializedRepresentation = "map['$alias']"; + String deserializedRepresentation = "map['$alias'] as ${typeToString( + field.type)}"; // Deserialize dates if (dateTimeTypeChecker.isAssignableFromType(field.type)) deserializedRepresentation = "map['$alias'] != null ? " - "(map['$alias'] is DateTime ? map['$alias'] : DateTime.parse(map['$alias']))" + "(map['$alias'] is DateTime ? (map['$alias'] as DateTime) : DateTime.parse(map['$alias']))" " : null"; // Serialize model classes via `XSerializer.toMap` @@ -192,16 +193,16 @@ class SerializerGenerator extends GeneratorForAnnotation { if (isListModelType(t)) { var rc = new ReCase(t.typeArguments[0].name); deserializedRepresentation = "map['$alias'] is Iterable" - " ? new List.unmodifiable(map['$alias'].map(${rc + " ? new List.unmodifiable((map['$alias'] as Iterable).whereType().map(${rc .pascalCase}Serializer.fromMap))" " : null"; } else if (isMapToModelType(t)) { var rc = new ReCase(t.typeArguments[1].name); deserializedRepresentation = ''' map['$alias'] is Map - ? new Map.unmodifiable(map['$alias'].keys.fold({}, (out, key) { + ? new Map.unmodifiable((map['$alias'] as Map).keys.fold({}, (out, key) { return out..[key] = ${rc - .pascalCase}Serializer.fromMap(map['$alias'][key]); + .pascalCase}Serializer.fromMap((map['$alias'] as Map)[key]); })) : null '''; diff --git a/angel_serialize_generator/pubspec.yaml b/angel_serialize_generator/pubspec.yaml index c386eab7..e337d180 100644 --- a/angel_serialize_generator/pubspec.yaml +++ b/angel_serialize_generator/pubspec.yaml @@ -1,11 +1,12 @@ name: angel_serialize_generator -version: 2.0.7 +version: 2.0.8 description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling. author: Tobe O homepage: https://github.com/angel-dart/serialize environment: - sdk: '>=2.0.0-dev.1.2 <2.0.0' + sdk: '>=2.0.0-dev.1.2 <3.0.0' dependencies: + angel_model: ^1.0.0 angel_serialize: ^2.0.0-alpha build_config: ">=0.2.4 <2.0.0" code_buffer: ^1.0.0 @@ -15,6 +16,5 @@ dependencies: recase: ^1.0.0 source_gen: ^0.7.0 dev_dependencies: - angel_framework: ^1.0.0 - build_runner: ^0.7.0 - test: ">= 0.12.13 < 0.13.0" \ No newline at end of file + build_runner: ^0.9.0 + test: ^1.0.0 \ No newline at end of file diff --git a/angel_serialize_generator/test/models/author.dart b/angel_serialize_generator/test/models/author.dart index 8949c405..c3b7f437 100644 --- a/angel_serialize_generator/test/models/author.dart +++ b/angel_serialize_generator/test/models/author.dart @@ -1,6 +1,6 @@ library angel_serialize.test.models.author; -import 'package:angel_framework/common.dart'; +import 'package:angel_model/angel_model.dart'; import 'package:angel_serialize/angel_serialize.dart'; import 'package:collection/collection.dart'; import 'package:meta/meta.dart'; diff --git a/angel_serialize_generator/test/models/book.dart b/angel_serialize_generator/test/models/book.dart index a79b07cd..b130ee8d 100644 --- a/angel_serialize_generator/test/models/book.dart +++ b/angel_serialize_generator/test/models/book.dart @@ -1,6 +1,6 @@ library angel_serialize.test.models.book; -import 'package:angel_framework/common.dart'; +import 'package:angel_model/angel_model.dart'; import 'package:angel_serialize/angel_serialize.dart'; import 'package:collection/collection.dart'; part 'book.g.dart';