forked from rrousselGit/freezed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concrete_template.dart
574 lines (481 loc) · 15.5 KB
/
concrete_template.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:freezed/src/models.dart';
import 'package:freezed/src/templates/properties.dart';
import 'package:freezed/src/tools/type.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:source_gen/source_gen.dart';
import 'copy_with.dart';
import 'parameter_template.dart';
import 'prototypes.dart';
class Concrete {
Concrete({
required this.constructor,
required this.data,
required this.globalData,
required this.commonProperties,
required this.copyWith,
});
final ConstructorDetails constructor;
final List<Property> commonProperties;
final Data data;
final GlobalData globalData;
final CopyWith? copyWith;
String get concreteName {
return '_\$${constructor.redirectedName}';
}
String get nonPrivateConcreteName {
return '\$${constructor.redirectedName}';
}
late final bool _hasUnionKeyProperty =
(data.generateToJson || data.generateFromJson) &&
data.constructors.length > 1 &&
constructor.impliedProperties.every((e) => e.name != data.unionKey);
@override
String toString() {
var jsonSerializable = '';
if (!constructor.hasJsonSerializable) {
if (data.generateFromJson || data.generateToJson) {
final params = [
if (data.generateToJson == false) 'createToJson: false',
if (data.generateFromJson == false) 'createFactory: false',
if (data.genericsParameterTemplate.typeParameters.isNotEmpty &&
data.genericArgumentFactories == true)
'genericArgumentFactories: true',
].join(',');
jsonSerializable = '@JsonSerializable($params)';
}
}
return '''
${copyWith?.interface ?? ''}
${copyWith?.concreteImpl(constructor.parameters) ?? ''}
/// @nodoc
$jsonSerializable
${constructor.decorators.join('\n')}
class $concreteName${data.genericsDefinitionTemplate} $_concreteSuper {
$_concreteConstructor
$_concreteFromJsonConstructor
$_properties
$_toStringMethod
$_debugFillProperties
$_operatorEqualMethod
$_hashCodeMethod
${copyWith?.concreteCopyWithGetter ?? ''}
$_when
$_whenOrNull
$_maybeWhen
$_map
$_mapOrNull
$_maybeMap
$_toJson
}
abstract class ${constructor.redirectedName}${data.genericsDefinitionTemplate} $_superKeyword ${data.name}${data.genericsParameterTemplate}$interfaces {
$_isConst factory ${constructor.redirectedName}(${constructor.parameters.asExpandedDefinition}) = $concreteName${data.genericsParameterTemplate};
$_privateConcreteConstructor
$_redirectedFromJsonConstructor
$_abstractProperties
${copyWith?.abstractCopyWithGetter ?? ''}
}
''';
}
String get _concreteConstructor {
final superConstructor = _superConstructor;
final trailingStrings = <String>[
if (constructor.asserts.isNotEmpty)
...constructor.asserts.map((a) => a.toString()),
if (data.makeCollectionsImmutable)
...constructor.impliedProperties
.where((e) => e.isDartList || e.isDartMap || e.isDartSet)
.map((e) => '_${e.name} = ${e.name}'),
if (_hasUnionKeyProperty)
"\$type = \$type ?? '${constructor.unionValue}'",
if (superConstructor.isNotEmpty) superConstructor,
];
var parameters = constructor.parameters.mapParameters((p) {
if (data.makeCollectionsImmutable &&
(p.isDartList || p.isDartMap || p.isDartSet)) {
return Parameter.fromParameter(p);
}
return LocalParameter.fromParameter(p);
});
if (_hasUnionKeyProperty) {
final typeProperty = Parameter(
type: 'String?',
name: '\$type',
isNullable: true,
isFinal: true,
isDartList: false,
isDartSet: false,
isDartMap: false,
defaultValueSource: null,
isRequired: false,
decorators: [],
doc: '',
isPossiblyDartCollection: false,
showDefaultValue: false,
commonSupertype: null,
commonSubtype: null,
parameterElement: null,
);
parameters = ParametersTemplate(
parameters.requiredPositionalParameters,
optionalPositionalParameters: [
...parameters.optionalPositionalParameters,
if (parameters.optionalPositionalParameters.isNotEmpty) typeProperty,
],
namedParameters: [
...parameters.namedParameters,
if (parameters.optionalPositionalParameters.isEmpty) typeProperty,
],
);
}
var trailing = '';
if (trailingStrings.isNotEmpty) {
trailing = ': ${trailingStrings.join(',')}';
}
return '$_isConst $concreteName($parameters)$trailing;';
}
String get interfaces {
if (constructor.withDecorators.isEmpty &&
constructor.implementsDecorators.isEmpty) {
return '';
}
final interfaces = [
...constructor.implementsDecorators,
...constructor.withDecorators,
].join(', ');
final buffer = StringBuffer();
if (interfaces.isNotEmpty) {
if (data.shouldUseExtends) {
buffer.write(' implements ');
} else {
buffer.write(', ');
}
buffer.write(interfaces);
}
return buffer.toString();
}
String get _superConstructor {
if (!data.shouldUseExtends) return '';
return 'super._()';
}
String get _privateConcreteConstructor {
if (!data.shouldUseExtends) return '';
return '$_isConst ${constructor.redirectedName}._(): super._();';
}
String get _superKeyword {
return data.shouldUseExtends ? 'extends' : 'implements';
}
String get _concreteSuper {
final mixins = [
if (globalData.hasDiagnostics && data.generateToString)
'DiagnosticableTreeMixin',
...constructor.withDecorators,
];
final mixinsStr = mixins.isEmpty ? '' : ' with ${mixins.join(',')}';
if (data.shouldUseExtends) {
return 'extends ${constructor.redirectedName}${data.genericsParameterTemplate} $mixinsStr';
} else {
return '$mixinsStr implements ${constructor.redirectedName}${data.genericsParameterTemplate}';
}
}
String get _properties {
final classProperties = constructor.impliedProperties.expand((p) {
final annotatedProperty = p.copyWith(
decorators: [
'@override',
if (p.defaultValueSource != null && !p.hasJsonKey) '@JsonKey()',
...p.decorators,
],
);
if (data.makeCollectionsImmutable) {
String? viewType;
if (p.isDartList) {
viewType = 'EqualUnmodifiableListView';
} else if (p.isDartMap) {
viewType = 'EqualUnmodifiableMapView';
} else if (p.isDartSet) {
viewType = 'EqualUnmodifiableSetView';
}
if (viewType != null) {
return [
p.copyWith(name: '_${p.name}', decorators: const []),
if (p.isNullable) annotatedProperty.asGetter(''' {
final value = _${p.name};
if (value == null) return null;
// ignore: implicit_dynamic_type
return $viewType(value);
}
''') else annotatedProperty.asGetter(''' {
// ignore: implicit_dynamic_type
return $viewType(_${p.name});
}
'''),
];
}
}
return [annotatedProperty];
});
if (_hasUnionKeyProperty) {
return '''
${classProperties.join('\n')}
@JsonKey(name: '${data.unionKey}')
final String \$type;
''';
}
return classProperties.join('\n');
}
String get _isConst {
return constructor.isConst ? 'const' : '';
}
String get _fromJsonArgs => fromJsonArguments(
data.genericsParameterTemplate, data.genericArgumentFactories);
String get _fromJsonParams => fromJsonParameters(
data.genericsParameterTemplate, data.genericArgumentFactories);
String get _redirectedFromJsonConstructor {
if (!data.generateFromJson) return '';
return 'factory ${constructor.redirectedName}.fromJson(Map<String, dynamic> json$_fromJsonParams)'
' = $concreteName${data.genericsParameterTemplate}.fromJson;';
}
String get _concreteFromJsonConstructor {
if (!data.generateFromJson) return '';
return 'factory $concreteName.fromJson(Map<String, dynamic> json$_fromJsonParams)'
' => _\$${nonPrivateConcreteName}FromJson(json$_fromJsonArgs);';
}
String get _toJsonParams => toJsonParameters(
data.genericsParameterTemplate, data.genericArgumentFactories);
String get _toJsonArgs => toJsonArguments(
data.genericsParameterTemplate, data.genericArgumentFactories);
String get _toJson {
if (!data.generateToJson) return '';
return '''
@override
Map<String, dynamic> toJson($_toJsonParams) {
return _\$${nonPrivateConcreteName}ToJson${data.genericsParameterTemplate}(this, $_toJsonArgs);
}''';
}
String get _debugFillProperties {
if (!globalData.hasDiagnostics || !data.generateToString) return '';
final diagnostics = [
for (final e in constructor.impliedProperties)
"..add(DiagnosticsProperty('${e.name}', ${e.name}))",
].join();
return '''
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty('type', '${constructor.escapedName}'))
$diagnostics;
}
''';
}
String get _maybeMap {
if (!data.map.maybeMap) return '';
return '''
@override
${maybeMapPrototype(data.constructors, data.genericsParameterTemplate)} {
if (${constructor.callbackName} != null) {
return ${constructor.callbackName}(this);
}
return orElse();
}''';
}
String get _map {
if (!data.map.map) return '';
return '''
@override
${mapPrototype(data.constructors, data.genericsParameterTemplate)} {
return ${constructor.callbackName}(this);
}''';
}
String get _mapOrNull {
if (!data.map.mapOrNull) return '';
return '''
@override
${mapOrNullPrototype(data.constructors, data.genericsParameterTemplate)} {
return ${constructor.callbackName}?.call(this);
}''';
}
String get _maybeWhen {
if (!data.when.maybeWhen) return '';
var callbackParameters = constructor.impliedProperties.map((e) {
if (data.constructors.any((c) => c.callbackName == e.name)) {
return 'this.${e.name}';
}
return e.name;
}).join(',');
return '''
@override
${maybeWhenPrototype(data.constructors)} {
if (${constructor.callbackName} != null) {
return ${constructor.callbackName}($callbackParameters);
}
return orElse();
}''';
}
String get _when {
if (!data.when.when) return '';
var callbackParameters = constructor.impliedProperties.map((e) {
if (data.constructors.any((c) => c.callbackName == e.name)) {
return 'this.${e.name}';
}
return e.name;
}).join(',');
return '''
@override
${whenPrototype(data.constructors)} {
return ${constructor.callbackName}($callbackParameters);
}''';
}
String get _whenOrNull {
if (!data.when.whenOrNull) return '';
var callbackParameters = constructor.impliedProperties.map((e) {
if (data.constructors.any((c) => c.callbackName == e.name)) {
return 'this.${e.name}';
}
return e.name;
}).join(',');
return '''
@override
${whenOrNullPrototype(data.constructors)} {
return ${constructor.callbackName}?.call($callbackParameters);
}''';
}
String get _abstractProperties {
return constructor.impliedProperties.expand((p) {
return [
if (commonProperties.any((element) => element.name == p.name))
'@override ${p.abstractGetter}'
else
'${p.abstractGetter}',
if (!p.isFinal) p.abstractSetter,
];
}).join();
}
String get _toStringMethod {
if (!data.generateToString) return '';
final parameters = globalData.hasDiagnostics
? '{ DiagnosticLevel minLevel = DiagnosticLevel.info }'
: '';
final properties = [
for (final p in constructor.impliedProperties) '${p.name}: \$${p.name}',
];
return '''
@override
String toString($parameters) {
return '${constructor.escapedName}(${properties.join(', ')})';
}
''';
}
String get _operatorEqualMethod {
if (!data.generateEqual) return '';
final comparisons = [
'other.runtimeType == runtimeType',
'other is $concreteName${data.genericsParameterTemplate}',
...constructor.impliedProperties.map((p) {
var name = p.name;
if (p.isPossiblyDartCollection) {
if (data.makeCollectionsImmutable &&
(p.isDartList || p.isDartMap || p.isDartSet)) {
name = '_$name';
}
}
final target = p.name == 'other' ? 'this.' : '';
if (p.isPossiblyDartCollection) {
// no need to check `identical` as `DeepCollectionEquality` already does it
return 'const DeepCollectionEquality().equals(other.$name, $target$name)';
}
return '(identical(other.${p.name}, $target$name) || other.$name == $target$name)';
}),
];
return '''
@override
bool operator ==(dynamic other) {
return identical(this, other) || (${comparisons.join('&&')});
}
''';
}
String get _hashCodeMethod {
if (!data.generateEqual) return '';
final jsonKey = data.generateFromJson || data.generateToJson
? '@JsonKey(ignore: true)'
: '';
final hashedProperties = [
'runtimeType',
for (final property in constructor.impliedProperties)
if (property.isPossiblyDartCollection)
if (data.makeCollectionsImmutable &&
(property.isDartList || property.isDartMap || property.isDartSet))
'const DeepCollectionEquality().hash(_${property.name})'
else
'const DeepCollectionEquality().hash(${property.name})'
else
property.name,
];
if (hashedProperties.length == 1) {
return '''
$jsonKey
@override
int get hashCode => ${hashedProperties.first}.hashCode;
''';
}
if (hashedProperties.length >= 20) {
return '''
$jsonKey
@override
int get hashCode => Object.hashAll([${hashedProperties.join(',')}]);
''';
}
return '''
$jsonKey
@override
int get hashCode => Object.hash(${hashedProperties.join(',')});
''';
}
}
extension DefaultValue on ParameterElement {
/// Returns the sources of the default value associated with a `@Default`,
/// or `null` if no `@Default` are specified.
String? get defaultValue {
const matcher = TypeChecker.fromRuntime(Default);
for (final meta in metadata) {
final obj = meta.computeConstantValue()!;
if (matcher.isExactlyType(obj.type!)) {
final source = meta.toSource();
final res = source.substring('@Default('.length, source.length - 1);
var needsConstModifier = !res.trimLeft().startsWith('const') &&
(res.contains('(') || res.contains('[') || res.contains('{'));
if (needsConstModifier) {
return 'const $res';
} else {
return res;
}
}
}
return null;
}
bool get hasJsonKey {
return const TypeChecker.fromRuntime(JsonKey).hasAnnotationOf(this);
}
}
String? parseTypeSource(VariableElement element) {
String? type = element.type.getDisplayString(withNullability: true);
if (type.contains('dynamic') && element.nameOffset > 0) {
final source =
element.source!.contents.data.substring(0, element.nameOffset);
final eleType = element.type;
if (eleType is DynamicType) {
final match = RegExp(r'(\w+\??)\s+$').firstMatch(source);
return match?.group(1);
} else if (eleType is InterfaceType) {
final match = RegExp(r'(\w+<.+?>\??)\s+$').firstMatch(source);
return match?.group(1) ?? type;
}
}
return resolveFullTypeStringFrom(
element.library!,
element.type,
withNullability: true,
);
}