Skip to content

Commit 2dceb1b

Browse files
committed
DRY up implementation of TypeHelper impls that map to String
1 parent 025bb14 commit 2dceb1b

File tree

4 files changed

+89
-86
lines changed

4 files changed

+89
-86
lines changed

json_serializable/lib/src/type_helpers/big_int_helper.dart

+6-30
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,20 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/element/type.dart';
6-
import 'package:source_gen/source_gen.dart' show TypeChecker;
6+
77
import '../type_helper.dart';
8+
import 'to_from_string.dart';
89

910
class BigIntHelper extends TypeHelper {
1011
const BigIntHelper();
1112

1213
@override
1314
String serialize(
14-
DartType targetType, String expression, TypeHelperContext context) {
15-
if (!_matchesType(targetType)) {
16-
return null;
17-
}
18-
19-
final buffer = StringBuffer(expression);
20-
21-
if (context.nullable) {
22-
buffer.write('?');
23-
}
24-
25-
buffer.write('.toString()');
26-
27-
return buffer.toString();
28-
}
15+
DartType targetType, String expression, TypeHelperContext context) =>
16+
bigIntString.serialize(targetType, expression, context.nullable);
2917

3018
@override
3119
String deserialize(
32-
DartType targetType, String expression, TypeHelperContext context) {
33-
if (!_matchesType(targetType)) {
34-
return null;
35-
}
36-
37-
return commonNullPrefix(
38-
context.nullable,
39-
expression,
40-
'BigInt.parse($expression as String)',
41-
).toString();
42-
}
20+
DartType targetType, String expression, TypeHelperContext context) =>
21+
bigIntString.deserialize(targetType, expression, context.nullable, false);
4322
}
44-
45-
bool _matchesType(DartType type) =>
46-
const TypeChecker.fromUrl('dart:core#BigInt').isExactlyType(type);

json_serializable/lib/src/type_helpers/date_time_helper.dart

+6-28
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,21 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/element/type.dart';
6-
import 'package:source_gen/source_gen.dart' show TypeChecker;
76

87
import '../type_helper.dart';
8+
import 'to_from_string.dart';
99

1010
class DateTimeHelper extends TypeHelper {
1111
const DateTimeHelper();
1212

1313
@override
1414
String serialize(
15-
DartType targetType, String expression, TypeHelperContext context) {
16-
if (!_matchesType(targetType)) {
17-
return null;
18-
}
19-
20-
final buffer = StringBuffer(expression);
21-
22-
if (context.nullable) {
23-
buffer.write('?');
24-
}
25-
26-
buffer.write('.toIso8601String()');
27-
28-
return buffer.toString();
29-
}
15+
DartType targetType, String expression, TypeHelperContext context) =>
16+
dateTimeString.serialize(targetType, expression, context.nullable);
3017

3118
@override
3219
String deserialize(
33-
DartType targetType, String expression, TypeHelperContext context) {
34-
if (!_matchesType(targetType)) {
35-
return null;
36-
}
37-
38-
return commonNullPrefix(context.nullable, expression,
39-
'DateTime.parse($expression as String)')
40-
.toString();
41-
}
20+
DartType targetType, String expression, TypeHelperContext context) =>
21+
dateTimeString.deserialize(
22+
targetType, expression, context.nullable, false);
4223
}
43-
44-
bool _matchesType(DartType type) =>
45-
const TypeChecker.fromUrl('dart:core#DateTime').isExactlyType(type);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/dart/element/type.dart';
6+
import 'package:source_gen/source_gen.dart';
7+
8+
import '../type_helper.dart';
9+
10+
const bigIntString = ToFromStringHelper(
11+
'BigInt.parse',
12+
'toString()',
13+
TypeChecker.fromUrl('dart:core#BigInt'),
14+
);
15+
16+
const dateTimeString = ToFromStringHelper(
17+
'DateTime.parse',
18+
'toIso8601String()',
19+
TypeChecker.fromUrl('dart:core#DateTime'),
20+
);
21+
22+
const uriString = ToFromStringHelper(
23+
'Uri.parse',
24+
'toString()',
25+
TypeChecker.fromUrl('dart:core#Uri'),
26+
);
27+
28+
/// Package-internal helper that unifies implementations of [Type]s that convert
29+
/// trivially to-from [String].
30+
class ToFromStringHelper {
31+
/// The function or constructor to call when creating the associated type.
32+
///
33+
/// Assumed to have one parameter of type [String].
34+
///
35+
/// Example: `MyClass.parse`
36+
final String _parse;
37+
38+
/// Represents an invocation – property access or function call – on an
39+
/// instance of the target type that returns [String].
40+
///
41+
/// Examples: `toString()` for a function or `stringValue` for a property.
42+
final String _toString;
43+
final TypeChecker _checker;
44+
45+
const ToFromStringHelper(this._parse, this._toString, this._checker);
46+
47+
bool matches(DartType type) => _checker.isExactlyType(type);
48+
49+
String serialize(DartType type, String expression, bool nullable) {
50+
if (!matches(type)) {
51+
return null;
52+
}
53+
54+
if (nullable) {
55+
expression = '$expression?';
56+
}
57+
58+
return '$expression.$_toString';
59+
}
60+
61+
String deserialize(
62+
DartType type, String expression, bool nullable, bool isString) {
63+
if (!matches(type)) {
64+
return null;
65+
}
66+
67+
final parseParam = isString ? expression : '$expression as String';
68+
69+
return commonNullPrefix(nullable, expression, '$_parse($parseParam)')
70+
.toString();
71+
}
72+
}

json_serializable/lib/src/type_helpers/uri_helper.dart

+5-28
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,20 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/element/type.dart';
6-
import 'package:source_gen/source_gen.dart' show TypeChecker;
76

87
import '../type_helper.dart';
8+
import 'to_from_string.dart';
99

1010
class UriHelper extends TypeHelper {
1111
const UriHelper();
1212

1313
@override
1414
String serialize(
15-
DartType targetType, String expression, TypeHelperContext context) {
16-
if (!_matchesType(targetType)) {
17-
return null;
18-
}
19-
20-
final buffer = StringBuffer(expression);
21-
22-
if (context.nullable) {
23-
buffer.write('?');
24-
}
25-
26-
buffer.write('.toString()');
27-
28-
return buffer.toString();
29-
}
15+
DartType targetType, String expression, TypeHelperContext context) =>
16+
uriString.serialize(targetType, expression, context.nullable);
3017

3118
@override
3219
String deserialize(
33-
DartType targetType, String expression, TypeHelperContext context) {
34-
if (!_matchesType(targetType)) {
35-
return null;
36-
}
37-
38-
return commonNullPrefix(
39-
context.nullable, expression, 'Uri.parse($expression as String)')
40-
.toString();
41-
}
20+
DartType targetType, String expression, TypeHelperContext context) =>
21+
uriString.deserialize(targetType, expression, context.nullable, false);
4222
}
43-
44-
bool _matchesType(DartType type) =>
45-
const TypeChecker.fromUrl('dart:core#Uri').isExactlyType(type);

0 commit comments

Comments
 (0)