@@ -21,10 +21,10 @@ class JsonConverterHelper extends TypeHelper<TypeHelperContextWithConfig> {
21
21
22
22
@override
23
23
Object ? serialize (
24
- DartType targetType,
25
- String expression,
26
- TypeHelperContextWithConfig context,
27
- ) {
24
+ DartType targetType,
25
+ String expression,
26
+ TypeHelperContextWithConfig context,
27
+ ) {
28
28
final converter = _typeConverter (targetType, context);
29
29
30
30
if (converter == null ) {
@@ -54,11 +54,11 @@ Json? $converterToJsonName<Json, Value>(
54
54
55
55
@override
56
56
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
+ ) {
62
62
final converter = _typeConverter (targetType, context);
63
63
if (converter == null ) {
64
64
return null ;
@@ -91,14 +91,16 @@ Value? $converterFromJsonName<Json, Value>(
91
91
}
92
92
93
93
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
+ }) {
100
100
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);
102
104
103
105
return '$name <$jsonDisplayString , $fieldTypeDisplayString >('
104
106
'$expression , $callback )' ;
@@ -111,31 +113,33 @@ class _JsonConvertData {
111
113
final bool isGeneric;
112
114
113
115
_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 ?? '' })' ,
120
122
isGeneric = false ;
121
123
122
124
_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 ?? '' })' ,
130
133
isGeneric = true ;
131
134
132
135
_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 ;
137
140
138
- static String _withAccessor (String accessor) => accessor.isEmpty ? '' : '.$accessor ' ;
141
+ static String _withAccessor (String accessor) =>
142
+ accessor.isEmpty ? '' : '.$accessor ' ;
139
143
}
140
144
141
145
/// If there is no converter for the params, return `null` .
@@ -144,9 +148,9 @@ class _JsonConvertData {
144
148
///
145
149
/// Used to make sure we create a smart encoding function.
146
150
bool ? hasConverterNullEncode (
147
- DartType targetType,
148
- TypeHelperContextWithConfig ctx,
149
- ) {
151
+ DartType targetType,
152
+ TypeHelperContextWithConfig ctx,
153
+ ) {
150
154
final data = _typeConverter (targetType, ctx);
151
155
152
156
if (data == null ) {
@@ -157,30 +161,34 @@ bool? hasConverterNullEncode(
157
161
}
158
162
159
163
_JsonConvertData ? _typeConverter (
160
- DartType targetType,
161
- TypeHelperContextWithConfig ctx,
162
- ) {
164
+ DartType targetType,
165
+ TypeHelperContextWithConfig ctx,
166
+ ) {
163
167
List <_ConverterMatch > converterMatches (List <ElementAnnotation > items) => items
164
168
.map (
165
169
(annotation) => _compatibleMatch (
166
- targetType,
167
- annotation,
168
- annotation.computeConstantValue ()! ,
169
- ),
170
- )
170
+ targetType,
171
+ annotation,
172
+ annotation.computeConstantValue ()! ,
173
+ ),
174
+ )
171
175
.whereType <_ConverterMatch >()
172
176
.toList ();
173
177
174
178
var matchingAnnotations = converterMatches (ctx.fieldElement.metadata);
175
179
176
180
if (matchingAnnotations.isEmpty) {
177
- matchingAnnotations = converterMatches (ctx.fieldElement.getter? .metadata ?? []);
181
+ matchingAnnotations =
182
+ converterMatches (ctx.fieldElement.getter? .metadata ?? []);
178
183
179
184
if (matchingAnnotations.isEmpty) {
180
185
matchingAnnotations = converterMatches (ctx.classElement.metadata);
181
186
182
187
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 ();
184
192
}
185
193
}
186
194
}
@@ -189,9 +197,9 @@ _JsonConvertData? _typeConverter(
189
197
}
190
198
191
199
_JsonConvertData ? _typeConverterFrom (
192
- List <_ConverterMatch > matchingAnnotations,
193
- DartType targetType,
194
- ) {
200
+ List <_ConverterMatch > matchingAnnotations,
201
+ DartType targetType,
202
+ ) {
195
203
if (matchingAnnotations.isEmpty) {
196
204
return null ;
197
205
}
@@ -315,23 +323,24 @@ class _ConverterMatch {
315
323
final String ? genericTypeArg;
316
324
317
325
_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
+ );
324
332
}
325
333
326
334
_ConverterMatch ? _compatibleMatch (
327
- DartType targetType,
328
- ElementAnnotation ? annotation,
329
- DartObject constantValue,
330
- ) {
335
+ DartType targetType,
336
+ ElementAnnotation ? annotation,
337
+ DartObject constantValue,
338
+ ) {
331
339
final converterClassElement = constantValue.type! .element as ClassElement ;
332
340
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),
335
344
);
336
345
337
346
if (jsonConverterSuper == null ) {
@@ -360,8 +369,8 @@ _ConverterMatch? _compatibleMatch(
360
369
if (converterClassElement.typeParameters.length > 1 ) {
361
370
throw InvalidGenerationSourceError (
362
371
'`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 }.' ,
365
374
element: converterClassElement);
366
375
}
367
376
0 commit comments