Skip to content

Commit fbd6003

Browse files
authored
Cleanup (#189)
* rename test class to 'KitchenSink' * JSON: rename helper function * Remove single-use helper
1 parent 494e0e7 commit fbd6003

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

lib/generators/json_serializable_generator.dart

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@ class JsonSerializableGenerator
237237
{ParameterElement ctorParam}) {
238238
name = _fieldToJsonMapKey(name, field);
239239
var result = "json['$name']";
240-
return _writeAccessToVar(result, field.type, ctorParam: ctorParam);
240+
return _writeAccessToJsonValue(result, field.type, ctorParam: ctorParam);
241241
}
242242

243-
String _writeAccessToVar(String varExpression, DartType searchType,
243+
String _writeAccessToJsonValue(String varExpression, DartType searchType,
244244
{ParameterElement ctorParam, int depth: 0}) {
245245
if (ctorParam != null) {
246246
searchType = ctorParam.type as InterfaceType;
@@ -258,16 +258,16 @@ class JsonSerializableGenerator
258258
_getIterableGenericType(searchType as InterfaceType);
259259

260260
var itemVal = "v$depth";
261-
var itemSubVal =
262-
_writeAccessToVar(itemVal, iterableGenericType, depth: depth + 1);
261+
var itemSubVal = _writeAccessToJsonValue(itemVal, iterableGenericType,
262+
depth: depth + 1);
263263

264264
// If `itemSubVal` is the same, then we don't need to do anything fancy
265265
if (itemVal == itemSubVal) {
266266
return varExpression;
267267
}
268268

269269
var output = "($varExpression as List)?.map(($itemVal) => "
270-
"${_writeAccessToVar(itemVal, iterableGenericType, depth: depth+1)}"
270+
"${_writeAccessToJsonValue(itemVal, iterableGenericType, depth: depth+1)}"
271271
")";
272272

273273
if (_coreListChecker.isAssignableFromType(searchType)) {
@@ -310,10 +310,10 @@ DartType _getImplementationType(DartType type, TypeChecker checker) {
310310
if (checker.isExactlyType(type)) return type;
311311

312312
if (type is InterfaceType) {
313-
var tests = [type.interfaces, type.mixins]
313+
var match = [type.interfaces, type.mixins]
314314
.expand((e) => e)
315-
.map((type) => _getImplementationType(type, checker));
316-
var match = _firstNotNull(tests);
315+
.map((type) => _getImplementationType(type, checker))
316+
.firstWhere((value) => value != null, orElse: () => null);
317317

318318
if (match != null) return match;
319319

@@ -324,9 +324,6 @@ DartType _getImplementationType(DartType type, TypeChecker checker) {
324324
return null;
325325
}
326326

327-
T _firstNotNull<T>(Iterable<T> values) =>
328-
values.firstWhere((value) => value != null, orElse: () => null);
329-
330327
final _coreIterableChecker = const TypeChecker.fromUrl('dart:core#Iterable');
331328

332329
final _coreListChecker = const TypeChecker.fromUrl('dart:core#List');

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 0.5.9
2+
version: 0.5.10-dev
33
author: Dart Team <[email protected]>
44
description: Automated source code generation for Dart.
55
homepage: https://github.com/dart-lang/source_gen

test/test_files/json_test_example.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import 'package:source_gen/generators/json_serializable.dart';
1212
part 'json_test_example.g.dart';
1313

1414
@JsonSerializable()
15-
class ListGenericTests extends Object with _$ListGenericTestsSerializerMixin {
16-
ListGenericTests();
15+
class KitchenSink extends Object with _$KitchenSinkSerializerMixin {
16+
KitchenSink();
1717

18-
factory ListGenericTests.fromJson(Map<String, Object> json) =>
19-
_$ListGenericTestsFromJson(json);
18+
factory KitchenSink.fromJson(Map<String, Object> json) =>
19+
_$KitchenSinkFromJson(json);
2020

2121
Iterable iterable;
2222
Iterable<dynamic> dynamicIterable;

test/test_files/json_test_example.g.dart

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

0 commit comments

Comments
 (0)