@@ -70,7 +70,9 @@ Future<void> generateDartModels(
7070 dartSdk = argResults.option (DefaultOptionParams .DART_SDK .name);
7171 outputFileNamePattern = argResults.option (OUTPUT_FILE_NAME_PATTERN .name)! ;
7272 } catch (_) {
73- Log .printRed ('Missing required args! Use --help flag for more information.' );
73+ Log .printRed (
74+ 'Missing required args! Use --help flag for more information.' ,
75+ );
7476
7577 exit (ExitCodes .FAILURE .code);
7678 }
@@ -79,7 +81,9 @@ Future<void> generateDartModels(
7981 dartSdk, //
8082 );
8183 Log .printWhite ('Reading template at: $templatePathOrUrl ...' );
82- final result = await MdTemplateUtility .i.readTemplateFromPathOrUrl (templatePathOrUrl).value;
84+ final result = await MdTemplateUtility .i
85+ .readTemplateFromPathOrUrl (templatePathOrUrl)
86+ .value;
8387 if (result.isErr ()) {
8488 Log .printRed (' Failed to read template!' );
8589 exit (ExitCodes .FAILURE .code);
@@ -147,70 +151,86 @@ bool _isAllowedFileName(String e) {
147151
148152final _interpolator = TemplateInterpolator <ClassInsight <GenerateDartModel >>({
149153 '___DESCRIPTION___' : (insight) {
150- return insight.annotation.description ?? 'Generated class for [${insight .className }].' ;
154+ return insight.annotation.description ??
155+ 'Generated class for [${insight .className }].' ;
151156 },
152157 '___SUPER_CLASS_NAME___' : (insight) {
153- return insight.annotation.shouldInherit == true ? insight.className : 'Model' ;
158+ return insight.annotation.shouldInherit == true
159+ ? insight.className
160+ : 'Model' ;
154161 },
155162 '___CLASS_FILE_NAME___' : (insight) {
156163 return insight.fileName;
157164 },
158165 '___CLASS_NAME___' : (insight) {
159- return insight.annotation.className ?? insight.className.replaceFirst (RegExp (r'^[_$]+' ), '' );
166+ return insight.annotation.className ??
167+ insight.className.replaceFirst (RegExp (r'^[_$]+' ), '' );
160168 },
161169 '___SUPER_CONSTRUCTOR___' : (insight) {
162170 return insight.annotation.shouldInherit == true
163171 ? insight.annotation.inheritanceConstructor? .nullIfEmpty != null
164- ? ': super.${insight .annotation .inheritanceConstructor }()'
165- : ''
172+ ? ': super.${insight .annotation .inheritanceConstructor }()'
173+ : ''
166174 : '' ;
167175 },
168176 '___FIELD_DECLARATIONS___' : (insight) {
169- return insight.fields.map ((e) {
170- final description = e.description;
171- final t = e.fieldType! ;
172- final r = stripSpecialSyntaxFromFieldType (t);
173- final f = e.fieldName;
174- return [
175- ' /// ${description ?? 'No description provided.' }' ,
176- 'final $r ? $f ;' ,
177- '' ,
178- ].join ('\n ' );
179- }).join ('\n ' );
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 ' );
180190 },
181191 '___PARAMS___' : (insight) {
182- return insight.fields.map ((e) {
183- final n = e.nullable;
184- final f = e.fieldName;
185- return '${n ? '' : 'required' } this.$f ,' ;
186- }).join ('\n ' );
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 ' );
187199 },
188200 '___PERMISSIVE_PARAMS___' : (insight) {
189- return insight.fields.map ((e) {
190- final f = e.fieldName;
191- return 'this.$f ,' ;
192- }).join ('\n ' );
201+ return insight.fields
202+ .map ((e) {
203+ final f = e.fieldName;
204+ return 'this.$f ,' ;
205+ })
206+ .join ('\n ' );
193207 },
194208 '___STRICT_PARAMS___' : (insight) {
195- return insight.fields.map ((e) {
196- final t = e.fieldType! ;
197- final r = stripSpecialSyntaxFromFieldType (t);
198- final f = e.fieldName;
199- return '$r ? $f ,' ;
200- }).join ('\n ' );
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 ' );
201217 },
202218 '___STRICT_PARAM_ASSERTIONS___' : (insight) {
203- return insight.fields.map ((e) {
204- final n = e.nullable;
205- final f = e.fieldName;
206- return n ? '' : 'assert($f != null);' ;
207- }).join ('\n ' );
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 ' );
208226 },
209227 '___STRICT_ARGS___' : (insight) {
210- return insight.fields.map ((e) {
211- final f = e.fieldName;
212- return '$f : $f ,' ;
213- }).join ('\n ' );
228+ return insight.fields
229+ .map ((e) {
230+ final f = e.fieldName;
231+ return '$f : $f ,' ;
232+ })
233+ .join ('\n ' );
214234 },
215235 '___FROM_JSON_OR_NULL_PARAMS___' : (insight) {
216236 final fields = insight.fields.toList ();
@@ -230,7 +250,8 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
230250 final j = fields.map ((a) {
231251 final ff = fields
232252 .where (
233- (b) => a.fieldPath! .join ('.' ).startsWith ('${b .fieldPath !.join ('.' )}.' ),
253+ (b) =>
254+ a.fieldPath! .join ('.' ).startsWith ('${b .fieldPath !.join ('.' )}.' ),
234255 )
235256 .toList ();
236257 ff.sort ((a, b) => b.fieldName! .compareTo (a.fieldName! ));
@@ -239,22 +260,26 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
239260 return j.join ('\n ' );
240261 },
241262 '___FROM_JSON_OR_NULL_ARGS___' : (insight) {
242- return insight.fields.map ((e) {
243- final f = e.fieldName;
244- return '$f : $f ,' ;
245- }).join ('\n ' );
263+ return insight.fields
264+ .map ((e) {
265+ final f = e.fieldName;
266+ return '$f : $f ,' ;
267+ })
268+ .join ('\n ' );
246269 },
247270 '___TO_JSON_PARAMS___' : (insight) {
248- return insight.fields.map ((e) {
249- final f = e.fieldName! ;
250- final f0 = '${f }0' ;
251- final x = e.fieldTypeCode! ;
252- final s = stripSpecialSyntaxFromFieldType (x);
253- final a = DartTypeCodeMapper (
254- DartLooseTypeMappers .instance.toMappers,
255- ).map (fieldName: f, fieldTypeCode: s);
256- return 'final $f0 = $a ;' ;
257- }).join ('\n ' );
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 ' );
258283 },
259284 '___TO_JSON_ARGS___' : (insight) {
260285 final fields = insight.fields.toList ();
@@ -269,15 +294,16 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
269294 .toList ();
270295 fields.removeWhere ((e) => parents.contains (e));
271296 final stringCaseType = insight.stringCaseType;
272- final entries = fields
273- .map (
274- (e) => MapEntry (
275- stringCaseType.convertAll (e.fieldPath! ).join ('.' ),
276- '${e .fieldName }0' ,
277- ),
278- )
279- .toList ()
280- ..sort ((a, b) => b.key.compareTo (a.key));
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));
281307
282308 var buffer = < String , dynamic > {};
283309
@@ -305,59 +331,72 @@ final _interpolator = TemplateInterpolator<ClassInsight<GenerateDartModel>>({
305331 return test.replaceAll ('#:' , '' );
306332 },
307333 '___GETTERS___' : (insight) {
308- return insight.fields.map ((e) {
309- final f = e.fieldName;
310- final x = e.fieldTypeCode! ;
311- final s = stripSpecialSyntaxFromFieldType (x);
312- final n = e.nullable;
313- return [
314- ' /// Returns the value of the [$f ] field.' ,
315- ' /// If the field is nullable, the return value may be null; otherwise, it' ,
316- ' /// will always return a non-null value.' ,
317- "@pragma('vm:prefer-inline')" ,
318- '$s get $f \$ => $f ${n ? '' : '!' };' ,
319- '' ,
320- ].join ('\n ' );
321- }).join ('\n ' );
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 ' );
322350 },
323351 '___FIELD_NAMES___' : (insight) {
324- return insight.fields.map ((e) {
325- final className =
326- insight.annotation.className ?? insight.className.replaceFirst (RegExp (r'^[_$]+' ), '' );
327- final f = e.fieldName;
328- final c = insight.stringCaseType.convert (e.fieldName! );
329- return [
330- ' /// The field name of [$className .$f ].' ,
331- "static const $f = '$c ';" ,
332- '' ,
333- ].join ('\n ' );
334- }).join ('\n ' );
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 ' );
335366 },
336367 '___COPY_WITH_PARAMS___' : (insight) {
337- return insight.fields.map ((e) {
338- final f = e.fieldName;
339- final t = e.fieldType! ;
340- final r = stripSpecialSyntaxFromFieldType (t);
341- return '$r ? $f ,' ;
342- }).join ('\n ' );
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 ' );
343376 },
344377 '___COPY_WITH_ARGS___' : (insight) {
345- return insight.fields.map ((e) {
346- final f = e.fieldName;
347- return '$f : $f ?? this.$f ,' ;
348- }).join ('\n ' );
378+ return insight.fields
379+ .map ((e) {
380+ final f = e.fieldName;
381+ return '$f : $f ?? this.$f ,' ;
382+ })
383+ .join ('\n ' );
349384 },
350385 '___COPY_WITHOUT_PARAMS___' : (insight) {
351- return insight.fields.map ((e) {
352- final f = e.fieldName;
353- return 'bool $f = true,' ;
354- }).join ('\n ' );
386+ return insight.fields
387+ .map ((e) {
388+ final f = e.fieldName;
389+ return 'bool $f = true,' ;
390+ })
391+ .join ('\n ' );
355392 },
356393 '___COPY_WITHOUT_ARGS___' : (insight) {
357- return insight.fields.map ((e) {
358- final f = e.fieldName;
359- return '$f : $f ? this.$f : null,' ;
360- }).join ('\n ' );
394+ return insight.fields
395+ .map ((e) {
396+ final f = e.fieldName;
397+ return '$f : $f ? this.$f : null,' ;
398+ })
399+ .join ('\n ' );
361400 },
362401});
363402
@@ -371,6 +410,7 @@ extension _ClassInsightExtension on ClassInsight<GenerateDartModel> {
371410 }
372411
373412 StringCaseType get stringCaseType {
374- return StringCaseType .values.valueOf (annotation.keyStringCase) ?? StringCaseType .CAMEL_CASE ;
413+ return StringCaseType .values.valueOf (annotation.keyStringCase) ??
414+ StringCaseType .CAMEL_CASE ;
375415 }
376416}
0 commit comments