Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.1.3

- Fix serialization when built_value builder is fully generated.

## 0.1.2

- Fix issue with BuiltMap deserialization.
Expand Down
2 changes: 1 addition & 1 deletion built_json/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: built_json
version: 0.1.2
version: 0.1.3
description: >
JSON serialization for Built Collections, Built Values and Enum Classes.
This library is the runtime dependency.
Expand Down
37 changes: 35 additions & 2 deletions built_json_generator/lib/src/source_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
library built_json_generator.source_field;

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';

part 'source_field.g.dart';

BuiltSet<String> _builtCollectionNames = new BuiltSet<String>([
'BuiltList',
'BuiltListMultimap',
'BuiltMap',
'BuiltSet',
'BuiltSetMultimap',
]);

abstract class SourceField implements Built<SourceField, SourceFieldBuilder> {
static final BuiltMap<String, String> typesWithBuilder =
new BuiltMap<String, String>({
Expand Down Expand Up @@ -43,8 +52,13 @@ abstract class SourceField implements Built<SourceField, SourceFieldBuilder> {
(metadata) => metadata.constantValue.toStringValue() == 'nullable');
result.name = fieldElement.displayName;
result.type = fieldElement.getter.returnType.displayName;
result.builderFieldUsesNestedBuilder = builderFieldElement != null &&
fieldElement.getter.returnType.displayName !=

// If the builder is present, check it to determine whether a nested
// builder is needed. Otherwise, use the same logic as built_value when
// it decides whether to use a nested builder.
result.builderFieldUsesNestedBuilder = builderFieldElement == null
? _needsNestedBuilder(fieldElement.getter.returnType)
: fieldElement.getter.returnType.displayName !=
builderFieldElement.getter.returnType.displayName;
}

Expand Down Expand Up @@ -87,6 +101,25 @@ abstract class SourceField implements Built<SourceField, SourceFieldBuilder> {
.substring(genericsStart + 1)
.substring(0, name.length - genericsStart - 2);
}

// These three methods are copied from built_value to match the behaviour
// when a builder is not explicitly defined.
// TODO(davidmorgan): dedupe.
static bool _needsNestedBuilder(DartType type) {
return _isBuiltValue(type) || _isBuiltCollection(type);
}

static bool _isBuiltValue(DartType type) {
if (type.element is! ClassElement) return false;
return (type.element as ClassElement)
.allSupertypes
.any((interfaceType) => interfaceType.name == 'Built');
}

static bool _isBuiltCollection(DartType type) {
return _builtCollectionNames
.any((name) => type.displayName.startsWith('${name}<'));
}
}

abstract class SourceFieldBuilder
Expand Down
6 changes: 4 additions & 2 deletions built_json_generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: built_json_generator
version: 0.1.2
version: 0.1.3
description: >
JSON serialization for Built Collections, Built Values and Enum Classes.
This library is the dev dependency.
Expand All @@ -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.2'
#built_json: '^0.1.3'
built_json:
path: ../built_json
source_gen: '>=0.5.0 <0.6.0'
quiver: '>=0.21.0 <0.22.0'

Expand Down
14 changes: 8 additions & 6 deletions example/lib/collections.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions example/lib/compound_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,3 @@ abstract class CompoundValue
CompoundValue._();
factory CompoundValue([updates(CompoundValueBuilder b)]) = _$CompoundValue;
}

/// Builder class for [CompoundValue].
abstract class CompoundValueBuilder
implements Builder<CompoundValue, CompoundValueBuilder> {
ValueBuilder aValue = new ValueBuilder();
HasInt aHasInt;
TestEnum aTestEnum;

CompoundValueBuilder._();
factory CompoundValueBuilder() = _$CompoundValueBuilder;
}
32 changes: 19 additions & 13 deletions example/lib/compound_value.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 35 additions & 32 deletions example/lib/has_int.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/lib/test_enum.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions example/lib/value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,8 @@ abstract class Value implements Built<Value, ValueBuilder>, HasInt {
String get aString;
@nullable String get anotherString;
@nullable Object get anObject;
int get aDefaultInt;
BuiltList<int> get listOfInt;

int get youCanWriteDerivedGetters => anInt + aDefaultInt;

Value._();
factory Value([updates(ValueBuilder b)]) = _$Value;
}

/// Builder class for [Value].
abstract class ValueBuilder implements Builder<Value, ValueBuilder> {
int anInt;
String aString;
@nullable String anotherString;
@nullable Object anObject;
int aDefaultInt = 7;
ListBuilder<int> listOfInt = new ListBuilder<int>();

ValueBuilder._();
factory ValueBuilder() = _$ValueBuilder;

set youCanWriteExtraSetters(int value) {
anInt = value;
aDefaultInt = value;
}
}
Loading