@@ -11,6 +11,7 @@ import '../util/case_util.dart';
11
11
import '../util/type_checker.dart' ;
12
12
13
13
class ObjectModelWriter {
14
+ static const autoGeneratedWarning = '// GENERATED CODE - DO NOT MODIFY BY HAND' ;
14
15
final PubspecConfig pubspecConfig;
15
16
final ObjectModel jsonModel;
16
17
final List <Field > extendsFields;
@@ -25,12 +26,10 @@ class ObjectModelWriter {
25
26
26
27
String write () {
27
28
final sb = StringBuffer ()
28
- ..writeln ("// GENERATED CODE - DO NOT MODIFY BY HAND" )
29
+ ..writeln (autoGeneratedWarning )
29
30
..writeln ();
30
- final imports = < String > {}
31
- ..add ("import 'package:json_annotation/json_annotation.dart';" );
32
- for (final element
33
- in (jsonModel.extraImports ?? pubspecConfig.extraImports)) {
31
+ final imports = < String > {}..add ("import 'package:json_annotation/json_annotation.dart';" );
32
+ for (final element in (jsonModel.extraImports ?? pubspecConfig.extraImports)) {
34
33
imports.add ('import \' $element \' ;' );
35
34
}
36
35
final extendsModel = jsonModel.extendsModel;
@@ -43,8 +42,7 @@ class ObjectModelWriter {
43
42
44
43
for (final field in jsonModel.fields) {
45
44
final type = field.type;
46
- if (! TypeChecker .isKnownDartType (type.name) &&
47
- type.name != jsonModel.name) {
45
+ if (! TypeChecker .isKnownDartType (type.name) && type.name != jsonModel.name) {
48
46
imports.addAll (_getImportsFromPath (type.name));
49
47
}
50
48
if (type is MapType && ! TypeChecker .isKnownDartType (type.valueName)) {
@@ -75,8 +73,7 @@ class ObjectModelWriter {
75
73
} else {
76
74
sb.writeln ('@JsonSerializable()' );
77
75
}
78
- (jsonModel.extraAnnotations ?? pubspecConfig.extraAnnotations)
79
- .forEach (sb.writeln);
76
+ (jsonModel.extraAnnotations ?? pubspecConfig.extraAnnotations).forEach (sb.writeln);
80
77
81
78
for (final converter in jsonModel.converters) {
82
79
sb.writeln ('@$converter ()' );
@@ -117,8 +114,7 @@ class ObjectModelWriter {
117
114
}
118
115
119
116
if (key.unknownEnumValue != null ) {
120
- sb.write (
121
- ', unknownEnumValue: ${key .type .name }.${key .unknownEnumValue }' );
117
+ sb.write (', unknownEnumValue: ${key .type .name }.${key .unknownEnumValue }' );
122
118
}
123
119
124
120
final fieldModel = yamlConfig.getModelByName (key.type);
@@ -141,29 +137,22 @@ class ObjectModelWriter {
141
137
sb.writeln ('${_getKeyType (key )} ${key .name };' );
142
138
}
143
139
144
- final anyNonFinal = jsonModel.fields.any ((element) => element.nonFinal) ||
145
- extendsFields.any ((element) => element.nonFinal);
140
+ final anyNonFinal = jsonModel.fields.any ((element) => element.nonFinal) || extendsFields.any ((element) => element.nonFinal);
146
141
sb
147
142
..writeln ()
148
143
..writeln (' ${anyNonFinal ? '' : 'const ' }${jsonModel .name }({' );
149
144
150
- for (final key in jsonModel.fields
151
- .where ((key) => (key.isRequired && ! key.hasDefaultValue))) {
145
+ for (final key in jsonModel.fields.where ((key) => (key.isRequired && ! key.hasDefaultValue))) {
152
146
sb.writeln (' required this.${key .name }${_fillDefaulValue (key )},' );
153
147
}
154
- for (final key in extendsFields
155
- .where ((key) => (key.isRequired && ! key.hasDefaultValue))) {
156
- sb.writeln (
157
- ' required ${_getKeyType (key )} ${key .name }${_fillDefaulValue (key )},' );
148
+ for (final key in extendsFields.where ((key) => (key.isRequired && ! key.hasDefaultValue))) {
149
+ sb.writeln (' required ${_getKeyType (key )} ${key .name }${_fillDefaulValue (key )},' );
158
150
}
159
- for (final key in jsonModel.fields
160
- .where ((key) => ! (key.isRequired && ! key.hasDefaultValue))) {
151
+ for (final key in jsonModel.fields.where ((key) => ! (key.isRequired && ! key.hasDefaultValue))) {
161
152
sb.writeln (' this.${key .name }${_fillDefaulValue (key )},' );
162
153
}
163
- for (final key in extendsFields
164
- .where ((key) => ! (key.isRequired && ! key.hasDefaultValue))) {
165
- sb.writeln (
166
- ' ${_getKeyType (key )} ${key .name }${_fillDefaulValue (key )},' );
154
+ for (final key in extendsFields.where ((key) => ! (key.isRequired && ! key.hasDefaultValue))) {
155
+ sb.writeln (' ${_getKeyType (key )} ${key .name }${_fillDefaulValue (key )},' );
167
156
}
168
157
if (extendsModel != null ) {
169
158
sb.writeln (' }) : super(' );
@@ -179,25 +168,21 @@ class ObjectModelWriter {
179
168
..writeln ();
180
169
}
181
170
if (jsonModel.generateForGenerics) {
182
- sb.writeln (
183
- ' factory ${jsonModel .name }.fromJson(Object? json) => _\$ ${jsonModel .name }FromJson(json as Map<String, dynamic>); // ignore: avoid_as' );
171
+ sb.writeln (' factory ${jsonModel .name }.fromJson(Object? json) => _\$ ${jsonModel .name }FromJson(json as Map<String, dynamic>); // ignore: avoid_as' );
184
172
} else {
185
- sb.writeln (
186
- ' factory ${jsonModel .name }.fromJson(Map<String, dynamic> json) => _\$ ${jsonModel .name }FromJson(json);' );
173
+ sb.writeln (' factory ${jsonModel .name }.fromJson(Map<String, dynamic> json) => _\$ ${jsonModel .name }FromJson(json);' );
187
174
}
188
175
sb.writeln ();
189
176
if (extendsModel != null ) {
190
177
sb.writeln (' @override' );
191
178
}
192
- sb.writeln (
193
- ' Map<String, dynamic> toJson() => _\$ ${jsonModel .name }ToJson(this);' );
179
+ sb.writeln (' Map<String, dynamic> toJson() => _\$ ${jsonModel .name }ToJson(this);' );
194
180
195
181
if (jsonModel.staticCreate ?? pubspecConfig.staticCreate) {
196
182
sb
197
183
..writeln ()
198
184
..writeln (' // ignore: prefer_constructors_over_static_methods' )
199
- ..writeln (
200
- ' static ${jsonModel .name } create(${jsonModel .generateForGenerics ? 'Object? json' : 'Map<String, dynamic> json' }) => ${jsonModel .name }.fromJson(json);' );
185
+ ..writeln (' static ${jsonModel .name } create(${jsonModel .generateForGenerics ? 'Object? json' : 'Map<String, dynamic> json' }) => ${jsonModel .name }.fromJson(json);' );
201
186
}
202
187
203
188
if (jsonModel.equalsAndHashCode ?? pubspecConfig.equalsHashCode) {
@@ -208,8 +193,7 @@ class ObjectModelWriter {
208
193
..writeln (' identical(this, other) ||' )
209
194
..writeln (' other is ${jsonModel .name } &&' )
210
195
..write (' runtimeType == other.runtimeType' );
211
- for (final field
212
- in jsonModel.fields.where ((field) => ! field.ignoreEquality)) {
196
+ for (final field in jsonModel.fields.where ((field) => ! field.ignoreEquality)) {
213
197
sb.write (' &&\n ${field .name } == other.${field .name }' );
214
198
}
215
199
if (extendsModel != null ) {
@@ -221,8 +205,7 @@ class ObjectModelWriter {
221
205
..writeln (' @override' )
222
206
..writeln (' int get hashCode =>' );
223
207
var c = 0 ;
224
- for (final field
225
- in jsonModel.fields.where ((field) => ! field.ignoreEquality)) {
208
+ for (final field in jsonModel.fields.where ((field) => ! field.ignoreEquality)) {
226
209
if (c++ > 0 ) sb.write (' ^\n ' );
227
210
sb.write (' ${field .name }.hashCode' );
228
211
}
@@ -263,8 +246,7 @@ class ObjectModelWriter {
263
246
}
264
247
265
248
String _getKeyType (Field key) {
266
- final nullableFlag =
267
- key.isRequired || key.type.name == 'dynamic' ? '' : '?' ;
249
+ final nullableFlag = key.isRequired || key.type.name == 'dynamic' ? '' : '?' ;
268
250
final keyType = key.type;
269
251
if (keyType is ArrayType ) {
270
252
return 'List<${keyType .name }>$nullableFlag ' ;
@@ -304,8 +286,7 @@ class ObjectModelWriter {
304
286
if (path.endsWith ('.dart' )) {
305
287
imports.add ("import '$pathWithPackage ';" );
306
288
} else {
307
- imports.add (
308
- "import '$pathWithPackage /${reCaseFieldName .snakeCase }.dart';" );
289
+ imports.add ("import '$pathWithPackage /${reCaseFieldName .snakeCase }.dart';" );
309
290
}
310
291
}
311
292
}
@@ -321,25 +302,19 @@ class ObjectModelWriter {
321
302
}
322
303
323
304
void createRetrofitMappers (StringBuffer sb) {
324
- final hasConstructorTearOffs =
325
- pubspecConfig.languageVersion? .isAtLeast (2 , 15 ) ?? false ;
305
+ final hasConstructorTearOffs = pubspecConfig.languageVersion? .isAtLeast (2 , 15 ) ?? false ;
326
306
sb
327
307
..writeln ()
328
308
..writeln (hasConstructorTearOffs
329
309
? 'const deserialize${jsonModel .name } = ${jsonModel .name }.fromJson;'
330
310
: '${jsonModel .name } deserialize${jsonModel .name }(Map<String, dynamic> json) => ${jsonModel .name }.fromJson(json);' )
331
311
..writeln ()
332
- ..writeln (
333
- 'Map<String, dynamic> serialize${jsonModel .name }(${jsonModel .name } object) => object.toJson();' )
312
+ ..writeln ('Map<String, dynamic> serialize${jsonModel .name }(${jsonModel .name } object) => object.toJson();' )
334
313
..writeln ()
335
- ..writeln (
336
- 'List<${jsonModel .name }> deserialize${jsonModel .name }List(List<Map<String, dynamic>> jsonList)' )
337
- ..writeln (hasConstructorTearOffs
338
- ? ' => jsonList.map(${jsonModel .name }.fromJson).toList();'
339
- : ' => jsonList.map((json) => ${jsonModel .name }.fromJson(json)).toList();' )
314
+ ..writeln ('List<${jsonModel .name }> deserialize${jsonModel .name }List(List<Map<String, dynamic>> jsonList)' )
315
+ ..writeln (hasConstructorTearOffs ? ' => jsonList.map(${jsonModel .name }.fromJson).toList();' : ' => jsonList.map((json) => ${jsonModel .name }.fromJson(json)).toList();' )
340
316
..writeln ()
341
- ..writeln (
342
- 'List<Map<String, dynamic>> serialize${jsonModel .name }List(List<${jsonModel .name }> objects)' )
317
+ ..writeln ('List<Map<String, dynamic>> serialize${jsonModel .name }List(List<${jsonModel .name }> objects)' )
343
318
..writeln (' => objects.map((object) => object.toJson()).toList();' );
344
319
}
345
320
}
0 commit comments