Skip to content

Commit 0ad1a5a

Browse files
authored
Merge pull request #250 from davidmorgan/allow-concrete-in-mixins
Stop generating implementation for fields already implemented in a mixin.
2 parents 4bae5d8 + f4530a0 commit 0ad1a5a

File tree

14 files changed

+210
-15
lines changed

14 files changed

+210
-15
lines changed

CHANGELOG.md

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

3+
## 4.3.1
4+
5+
- Fix generation when a field comes from an interface but is also implemented
6+
by a mixin.
7+
38
## 4.3.0
49

510
- Support serializing Int64.

benchmark/pubspec.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ environment:
1212
dependencies:
1313
browser: any
1414
built_collection: ^1.0.0
15-
built_value: ^4.3.0
15+
# built_value: ^4.3.0
16+
built_value:
17+
path: ../built_value
1618

1719
dev_dependencies:
1820
build: ^0.10.0
1921
build_runner: ^0.4.0
20-
built_value_generator: ^4.3.0
22+
# built_value_generator: ^4.3.0
23+
built_value_generator:
24+
path: ../built_value_generator
2125
source_gen: ^0.7.0
2226
quiver: '>=0.21.0 <0.26.0'
2327
test: any

built_value_generator/lib/src/fields.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import 'package:analyzer/dart/element/element.dart';
77
import 'package:analyzer/dart/element/type.dart';
88
import 'package:built_collection/built_collection.dart';
99

10-
/// Gets fields, including from interfaces.
10+
/// Gets fields, including from interfaces. Fields from interfaces are only
11+
/// returned if they are not also implemented by a mixin.
1112
///
1213
/// If a field is overriden then just the closest (overriding) field is
1314
/// returned.
@@ -21,11 +22,17 @@ BuiltList<FieldElement> collectFields(ClassElement element) {
2122
..addAll(element.mixins)
2223
..forEach((interface) => fields.addAll(collectFields(interface.element)));
2324

24-
// Overridden fields have multiple declarations, so deduplicate by adding
25-
// to a set that compares on field name.
25+
// Overridden fields have multiple declarations, so deduplicate by adding
26+
// to a set that compares on field name.
2627
final fieldSet = new LinkedHashSet<FieldElement>(
2728
equals: (a, b) => a.displayName == b.displayName,
2829
hashCode: (a) => a.displayName.hashCode);
2930
fieldSet.addAll(fields);
30-
return new BuiltList<FieldElement>(fieldSet);
31+
32+
// Filter out interface fields that are implemented in a mixin.
33+
return new BuiltList<FieldElement>.build((b) => b
34+
..addAll(fieldSet)
35+
..where((field) =>
36+
field.enclosingElement == element ||
37+
element.lookUpGetter(field.name, element.library).isAbstract));
3138
}

built_value_generator/pubspec.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ dependencies:
1414
analyzer: '>=0.29.0 <0.31.0'
1515
build: ^0.10.0
1616
built_collection: ^1.0.0
17-
built_value: ^4.3.0
17+
# built_value: ^4.3.0
18+
built_value:
19+
path: ../built_value
1820
source_gen: ^0.7.0
1921
quiver: '>=0.21.0 <0.26.0'
2022

built_value_test/pubspec.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ environment:
1111
sdk: '>=1.8.0 <2.0.0'
1212

1313
dependencies:
14-
built_value: ^4.3.0
14+
# built_value: ^4.3.0
15+
built_value:
16+
path: ../built_value
1517
built_collection: ^1.0.0
1618
collection: ^1.0.0
1719
quiver: '>=0.21.0 <0.26.0'
@@ -20,5 +22,7 @@ dependencies:
2022
dev_dependencies:
2123
build: ^0.10.0
2224
build_runner: ^0.4.0
23-
built_value_generator: ^4.3.0
25+
# built_value_generator: ^4.3.0
26+
built_value_generator:
27+
path: ../built_value_generator
2428
source_gen: ^0.7.0

chat_example/pubspec.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ environment:
1212
dependencies:
1313
browser: ^0.10.0
1414
built_collection: ^1.0.0
15-
built_value: ^4.3.0
15+
# built_value: ^4.3.0
16+
built_value:
17+
path: ../built_value
1618
shelf: ^0.6.0
1719
shelf_proxy: ^0.1.0
1820
shelf_web_socket: ^0.2.1
@@ -21,6 +23,8 @@ dependencies:
2123
dev_dependencies:
2224
build: ^0.10.0
2325
build_runner: ^0.4.0
24-
built_value_generator: ^4.3.0
26+
# built_value_generator: ^4.3.0
27+
built_value_generator:
28+
path: ../built_value_generator
2529
source_gen: ^0.7.0
2630
test: any

end_to_end_test/lib/polymorphism.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ abstract class Cage implements Built<Cage, CageBuilder> {
6565
Cage._();
6666
}
6767

68+
// It's possible to implement getters via mixins. Such getters are treated
69+
// exactly as concrete getters in the immediate class -- they are ignored.
70+
abstract class FourLeggedWalker {
71+
int get legs => 4;
72+
}
73+
74+
abstract class HasLegs {
75+
int get legs;
76+
}
77+
78+
// "Legs" field comes from "FourLeggedWalker" mixin and does not lead to a
79+
// generated field.
80+
abstract class StandardCat extends Object
81+
with Walker, FourLeggedWalker
82+
implements HasLegs, Built<StandardCat, StandardCatBuilder> {
83+
static Serializer<StandardCat> get serializer => _$standardCatSerializer;
84+
85+
bool get tail;
86+
87+
factory StandardCat([updates(StandardCatBuilder b)]) = _$StandardCat;
88+
StandardCat._();
89+
}
90+
6891
@BuiltValue(instantiable: false)
6992
abstract class HasField<T> implements Built<HasField<T>, HasFieldBuilder<T>> {
7093
T get field;

end_to_end_test/lib/polymorphism.g.dart

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

end_to_end_test/lib/serializers.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ part 'serializers.g.dart';
3636
Robot,
3737
SecondTestEnum,
3838
SimpleValue,
39+
StandardCat,
3940
StandardJsonValue,
4041
TestEnum,
4142
ValueUsingImportAs,

end_to_end_test/lib/serializers.g.dart

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

0 commit comments

Comments
 (0)