Skip to content

Commit d93c11c

Browse files
committedDec 21, 2022
#123 Use a const in the warning
1 parent 9df466a commit d93c11c

File tree

2 files changed

+35
-68
lines changed

2 files changed

+35
-68
lines changed
 

‎lib/writer/enum_model_writer.dart

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:model_generator/model/model/enum_model.dart';
2+
import 'package:model_generator/writer/object_model_writer.dart';
23

34
class EnumModelWriter {
45
final EnumModel jsonModel;
@@ -7,7 +8,7 @@ class EnumModelWriter {
78

89
String write() {
910
final sb = StringBuffer()
10-
..writeln("// GENERATED CODE - DO NOT MODIFY BY HAND")
11+
..writeln(ObjectModelWriter.autoGeneratedWarning)
1112
..writeln()
1213
..writeln("import 'package:json_annotation/json_annotation.dart';")
1314
..writeln();
@@ -19,9 +20,7 @@ class EnumModelWriter {
1920

2021
sb.writeln('enum ${jsonModel.name} {');
2122
jsonModel.fields?.forEach((key) {
22-
final jsonValue = key.value == null || key.value?.isEmpty == null
23-
? key.serializedName
24-
: key.value;
23+
final jsonValue = key.value == null || key.value?.isEmpty == null ? key.serializedName : key.value;
2524
final description = key.description;
2625
if (description != null) {
2726
sb.writeln(' ///$description');
@@ -38,9 +37,7 @@ class EnumModelWriter {
3837
..writeln('const ${jsonModel.name}Mapping = {');
3938

4039
jsonModel.fields?.forEach((key) {
41-
final jsonValue = key.value == null || key.value?.isEmpty == null
42-
? key.serializedName
43-
: key.value;
40+
final jsonValue = key.value == null || key.value?.isEmpty == null ? key.serializedName : key.value;
4441
sb
4542
..write(' ${jsonModel.name}.${key.name}: ')
4643
..writeln('\'$jsonValue\',');
@@ -52,9 +49,7 @@ class EnumModelWriter {
5249
..writeln('const reverse${jsonModel.name}Mapping = {');
5350

5451
jsonModel.fields?.forEach((key) {
55-
final jsonValue = key.value == null || key.value?.isEmpty == null
56-
? key.serializedName
57-
: key.value;
52+
final jsonValue = key.value == null || key.value?.isEmpty == null ? key.serializedName : key.value;
5853
sb
5954
..write(' \'$jsonValue\': ')
6055
..writeln('${jsonModel.name}.${key.name},');
@@ -65,15 +60,12 @@ class EnumModelWriter {
6560
if (jsonModel.generateExtensions) {
6661
sb
6762
..writeln()
68-
..writeln(
69-
'extension ${jsonModel.name}Extension on ${jsonModel.name} {')
70-
..writeln(
71-
' String get stringValue => ${jsonModel.name}Mapping[this]!;')
63+
..writeln('extension ${jsonModel.name}Extension on ${jsonModel.name} {')
64+
..writeln(' String get stringValue => ${jsonModel.name}Mapping[this]!;')
7265
..writeln('}')
7366
..writeln()
7467
..writeln('extension ${jsonModel.name}StringExtension on String {')
75-
..writeln(
76-
' ${jsonModel.name}? get as${jsonModel.name} => reverse${jsonModel.name}Mapping[this];')
68+
..writeln(' ${jsonModel.name}? get as${jsonModel.name} => reverse${jsonModel.name}Mapping[this];')
7769
..writeln('}');
7870
}
7971
}

‎lib/writer/object_model_writer.dart

+27-52
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import '../util/case_util.dart';
1111
import '../util/type_checker.dart';
1212

1313
class ObjectModelWriter {
14+
static const autoGeneratedWarning = '// GENERATED CODE - DO NOT MODIFY BY HAND';
1415
final PubspecConfig pubspecConfig;
1516
final ObjectModel jsonModel;
1617
final List<Field> extendsFields;
@@ -25,12 +26,10 @@ class ObjectModelWriter {
2526

2627
String write() {
2728
final sb = StringBuffer()
28-
..writeln("// GENERATED CODE - DO NOT MODIFY BY HAND")
29+
..writeln(autoGeneratedWarning)
2930
..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)) {
3433
imports.add('import \'$element\';');
3534
}
3635
final extendsModel = jsonModel.extendsModel;
@@ -43,8 +42,7 @@ class ObjectModelWriter {
4342

4443
for (final field in jsonModel.fields) {
4544
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) {
4846
imports.addAll(_getImportsFromPath(type.name));
4947
}
5048
if (type is MapType && !TypeChecker.isKnownDartType(type.valueName)) {
@@ -75,8 +73,7 @@ class ObjectModelWriter {
7573
} else {
7674
sb.writeln('@JsonSerializable()');
7775
}
78-
(jsonModel.extraAnnotations ?? pubspecConfig.extraAnnotations)
79-
.forEach(sb.writeln);
76+
(jsonModel.extraAnnotations ?? pubspecConfig.extraAnnotations).forEach(sb.writeln);
8077

8178
for (final converter in jsonModel.converters) {
8279
sb.writeln('@$converter()');
@@ -117,8 +114,7 @@ class ObjectModelWriter {
117114
}
118115

119116
if (key.unknownEnumValue != null) {
120-
sb.write(
121-
', unknownEnumValue: ${key.type.name}.${key.unknownEnumValue}');
117+
sb.write(', unknownEnumValue: ${key.type.name}.${key.unknownEnumValue}');
122118
}
123119

124120
final fieldModel = yamlConfig.getModelByName(key.type);
@@ -141,29 +137,22 @@ class ObjectModelWriter {
141137
sb.writeln('${_getKeyType(key)} ${key.name};');
142138
}
143139

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);
146141
sb
147142
..writeln()
148143
..writeln(' ${anyNonFinal ? '' : 'const '}${jsonModel.name}({');
149144

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))) {
152146
sb.writeln(' required this.${key.name}${_fillDefaulValue(key)},');
153147
}
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)},');
158150
}
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))) {
161152
sb.writeln(' this.${key.name}${_fillDefaulValue(key)},');
162153
}
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)},');
167156
}
168157
if (extendsModel != null) {
169158
sb.writeln(' }) : super(');
@@ -179,25 +168,21 @@ class ObjectModelWriter {
179168
..writeln();
180169
}
181170
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');
184172
} 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);');
187174
}
188175
sb.writeln();
189176
if (extendsModel != null) {
190177
sb.writeln(' @override');
191178
}
192-
sb.writeln(
193-
' Map<String, dynamic> toJson() => _\$${jsonModel.name}ToJson(this);');
179+
sb.writeln(' Map<String, dynamic> toJson() => _\$${jsonModel.name}ToJson(this);');
194180

195181
if (jsonModel.staticCreate ?? pubspecConfig.staticCreate) {
196182
sb
197183
..writeln()
198184
..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);');
201186
}
202187

203188
if (jsonModel.equalsAndHashCode ?? pubspecConfig.equalsHashCode) {
@@ -208,8 +193,7 @@ class ObjectModelWriter {
208193
..writeln(' identical(this, other) ||')
209194
..writeln(' other is ${jsonModel.name} &&')
210195
..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)) {
213197
sb.write(' &&\n ${field.name} == other.${field.name}');
214198
}
215199
if (extendsModel != null) {
@@ -221,8 +205,7 @@ class ObjectModelWriter {
221205
..writeln(' @override')
222206
..writeln(' int get hashCode =>');
223207
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)) {
226209
if (c++ > 0) sb.write(' ^\n');
227210
sb.write(' ${field.name}.hashCode');
228211
}
@@ -263,8 +246,7 @@ class ObjectModelWriter {
263246
}
264247

265248
String _getKeyType(Field key) {
266-
final nullableFlag =
267-
key.isRequired || key.type.name == 'dynamic' ? '' : '?';
249+
final nullableFlag = key.isRequired || key.type.name == 'dynamic' ? '' : '?';
268250
final keyType = key.type;
269251
if (keyType is ArrayType) {
270252
return 'List<${keyType.name}>$nullableFlag';
@@ -304,8 +286,7 @@ class ObjectModelWriter {
304286
if (path.endsWith('.dart')) {
305287
imports.add("import '$pathWithPackage';");
306288
} else {
307-
imports.add(
308-
"import '$pathWithPackage/${reCaseFieldName.snakeCase}.dart';");
289+
imports.add("import '$pathWithPackage/${reCaseFieldName.snakeCase}.dart';");
309290
}
310291
}
311292
}
@@ -321,25 +302,19 @@ class ObjectModelWriter {
321302
}
322303

323304
void createRetrofitMappers(StringBuffer sb) {
324-
final hasConstructorTearOffs =
325-
pubspecConfig.languageVersion?.isAtLeast(2, 15) ?? false;
305+
final hasConstructorTearOffs = pubspecConfig.languageVersion?.isAtLeast(2, 15) ?? false;
326306
sb
327307
..writeln()
328308
..writeln(hasConstructorTearOffs
329309
? 'const deserialize${jsonModel.name} = ${jsonModel.name}.fromJson;'
330310
: '${jsonModel.name} deserialize${jsonModel.name}(Map<String, dynamic> json) => ${jsonModel.name}.fromJson(json);')
331311
..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();')
334313
..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();')
340316
..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)')
343318
..writeln(' => objects.map((object) => object.toJson()).toList();');
344319
}
345320
}

0 commit comments

Comments
 (0)
Please sign in to comment.