Skip to content

Commit 36df84a

Browse files
authored
Update json_converter_helper.dart
1 parent 2ff08f8 commit 36df84a

File tree

1 file changed

+73
-64
lines changed

1 file changed

+73
-64
lines changed

json_serializable/lib/src/type_helpers/json_converter_helper.dart

+73-64
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class JsonConverterHelper extends TypeHelper<TypeHelperContextWithConfig> {
2121

2222
@override
2323
Object? serialize(
24-
DartType targetType,
25-
String expression,
26-
TypeHelperContextWithConfig context,
27-
) {
24+
DartType targetType,
25+
String expression,
26+
TypeHelperContextWithConfig context,
27+
) {
2828
final converter = _typeConverter(targetType, context);
2929

3030
if (converter == null) {
@@ -54,11 +54,11 @@ Json? $converterToJsonName<Json, Value>(
5454

5555
@override
5656
Object? deserialize(
57-
DartType targetType,
58-
String expression,
59-
TypeHelperContextWithConfig context,
60-
bool defaultProvided,
61-
) {
57+
DartType targetType,
58+
String expression,
59+
TypeHelperContextWithConfig context,
60+
bool defaultProvided,
61+
) {
6262
final converter = _typeConverter(targetType, context);
6363
if (converter == null) {
6464
return null;
@@ -91,14 +91,16 @@ Value? $converterFromJsonName<Json, Value>(
9191
}
9292

9393
String _nullableJsonConverterLambdaResult(
94-
_JsonConvertData converter, {
95-
required String name,
96-
required DartType targetType,
97-
required String expression,
98-
required String callback,
99-
}) {
94+
_JsonConvertData converter, {
95+
required String name,
96+
required DartType targetType,
97+
required String expression,
98+
required String callback,
99+
}) {
100100
final jsonDisplayString = typeToCode(converter.jsonType);
101-
final fieldTypeDisplayString = converter.isGeneric ? typeToCode(targetType) : typeToCode(converter.fieldType);
101+
final fieldTypeDisplayString = converter.isGeneric
102+
? typeToCode(targetType)
103+
: typeToCode(converter.fieldType);
102104

103105
return '$name<$jsonDisplayString, $fieldTypeDisplayString>('
104106
'$expression, $callback)';
@@ -111,31 +113,33 @@ class _JsonConvertData {
111113
final bool isGeneric;
112114

113115
_JsonConvertData.className(
114-
String className,
115-
String? arguments,
116-
String accessor,
117-
this.jsonType,
118-
this.fieldType,
119-
) : accessString = 'const $className${_withAccessor(accessor)}(${arguments ?? ''})',
116+
String className,
117+
String? arguments,
118+
String accessor,
119+
this.jsonType,
120+
this.fieldType,
121+
) : accessString = 'const $className${_withAccessor(accessor)}(${arguments ?? ''})',
120122
isGeneric = false;
121123

122124
_JsonConvertData.genericClass(
123-
String className,
124-
String? arguments,
125-
String genericTypeArg,
126-
String accessor,
127-
this.jsonType,
128-
this.fieldType,
129-
) : accessString = '$className<$genericTypeArg>${_withAccessor(accessor)}(${arguments ?? ''})',
125+
String className,
126+
String? arguments,
127+
String genericTypeArg,
128+
String accessor,
129+
this.jsonType,
130+
this.fieldType,
131+
) : accessString =
132+
'$className<$genericTypeArg>${_withAccessor(accessor)}(${arguments ?? ''})',
130133
isGeneric = true;
131134

132135
_JsonConvertData.propertyAccess(
133-
this.accessString,
134-
this.jsonType,
135-
this.fieldType,
136-
) : isGeneric = false;
136+
this.accessString,
137+
this.jsonType,
138+
this.fieldType,
139+
) : isGeneric = false;
137140

138-
static String _withAccessor(String accessor) => accessor.isEmpty ? '' : '.$accessor';
141+
static String _withAccessor(String accessor) =>
142+
accessor.isEmpty ? '' : '.$accessor';
139143
}
140144

141145
/// If there is no converter for the params, return `null`.
@@ -144,9 +148,9 @@ class _JsonConvertData {
144148
///
145149
/// Used to make sure we create a smart encoding function.
146150
bool? hasConverterNullEncode(
147-
DartType targetType,
148-
TypeHelperContextWithConfig ctx,
149-
) {
151+
DartType targetType,
152+
TypeHelperContextWithConfig ctx,
153+
) {
150154
final data = _typeConverter(targetType, ctx);
151155

152156
if (data == null) {
@@ -157,30 +161,34 @@ bool? hasConverterNullEncode(
157161
}
158162

159163
_JsonConvertData? _typeConverter(
160-
DartType targetType,
161-
TypeHelperContextWithConfig ctx,
162-
) {
164+
DartType targetType,
165+
TypeHelperContextWithConfig ctx,
166+
) {
163167
List<_ConverterMatch> converterMatches(List<ElementAnnotation> items) => items
164168
.map(
165169
(annotation) => _compatibleMatch(
166-
targetType,
167-
annotation,
168-
annotation.computeConstantValue()!,
169-
),
170-
)
170+
targetType,
171+
annotation,
172+
annotation.computeConstantValue()!,
173+
),
174+
)
171175
.whereType<_ConverterMatch>()
172176
.toList();
173177

174178
var matchingAnnotations = converterMatches(ctx.fieldElement.metadata);
175179

176180
if (matchingAnnotations.isEmpty) {
177-
matchingAnnotations = converterMatches(ctx.fieldElement.getter?.metadata ?? []);
181+
matchingAnnotations =
182+
converterMatches(ctx.fieldElement.getter?.metadata ?? []);
178183

179184
if (matchingAnnotations.isEmpty) {
180185
matchingAnnotations = converterMatches(ctx.classElement.metadata);
181186

182187
if (matchingAnnotations.isEmpty) {
183-
matchingAnnotations = ctx.config.converters.map((e) => _compatibleMatch(targetType, null, e)).whereType<_ConverterMatch>().toList();
188+
matchingAnnotations = ctx.config.converters
189+
.map((e) => _compatibleMatch(targetType, null, e))
190+
.whereType<_ConverterMatch>()
191+
.toList();
184192
}
185193
}
186194
}
@@ -189,9 +197,9 @@ _JsonConvertData? _typeConverter(
189197
}
190198

191199
_JsonConvertData? _typeConverterFrom(
192-
List<_ConverterMatch> matchingAnnotations,
193-
DartType targetType,
194-
) {
200+
List<_ConverterMatch> matchingAnnotations,
201+
DartType targetType,
202+
) {
195203
if (matchingAnnotations.isEmpty) {
196204
return null;
197205
}
@@ -315,23 +323,24 @@ class _ConverterMatch {
315323
final String? genericTypeArg;
316324

317325
_ConverterMatch(
318-
this.elementAnnotation,
319-
this.annotation,
320-
this.jsonType,
321-
this.genericTypeArg,
322-
this.fieldType,
323-
);
326+
this.elementAnnotation,
327+
this.annotation,
328+
this.jsonType,
329+
this.genericTypeArg,
330+
this.fieldType,
331+
);
324332
}
325333

326334
_ConverterMatch? _compatibleMatch(
327-
DartType targetType,
328-
ElementAnnotation? annotation,
329-
DartObject constantValue,
330-
) {
335+
DartType targetType,
336+
ElementAnnotation? annotation,
337+
DartObject constantValue,
338+
) {
331339
final converterClassElement = constantValue.type!.element as ClassElement;
332340

333-
final jsonConverterSuper = converterClassElement.allSupertypes.singleWhereOrNull(
334-
(e) => _jsonConverterChecker.isExactly(e.element),
341+
final jsonConverterSuper =
342+
converterClassElement.allSupertypes.singleWhereOrNull(
343+
(e) => _jsonConverterChecker.isExactly(e.element),
335344
);
336345

337346
if (jsonConverterSuper == null) {
@@ -360,8 +369,8 @@ _ConverterMatch? _compatibleMatch(
360369
if (converterClassElement.typeParameters.length > 1) {
361370
throw InvalidGenerationSourceError(
362371
'`JsonConverter` implementations can have no more than one type '
363-
'argument. `${converterClassElement.name}` has '
364-
'${converterClassElement.typeParameters.length}.',
372+
'argument. `${converterClassElement.name}` has '
373+
'${converterClassElement.typeParameters.length}.',
365374
element: converterClassElement);
366375
}
367376

0 commit comments

Comments
 (0)