This repository was archived by the owner on Oct 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathannotation.dart
More file actions
158 lines (132 loc) · 3.97 KB
/
annotation.dart
File metadata and controls
158 lines (132 loc) · 3.97 KB
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
import 'package:artemis/generator/data_printer.dart';
import 'package:artemis/generator/data/definition.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
abstract class Annotation extends Equatable with DataPrinter {
String toAnnotation();
}
class JsonKeyItem extends Equatable with DataPrinter {
final String? defaultValue;
final String? disallowNullValue;
final String? fromJson;
final String? ignore;
final String? includeIfNull;
final String? name;
final String? required;
final String? toJson;
final String? unknownEnumValue;
const JsonKeyItem({
this.defaultValue,
this.disallowNullValue,
this.fromJson,
this.ignore,
this.includeIfNull,
this.name,
this.required,
this.toJson,
this.unknownEnumValue,
});
@override
Map<String, Object?> get namedProps {
return {
'defaultValue': defaultValue,
'disallowNullValue': disallowNullValue,
'fromJson': fromJson,
'ignore': ignore,
'includeIfNull': includeIfNull,
'name': name,
'required': required,
'toJson': toJson,
'unknownEnumValue': unknownEnumValue,
};
}
JsonKeyItem copyWith({
String? defaultValue,
String? disallowNullValue,
String? fromJson,
String? ignore,
String? includeIfNull,
String? name,
String? required,
String? toJson,
String? unknownEnumValue,
}) {
return JsonKeyItem(
defaultValue: defaultValue ?? this.defaultValue,
disallowNullValue: disallowNullValue ?? this.disallowNullValue,
fromJson: fromJson ?? this.fromJson,
ignore: ignore ?? this.ignore,
includeIfNull: includeIfNull ?? this.includeIfNull,
name: name ?? this.name,
required: required ?? this.required,
toJson: toJson ?? this.toJson,
unknownEnumValue: unknownEnumValue ?? this.unknownEnumValue,
);
}
}
class StringAnnotation extends Annotation {
final String name;
StringAnnotation({required this.name});
@override
String toAnnotation() => name;
@override
Map<String, Object?> get namedProps => {
'name': name,
};
}
class OverrideAnnotation extends StringAnnotation {
OverrideAnnotation() : super(name: 'override');
}
class JsonKeyAnnotation extends Annotation {
final JsonKeyItem jsonKey;
JsonKeyAnnotation({required this.jsonKey});
factory JsonKeyAnnotation.fromMap(Map<String, dynamic> jsonKeyAnnotation) {
return JsonKeyAnnotation(
jsonKey: JsonKeyItem(
defaultValue: jsonKeyAnnotation['defaultValue'] as String?,
disallowNullValue: jsonKeyAnnotation['disallowNullValue'] as String?,
fromJson: jsonKeyAnnotation['fromJson'] as String?,
ignore: jsonKeyAnnotation['ignore'] as String?,
includeIfNull: jsonKeyAnnotation['includeIfNull'] as String?,
name: jsonKeyAnnotation['name'] as String?,
required: jsonKeyAnnotation['required'] as String?,
toJson: jsonKeyAnnotation['toJson'] as String?,
unknownEnumValue: jsonKeyAnnotation['unknownEnumValue'] as String?,
),
);
}
@override
String toAnnotation() {
final jsonKeyAnnotation = <String, dynamic>{
'disallowNullValue': jsonKey.disallowNullValue,
'fromJson': jsonKey.fromJson,
'ignore': jsonKey.ignore,
'includeIfNull': jsonKey.includeIfNull,
'name': jsonKey.name,
'required': jsonKey.required,
'toJson': jsonKey.toJson,
'unknownEnumValue': jsonKey.unknownEnumValue
};
final key = jsonKeyAnnotation.entries
.map<String?>((e) {
if (e.value != null) {
switch (e.key) {
case 'name':
return '${e.key}: \'${e.value}\'';
default:
return '${e.key}: ${e.value}';
}
}
return null;
})
.whereType<String>()
.join(', ');
return 'JsonKey($key)';
}
@override
Map<String, Object?> get namedProps {
return {
'jsonKey': jsonKey,
};
}
}