forked from rrousselGit/freezed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_template.dart
94 lines (79 loc) · 2.5 KB
/
abstract_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
import 'package:freezed/src/models.dart';
import 'copy_with.dart';
import 'properties.dart';
import 'prototypes.dart';
class Abstract {
Abstract({
required this.data,
required this.copyWith,
required this.commonProperties,
});
final Data data;
final CopyWith? copyWith;
final List<Property> commonProperties;
@override
String toString() {
final abstractProperties = commonProperties
.expand((e) {
return <ClassMember?>[
e.unimplementedGetter,
if (!e.isFinal &&
// Don't add a setter for a field where the setters type is not
// a subtype of the getters type.
!(e.commonSupertype != null &&
e.commonSubtype != e.commonSupertype))
e.unimplementedSetter,
];
})
.whereType<ClassMember>()
.map((e) => e.toString())
.join();
return '''
/// @nodoc
mixin _\$${data.name}${data.genericsDefinitionTemplate} {
$abstractProperties
$_when
$_whenOrNull
$_maybeWhen
$_map
$_mapOrNull
$_maybeMap
$_toJson
${copyWith?.abstractCopyWithGetter ?? ''}
}
${copyWith?.interface ?? ''}
${copyWith?.commonContreteImpl ?? ''}
''';
}
String get _toJsonParams => toJsonParameters(
data.genericsParameterTemplate, data.genericArgumentFactories);
String get _toJson {
if (!data.generateToJson) return '';
return 'Map<String, dynamic> toJson($_toJsonParams)'
' => throw $privConstUsedErrorVarName;';
}
String get _when {
if (!data.when.when) return '';
return '${whenPrototype(data.constructors)} => throw $privConstUsedErrorVarName;';
}
String get _whenOrNull {
if (!data.when.whenOrNull) return '';
return '${whenOrNullPrototype(data.constructors)} => throw $privConstUsedErrorVarName;';
}
String get _maybeWhen {
if (!data.when.maybeWhen) return '';
return '${maybeWhenPrototype(data.constructors)} => throw $privConstUsedErrorVarName;';
}
String get _map {
if (!data.map.map) return '';
return '${mapPrototype(data.constructors, data.genericsParameterTemplate)} => throw $privConstUsedErrorVarName;';
}
String get _mapOrNull {
if (!data.map.mapOrNull) return '';
return '${mapOrNullPrototype(data.constructors, data.genericsParameterTemplate)} => throw $privConstUsedErrorVarName;';
}
String get _maybeMap {
if (!data.map.maybeMap) return '';
return '${maybeMapPrototype(data.constructors, data.genericsParameterTemplate)} => throw $privConstUsedErrorVarName;';
}
}