From 69426c738f5d1ba57d1777d90e216e4a5e4672c4 Mon Sep 17 00:00:00 2001 From: David Morgan Date: Sun, 11 Sep 2016 13:05:39 +0200 Subject: [PATCH 1/2] Fix issue with BuiltMap deserialization. Add test. --- CHANGELOG.md | 4 + built_json/pubspec.yaml | 2 +- .../lib/src/source_field.dart | 2 +- built_json_generator/pubspec.yaml | 6 +- example/lib/collections.dart | 40 ++++++ example/lib/collections.g.dart | 133 ++++++++++++++++++ example/lib/has_int.g.dart | 7 +- example/lib/serializers.dart | 1 + example/lib/serializers.g.dart | 10 ++ example/lib/test_enum.g.dart | 7 +- example/pubspec.yaml | 10 +- example/test/collections_test.dart | 34 +++++ 12 files changed, 245 insertions(+), 11 deletions(-) create mode 100644 example/lib/collections.dart create mode 100644 example/lib/collections.g.dart create mode 100644 example/test/collections_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bce344..723c87c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.1.2 + +- Fix issue with BuiltMap deserialization. + ## 0.1.1 - Fix error in pubspec.yaml. diff --git a/built_json/pubspec.yaml b/built_json/pubspec.yaml index 52976a1..7ef9054 100644 --- a/built_json/pubspec.yaml +++ b/built_json/pubspec.yaml @@ -1,5 +1,5 @@ name: built_json -version: 0.1.1 +version: 0.1.2 description: > JSON serialization for Built Collections, Built Values and Enum Classes. This library is the runtime dependency. diff --git a/built_json_generator/lib/src/source_field.dart b/built_json_generator/lib/src/source_field.dart index b9133e7..0ac6a53 100644 --- a/built_json_generator/lib/src/source_field.dart +++ b/built_json_generator/lib/src/source_field.dart @@ -14,7 +14,7 @@ abstract class SourceField implements Built { static final BuiltMap typesWithBuilder = new BuiltMap({ 'BuiltList': 'ListBuilder', - 'BuiltMap': 'MapBuiler', + 'BuiltMap': 'MapBuilder', 'BuiltSet': 'SetBuilder', }); diff --git a/built_json_generator/pubspec.yaml b/built_json_generator/pubspec.yaml index 9fa1c13..e8fccf6 100644 --- a/built_json_generator/pubspec.yaml +++ b/built_json_generator/pubspec.yaml @@ -1,5 +1,5 @@ name: built_json_generator -version: 0.1.1 +version: 0.1.2 description: > JSON serialization for Built Collections, Built Values and Enum Classes. This library is the dev dependency. @@ -14,7 +14,9 @@ dependencies: analyzer: '>=0.27.1 <0.28.0' build: '^0.3.0' built_collection: '^1.0.1' - built_json: '^0.1.0' + #built_json: '^0.1.2' + built_json: + path: ../built_json source_gen: '>=0.5.0 <0.6.0' quiver: '>=0.21.0 <0.22.0' diff --git a/example/lib/collections.dart b/example/lib/collections.dart new file mode 100644 index 0000000..5c23629 --- /dev/null +++ b/example/lib/collections.dart @@ -0,0 +1,40 @@ +// Copyright (c) 2016, Google Inc. Please see the AUTHORS file for details. +// All rights reserved. Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +library collections; + +import 'package:built_collection/built_collection.dart'; +import 'package:built_json/built_json.dart'; +import 'package:built_value/built_value.dart'; + +part 'collections.g.dart'; + +/// Example built_value type containing collections. +abstract class Collections + implements Built { + /// Example of how to make a built_value type serializable. + /// + /// Declare a static final [Serializers] field called `serializer`. + /// The built_json code generator will provide the implementation. You need to + /// do this for every type you want to serialize. + static final Serializer serializer = _$collectionsSerializer; + + BuiltList get list; + BuiltSet get set; + BuiltMap get map; + + Collections._(); + factory Collections([updates(CollectionsBuilder b)]) = _$Collections; +} + +/// Builder class for [Collections]. +abstract class CollectionsBuilder + implements Builder { + ListBuilder list = new ListBuilder(); + SetBuilder set = new SetBuilder(); + MapBuilder map = new MapBuilder(); + + CollectionsBuilder._(); + factory CollectionsBuilder() = _$CollectionsBuilder; +} diff --git a/example/lib/collections.g.dart b/example/lib/collections.g.dart new file mode 100644 index 0000000..fa7e3d2 --- /dev/null +++ b/example/lib/collections.g.dart @@ -0,0 +1,133 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of collections; + +// ************************************************************************** +// Generator: BuiltJsonGenerator +// Target: library collections +// ************************************************************************** + +Serializer _$collectionsSerializer = new _$CollectionsSerializer(); + +class _$CollectionsSerializer implements StructuredSerializer { + final Iterable types = + new BuiltList([Collections, _$Collections]); + final String wireName = 'Collections'; + + @override + Iterable serialize(Serializers serializers, Collections object, + {FullType specifiedType: FullType.unspecified}) { + final result = [ + 'list', + serializers.serialize(object.list, + specifiedType: + const FullType(BuiltList, const [const FullType(String)])), + 'set', + serializers.serialize(object.set, + specifiedType: const FullType(BuiltSet, const [const FullType(int)])), + 'map', + serializers.serialize(object.map, + specifiedType: const FullType( + BuiltMap, const [const FullType(String), const FullType(int)])), + ]; + + return result; + } + + @override + Collections deserialize(Serializers serializers, Iterable serialized, + {FullType specifiedType: FullType.unspecified}) { + final result = new CollectionsBuilder(); + + var key; + var value; + var expectingKey = true; + for (final item in serialized) { + if (expectingKey) { + key = item; + expectingKey = false; + } else { + value = item; + expectingKey = true; + + switch (key as String) { + case 'list': + result.list.replace(serializers.deserialize(value, + specifiedType: + const FullType(BuiltList, const [const FullType(String)]))); + break; + case 'set': + result.set.replace(serializers.deserialize(value, + specifiedType: + const FullType(BuiltSet, const [const FullType(int)]))); + break; + case 'map': + result.map.replace(serializers.deserialize(value, + specifiedType: const FullType(BuiltMap, + const [const FullType(String), const FullType(int)]))); + break; + } + } + } + + return result.build(); + } +} + +// ************************************************************************** +// Generator: BuiltValueGenerator +// Target: abstract class Collections +// ************************************************************************** + +class _$Collections extends Collections { + final BuiltList list; + final BuiltSet set; + final BuiltMap map; + _$Collections._({this.list, this.set, this.map}) : super._() { + if (list == null) throw new ArgumentError('null list'); + if (set == null) throw new ArgumentError('null set'); + if (map == null) throw new ArgumentError('null map'); + } + factory _$Collections([updates(CollectionsBuilder b)]) => + (new CollectionsBuilder()..update(updates)).build(); + Collections rebuild(updates(CollectionsBuilder b)) => + (toBuilder()..update(updates)).build(); + _$CollectionsBuilder toBuilder() => new _$CollectionsBuilder()..replace(this); + bool operator ==(other) { + if (other is! Collections) return false; + return list == other.list && set == other.set && map == other.map; + } + + int get hashCode { + return hashObjects([list, set, map]); + } + + String toString() { + return 'Collections {' + 'list=${list.toString()}\n' + 'set=${set.toString()}\n' + 'map=${map.toString()}\n' + '}'; + } +} + +class _$CollectionsBuilder extends CollectionsBuilder { + _$CollectionsBuilder() : super._(); + void replace(Collections other) { + super.list = other.list?.toBuilder(); + super.set = other.set?.toBuilder(); + super.map = other.map?.toBuilder(); + } + + void update(updates(CollectionsBuilder b)) { + if (updates != null) updates(this); + } + + Collections build() { + if (list == null) throw new ArgumentError('null list'); + if (set == null) throw new ArgumentError('null set'); + if (map == null) throw new ArgumentError('null map'); + return new _$Collections._( + list: list?.build(), set: set?.build(), map: map?.build()); + } +} diff --git a/example/lib/has_int.g.dart b/example/lib/has_int.g.dart index ee4ed87..2049058 100644 --- a/example/lib/has_int.g.dart +++ b/example/lib/has_int.g.dart @@ -154,5 +154,8 @@ EnumWithInt _$valueOf(String name) { } } -final BuiltSet _$values = - new BuiltSet(const [_$one, _$two, _$three,]); +final BuiltSet _$values = new BuiltSet(const [ + _$one, + _$two, + _$three, +]); diff --git a/example/lib/serializers.dart b/example/lib/serializers.dart index b73c839..2642a7f 100644 --- a/example/lib/serializers.dart +++ b/example/lib/serializers.dart @@ -6,6 +6,7 @@ library serializers; import 'package:built_collection/built_collection.dart'; import 'package:built_json/built_json.dart'; +import 'package:example/collections.dart'; import 'package:example/compound_value.dart'; import 'package:example/has_int.dart'; import 'package:example/test_enum.dart'; diff --git a/example/lib/serializers.g.dart b/example/lib/serializers.g.dart index 1d33c3e..191c6ad 100644 --- a/example/lib/serializers.g.dart +++ b/example/lib/serializers.g.dart @@ -12,6 +12,16 @@ Serializers _$serializers = (new Serializers().toBuilder() ..add(TestEnum.serializer) ..add(ValueWithInt.serializer) ..add(EnumWithInt.serializer) + ..add(Collections.serializer) + ..addBuilderFactory( + const FullType(BuiltList, const [const FullType(String)]), + () => new ListBuilder()) + ..addBuilderFactory(const FullType(BuiltSet, const [const FullType(int)]), + () => new SetBuilder()) + ..addBuilderFactory( + const FullType( + BuiltMap, const [const FullType(String), const FullType(int)]), + () => new MapBuilder()) ..add(Value.serializer) ..addBuilderFactory( const FullType(BuiltList, const [const FullType(int)]), diff --git a/example/lib/test_enum.g.dart b/example/lib/test_enum.g.dart index d249714..6ff77b9 100644 --- a/example/lib/test_enum.g.dart +++ b/example/lib/test_enum.g.dart @@ -48,5 +48,8 @@ TestEnum _$valueOf(String name) { } } -final BuiltSet _$values = - new BuiltSet(const [_$yes, _$no, _$maybe,]); +final BuiltSet _$values = new BuiltSet(const [ + _$yes, + _$no, + _$maybe, +]); diff --git a/example/pubspec.yaml b/example/pubspec.yaml index a94a45e..bbc3e95 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,5 +1,5 @@ name: example -version: 0.1.0 +version: 0.1.2 description: > Just an example, not for publishing. authors: @@ -11,12 +11,16 @@ environment: dependencies: built_collection: '^1.0.0' - built_json: '^0.1.0' +# built_json: '^0.1.2' + built_json: + path: ../built_json built_value: '^0.1.0' enum_class: '^0.1.0' dev_dependencies: - built_json_generator: '^0.1.0' +# built_json_generator: '^0.1.2' + built_json_generator: + path: ../built_json_generator built_value_generator: '^0.1.0' enum_class_generator: '^0.1.0' test: any diff --git a/example/test/collections_test.dart b/example/test/collections_test.dart new file mode 100644 index 0000000..dc3ef6f --- /dev/null +++ b/example/test/collections_test.dart @@ -0,0 +1,34 @@ +// Copyright (c) 2016, Google Inc. Please see the AUTHORS file for details. + +// All rights reserved. Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +import 'package:example/collections.dart'; +import 'package:example/serializers.dart'; +import 'package:test/test.dart'; + +void main() { + group('Collections', () { + final data = new Collections((b) => b + ..list.add('one') + ..set.add(2) + ..map['three'] = 4); + final serialized = [ + 'Collections', + 'list', + ['one'], + 'set', + [2], + 'map', + ['three', 4], + ]; + + test('can be serialized', () { + expect(serializers.serialize(data), serialized); + }); + + test('can be deserialized', () { + expect(serializers.deserialize(serialized), data); + }); + }); +} From 7f7ae86c39cbb6d3cd55e11e0bca99e45ee50685 Mon Sep 17 00:00:00 2001 From: David Morgan Date: Mon, 12 Sep 2016 17:50:44 +0200 Subject: [PATCH 2/2] Update pubspecs. --- built_json_generator/pubspec.yaml | 4 +--- example/pubspec.yaml | 8 ++------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/built_json_generator/pubspec.yaml b/built_json_generator/pubspec.yaml index e8fccf6..92e480b 100644 --- a/built_json_generator/pubspec.yaml +++ b/built_json_generator/pubspec.yaml @@ -14,9 +14,7 @@ dependencies: analyzer: '>=0.27.1 <0.28.0' build: '^0.3.0' built_collection: '^1.0.1' - #built_json: '^0.1.2' - built_json: - path: ../built_json + built_json: '^0.1.2' source_gen: '>=0.5.0 <0.6.0' quiver: '>=0.21.0 <0.22.0' diff --git a/example/pubspec.yaml b/example/pubspec.yaml index bbc3e95..3e3f36f 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -11,16 +11,12 @@ environment: dependencies: built_collection: '^1.0.0' -# built_json: '^0.1.2' - built_json: - path: ../built_json + built_json: '^0.1.2' built_value: '^0.1.0' enum_class: '^0.1.0' dev_dependencies: -# built_json_generator: '^0.1.2' - built_json_generator: - path: ../built_json_generator + built_json_generator: '^0.1.2' built_value_generator: '^0.1.0' enum_class_generator: '^0.1.0' test: any