Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit a989958

Browse files
committed
Fix serialization when built_value builder is fully generated.
1 parent 37c5781 commit a989958

File tree

15 files changed

+144
-136
lines changed

15 files changed

+144
-136
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.1.3
4+
5+
- Fix serialization when built_value builder is fully generated.
6+
37
## 0.1.2
48

59
- Fix issue with BuiltMap deserialization.

built_json/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: built_json
2-
version: 0.1.2
2+
version: 0.1.3
33
description: >
44
JSON serialization for Built Collections, Built Values and Enum Classes.
55
This library is the runtime dependency.

built_json_generator/lib/src/source_field.dart

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@
55
library built_json_generator.source_field;
66

77
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/type.dart';
89
import 'package:built_collection/built_collection.dart';
910
import 'package:built_value/built_value.dart';
1011

1112
part 'source_field.g.dart';
1213

14+
BuiltSet<String> _builtCollectionNames = new BuiltSet<String>([
15+
'BuiltList',
16+
'BuiltListMultimap',
17+
'BuiltMap',
18+
'BuiltSet',
19+
'BuiltSetMultimap',
20+
]);
21+
1322
abstract class SourceField implements Built<SourceField, SourceFieldBuilder> {
1423
static final BuiltMap<String, String> typesWithBuilder =
1524
new BuiltMap<String, String>({
@@ -43,8 +52,13 @@ abstract class SourceField implements Built<SourceField, SourceFieldBuilder> {
4352
(metadata) => metadata.constantValue.toStringValue() == 'nullable');
4453
result.name = fieldElement.displayName;
4554
result.type = fieldElement.getter.returnType.displayName;
46-
result.builderFieldUsesNestedBuilder = builderFieldElement != null &&
47-
fieldElement.getter.returnType.displayName !=
55+
56+
// If the builder is present, check it to determine whether a nested
57+
// builder is needed. Otherwise, use the same logic as built_value when
58+
// it decides whether to use a nested builder.
59+
result.builderFieldUsesNestedBuilder = builderFieldElement == null
60+
? _needsNestedBuilder(fieldElement.getter.returnType)
61+
: fieldElement.getter.returnType.displayName !=
4862
builderFieldElement.getter.returnType.displayName;
4963
}
5064

@@ -87,6 +101,25 @@ abstract class SourceField implements Built<SourceField, SourceFieldBuilder> {
87101
.substring(genericsStart + 1)
88102
.substring(0, name.length - genericsStart - 2);
89103
}
104+
105+
// These three methods are copied from built_value to match the behaviour
106+
// when a builder is not explicitly defined.
107+
// TODO(davidmorgan): dedupe.
108+
static bool _needsNestedBuilder(DartType type) {
109+
return _isBuiltValue(type) || _isBuiltCollection(type);
110+
}
111+
112+
static bool _isBuiltValue(DartType type) {
113+
if (type.element is! ClassElement) return false;
114+
return (type.element as ClassElement)
115+
.allSupertypes
116+
.any((interfaceType) => interfaceType.name == 'Built');
117+
}
118+
119+
static bool _isBuiltCollection(DartType type) {
120+
return _builtCollectionNames
121+
.any((name) => type.displayName.startsWith('${name}<'));
122+
}
90123
}
91124

92125
abstract class SourceFieldBuilder

built_json_generator/pubspec.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: built_json_generator
2-
version: 0.1.2
2+
version: 0.1.3
33
description: >
44
JSON serialization for Built Collections, Built Values and Enum Classes.
55
This library is the dev dependency.
@@ -14,7 +14,9 @@ dependencies:
1414
analyzer: '>=0.27.1 <0.28.0'
1515
build: '^0.3.0'
1616
built_collection: '^1.0.1'
17-
built_json: '^0.1.2'
17+
#built_json: '^0.1.3'
18+
built_json:
19+
path: ../built_json
1820
source_gen: '>=0.5.0 <0.6.0'
1921
quiver: '>=0.21.0 <0.22.0'
2022

example/lib/collections.g.dart

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/compound_value.dart

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,3 @@ abstract class CompoundValue
2929
CompoundValue._();
3030
factory CompoundValue([updates(CompoundValueBuilder b)]) = _$CompoundValue;
3131
}
32-
33-
/// Builder class for [CompoundValue].
34-
abstract class CompoundValueBuilder
35-
implements Builder<CompoundValue, CompoundValueBuilder> {
36-
ValueBuilder aValue = new ValueBuilder();
37-
HasInt aHasInt;
38-
TestEnum aTestEnum;
39-
40-
CompoundValueBuilder._();
41-
factory CompoundValueBuilder() = _$CompoundValueBuilder;
42-
}

example/lib/compound_value.g.dart

Lines changed: 19 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/has_int.g.dart

Lines changed: 35 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/test_enum.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/value.dart

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,8 @@ abstract class Value implements Built<Value, ValueBuilder>, HasInt {
2525
String get aString;
2626
@nullable String get anotherString;
2727
@nullable Object get anObject;
28-
int get aDefaultInt;
2928
BuiltList<int> get listOfInt;
3029

31-
int get youCanWriteDerivedGetters => anInt + aDefaultInt;
32-
3330
Value._();
3431
factory Value([updates(ValueBuilder b)]) = _$Value;
3532
}
36-
37-
/// Builder class for [Value].
38-
abstract class ValueBuilder implements Builder<Value, ValueBuilder> {
39-
int anInt;
40-
String aString;
41-
@nullable String anotherString;
42-
@nullable Object anObject;
43-
int aDefaultInt = 7;
44-
ListBuilder<int> listOfInt = new ListBuilder<int>();
45-
46-
ValueBuilder._();
47-
factory ValueBuilder() = _$ValueBuilder;
48-
49-
set youCanWriteExtraSetters(int value) {
50-
anInt = value;
51-
aDefaultInt = value;
52-
}
53-
}

0 commit comments

Comments
 (0)