Skip to content

Commit 5e74afc

Browse files
committed
Centralize logic for walking type hierarchy
1 parent b41111e commit 5e74afc

File tree

2 files changed

+15
-35
lines changed

2 files changed

+15
-35
lines changed

json_serializable/lib/src/shared_checkers.dart

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,21 @@ String asStatement(DartType type) {
5959
return ' as $type';
6060
}
6161

62-
DartType _getImplementationType(DartType type, TypeChecker checker) {
63-
if (checker.isExactlyType(type)) return type;
62+
/// Returns all of the [DartType] types that [type] implements, mixes-in, and
63+
/// extends, starting with [type] itself.
64+
Iterable<DartType> typeImplementations(DartType type) sync* {
65+
yield type;
6466

6567
if (type is InterfaceType) {
66-
var match = type.interfaces
67-
.followedBy(type.mixins)
68-
.map((type) => _getImplementationType(type, checker))
69-
.firstWhere((value) => value != null, orElse: () => null);
70-
71-
if (match != null) {
72-
return match;
73-
}
68+
yield* type.interfaces.expand(typeImplementations);
69+
yield* type.mixins.expand(typeImplementations);
7470

7571
if (type.superclass != null) {
76-
return _getImplementationType(type.superclass, checker);
72+
yield* typeImplementations(type.superclass);
7773
}
7874
}
79-
return null;
8075
}
76+
77+
DartType _getImplementationType(DartType type, TypeChecker checker) =>
78+
typeImplementations(type)
79+
.firstWhere(checker.isExactlyType, orElse: () => null);

json_serializable/lib/src/type_helpers/json_helper.dart

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class JsonHelper extends TypeHelper {
6767

6868
bool _canSerialize(DartType type) {
6969
if (type is InterfaceType) {
70-
var toJsonMethod = _getMethod(type, 'toJson');
70+
var toJsonMethod = _toJsonMethod(type);
7171

7272
if (toJsonMethod != null) {
7373
// TODO: validate there are no required parameters
@@ -95,25 +95,6 @@ JsonSerializable _annotation(InterfaceType source) {
9595
return valueForAnnotation(new ConstantReader(annotations.single));
9696
}
9797

98-
MethodElement _getMethod(DartType type, String methodName) {
99-
if (type is InterfaceType) {
100-
var method = type.element.getMethod(methodName);
101-
if (method != null) {
102-
return method;
103-
}
104-
105-
var match = type.interfaces
106-
.followedBy(type.mixins)
107-
.map((type) => _getMethod(type, methodName))
108-
.firstWhere((value) => value != null, orElse: () => null);
109-
110-
if (match != null) {
111-
return match;
112-
}
113-
114-
if (type.superclass != null) {
115-
return _getMethod(type.superclass, methodName);
116-
}
117-
}
118-
return null;
119-
}
98+
MethodElement _toJsonMethod(DartType type) => typeImplementations(type)
99+
.map((dt) => dt is InterfaceType ? dt.getMethod('toJson') : null)
100+
.firstWhere((me) => me != null, orElse: () => null);

0 commit comments

Comments
 (0)