Skip to content

Commit bb0f5d5

Browse files
committed
update
1 parent 934f902 commit bb0f5d5

File tree

1 file changed

+109
-136
lines changed

1 file changed

+109
-136
lines changed

lib/src/generate_dart_models.dart

Lines changed: 109 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ Future<void> generateDartModels(
7777

7878
exit(ExitCodes.FAILURE.code);
7979
}
80-
final analysisContextCollection = createDartAnalysisContextCollection({
81-
inputPath,
82-
}, dartSdk);
80+
final analysisContextCollection = createDartAnalysisContextCollection(
81+
{
82+
inputPath,
83+
},
84+
dartSdk, //
85+
);
8386
Log.printWhite('Reading template at: $templatePathOrUrl...');
8487
final result = await MdTemplateUtility.i
8588
.readTemplateFromPathOrUrl(templatePathOrUrl)
@@ -169,68 +172,56 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
169172
'___SUPER_CONSTRUCTOR___': (insight) {
170173
return insight.annotation.shouldInherit == true
171174
? insight.annotation.inheritanceConstructor?.nullIfEmpty != null
172-
? ': super.${insight.annotation.inheritanceConstructor}()'
173-
: ''
175+
? ': super.${insight.annotation.inheritanceConstructor}()'
176+
: ''
174177
: '';
175178
},
176179
'___FIELD_DECLARATIONS___': (insight) {
177-
return insight.fields
178-
.map((e) {
179-
final description = e.description;
180-
final t = e.fieldType!;
181-
final r = stripSpecialSyntaxFromFieldType(t);
182-
final f = e.fieldName;
183-
return [
184-
' /// ${description ?? 'No description provided.'}',
185-
'final $r? $f;',
186-
'',
187-
].join('\n');
188-
})
189-
.join('\n');
180+
return insight.fields.map((e) {
181+
final description = e.description;
182+
final t = e.fieldType!;
183+
final r = stripSpecialSyntaxFromFieldType(t);
184+
final f = e.fieldName;
185+
return [
186+
' /// ${description ?? 'No description provided.'}',
187+
'final $r? $f;',
188+
'',
189+
].join('\n');
190+
}).join('\n');
190191
},
191192
'___PARAMS___': (insight) {
192-
return insight.fields
193-
.map((e) {
194-
final n = e.nullable;
195-
final f = e.fieldName;
196-
return '${n ? '' : 'required'} this.$f,';
197-
})
198-
.join('\n');
193+
return insight.fields.map((e) {
194+
final n = e.nullable;
195+
final f = e.fieldName;
196+
return '${n ? '' : 'required'} this.$f,';
197+
}).join('\n');
199198
},
200199
'___PERMISSIVE_PARAMS___': (insight) {
201-
return insight.fields
202-
.map((e) {
203-
final f = e.fieldName;
204-
return 'this.$f,';
205-
})
206-
.join('\n');
200+
return insight.fields.map((e) {
201+
final f = e.fieldName;
202+
return 'this.$f,';
203+
}).join('\n');
207204
},
208205
'___STRICT_PARAMS___': (insight) {
209-
return insight.fields
210-
.map((e) {
211-
final t = e.fieldType!;
212-
final r = stripSpecialSyntaxFromFieldType(t);
213-
final f = e.fieldName;
214-
return '$r? $f,';
215-
})
216-
.join('\n');
206+
return insight.fields.map((e) {
207+
final t = e.fieldType!;
208+
final r = stripSpecialSyntaxFromFieldType(t);
209+
final f = e.fieldName;
210+
return '$r? $f,';
211+
}).join('\n');
217212
},
218213
'___STRICT_PARAM_ASSERTIONS___': (insight) {
219-
return insight.fields
220-
.map((e) {
221-
final n = e.nullable;
222-
final f = e.fieldName;
223-
return n ? '' : 'assert($f != null);';
224-
})
225-
.join('\n');
214+
return insight.fields.map((e) {
215+
final n = e.nullable;
216+
final f = e.fieldName;
217+
return n ? '' : 'assert($f != null);';
218+
}).join('\n');
226219
},
227220
'___STRICT_ARGS___': (insight) {
228-
return insight.fields
229-
.map((e) {
230-
final f = e.fieldName;
231-
return '$f: $f,';
232-
})
233-
.join('\n');
221+
return insight.fields.map((e) {
222+
final f = e.fieldName;
223+
return '$f: $f,';
224+
}).join('\n');
234225
},
235226
'___FROM_JSON_OR_NULL_PARAMS___': (insight) {
236227
final fields = insight.fields.toList();
@@ -260,26 +251,22 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
260251
return j.join('\n');
261252
},
262253
'___FROM_JSON_OR_NULL_ARGS___': (insight) {
263-
return insight.fields
264-
.map((e) {
265-
final f = e.fieldName;
266-
return '$f: $f,';
267-
})
268-
.join('\n');
254+
return insight.fields.map((e) {
255+
final f = e.fieldName;
256+
return '$f: $f,';
257+
}).join('\n');
269258
},
270259
'___TO_JSON_PARAMS___': (insight) {
271-
return insight.fields
272-
.map((e) {
273-
final f = e.fieldName!;
274-
final f0 = '${f}0';
275-
final x = e.fieldTypeCode!;
276-
final s = stripSpecialSyntaxFromFieldType(x);
277-
final a = DartTypeCodeMapper(
278-
DartLooseTypeMappers.instance.toMappers,
279-
).map(fieldName: f, fieldTypeCode: s);
280-
return 'final $f0 = $a;';
281-
})
282-
.join('\n');
260+
return insight.fields.map((e) {
261+
final f = e.fieldName!;
262+
final f0 = '${f}0';
263+
final x = e.fieldTypeCode!;
264+
final s = stripSpecialSyntaxFromFieldType(x);
265+
final a = DartTypeCodeMapper(
266+
DartLooseTypeMappers.instance.toMappers,
267+
).map(fieldName: f, fieldTypeCode: s);
268+
return 'final $f0 = $a;';
269+
}).join('\n');
283270
},
284271
'___TO_JSON_ARGS___': (insight) {
285272
final fields = insight.fields.toList();
@@ -294,16 +281,15 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
294281
.toList();
295282
fields.removeWhere((e) => parents.contains(e));
296283
final stringCaseType = insight.stringCaseType;
297-
final entries =
298-
fields
299-
.map(
300-
(e) => MapEntry(
301-
stringCaseType.convertAll(e.fieldPath!).join('.'),
302-
'${e.fieldName}0',
303-
),
304-
)
305-
.toList()
306-
..sort((a, b) => b.key.compareTo(a.key));
284+
final entries = fields
285+
.map(
286+
(e) => MapEntry(
287+
stringCaseType.convertAll(e.fieldPath!).join('.'),
288+
'${e.fieldName}0',
289+
),
290+
)
291+
.toList()
292+
..sort((a, b) => b.key.compareTo(a.key));
307293

308294
var buffer = <String, dynamic>{};
309295

@@ -331,72 +317,59 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
331317
return test.replaceAll('#:', '');
332318
},
333319
'___GETTERS___': (insight) {
334-
return insight.fields
335-
.map((e) {
336-
final f = e.fieldName;
337-
final x = e.fieldTypeCode!;
338-
final s = stripSpecialSyntaxFromFieldType(x);
339-
final n = e.nullable;
340-
return [
341-
' /// Returns the value of the [$f] field.',
342-
' /// If the field is nullable, the return value may be null; otherwise, it',
343-
' /// will always return a non-null value.',
344-
"@pragma('vm:prefer-inline')",
345-
'$s get $f\$ => $f${n ? '' : '!'};',
346-
'',
347-
].join('\n');
348-
})
349-
.join('\n');
320+
return insight.fields.map((e) {
321+
final f = e.fieldName;
322+
final x = e.fieldTypeCode!;
323+
final s = stripSpecialSyntaxFromFieldType(x);
324+
final n = e.nullable;
325+
return [
326+
' /// Returns the value of the [$f] field.',
327+
' /// If the field is nullable, the return value may be null; otherwise, it',
328+
' /// will always return a non-null value.',
329+
"@pragma('vm:prefer-inline')",
330+
'$s get $f\$ => $f${n ? '' : '!'};',
331+
'',
332+
].join('\n');
333+
}).join('\n');
350334
},
351335
'___FIELD_NAMES___': (insight) {
352-
return insight.fields
353-
.map((e) {
354-
final className =
355-
insight.annotation.className ??
356-
insight.className.replaceFirst(RegExp(r'^[_$]+'), '');
357-
final f = e.fieldName;
358-
final c = insight.stringCaseType.convert(e.fieldName!);
359-
return [
360-
' /// The field name of [$className.$f].',
361-
"static const $f = '$c';",
362-
'',
363-
].join('\n');
364-
})
365-
.join('\n');
336+
return insight.fields.map((e) {
337+
final className = insight.annotation.className ??
338+
insight.className.replaceFirst(RegExp(r'^[_$]+'), '');
339+
final f = e.fieldName;
340+
final c = insight.stringCaseType.convert(e.fieldName!);
341+
return [
342+
' /// The field name of [$className.$f].',
343+
"static const $f = '$c';",
344+
'',
345+
].join('\n');
346+
}).join('\n');
366347
},
367348
'___COPY_WITH_PARAMS___': (insight) {
368-
return insight.fields
369-
.map((e) {
370-
final f = e.fieldName;
371-
final t = e.fieldType!;
372-
final r = stripSpecialSyntaxFromFieldType(t);
373-
return '$r? $f,';
374-
})
375-
.join('\n');
349+
return insight.fields.map((e) {
350+
final f = e.fieldName;
351+
final t = e.fieldType!;
352+
final r = stripSpecialSyntaxFromFieldType(t);
353+
return '$r? $f,';
354+
}).join('\n');
376355
},
377356
'___COPY_WITH_ARGS___': (insight) {
378-
return insight.fields
379-
.map((e) {
380-
final f = e.fieldName;
381-
return '$f: $f ?? this.$f,';
382-
})
383-
.join('\n');
357+
return insight.fields.map((e) {
358+
final f = e.fieldName;
359+
return '$f: $f ?? this.$f,';
360+
}).join('\n');
384361
},
385362
'___COPY_WITHOUT_PARAMS___': (insight) {
386-
return insight.fields
387-
.map((e) {
388-
final f = e.fieldName;
389-
return 'bool $f = true,';
390-
})
391-
.join('\n');
363+
return insight.fields.map((e) {
364+
final f = e.fieldName;
365+
return 'bool $f = true,';
366+
}).join('\n');
392367
},
393368
'___COPY_WITHOUT_ARGS___': (insight) {
394-
return insight.fields
395-
.map((e) {
396-
final f = e.fieldName;
397-
return '$f: $f ? this.$f: null,';
398-
})
399-
.join('\n');
369+
return insight.fields.map((e) {
370+
final f = e.fieldName;
371+
return '$f: $f ? this.$f: null,';
372+
}).join('\n');
400373
},
401374
});
402375

0 commit comments

Comments
 (0)