-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go.tmpl
160 lines (140 loc) · 4.53 KB
/
types.go.tmpl
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
{{define "types"}}
{{- $typeMap := .TypeMap -}}
{{- $types := .Types -}}
{{- $services := .Services -}}
{{if $types -}}
{{range $_i, $type := $types -}}
{{if isEnumType $type }}
enum {{$type.Name}} {
{{- range $i, $field := $type.Fields}}
{{- if $i}},{{end}}
{{$field.Name}}
{{- end}};
factory {{.Name}}.fromJson(dynamic json) {
switch (json) {
{{- range $i, $field := $type.Fields}}
case '{{$field.Name}}':
return {{$type.Name}}.{{$field.Name}};
{{- end}}
default:
throw ArgumentError.value(json);
}
}
String toJson() {
return name;
}
}
{{end -}}
{{- if isStructType $type }}
class {{$type.Name}} implements JsonSerializable {
{{$type.Name}}({
{{- range $i, $field := $type.Fields}}
{{- if $i}},{{end}}
required this.{{template "fieldName" dict "Field" .}}
{{- end}}
});
{{range $_, $field := $type.Fields}}
{{- $isExportable := true -}}
{{- range $meta := $field.Meta -}}
{{- if exists $meta "json" -}}
{{- if eq (printf "%v" (get $meta "json")) "-" -}}
{{- $isExportable = false}}
{{- end -}}
{{- end -}}
{{- end }}
{{- if $isExportable }}
final {{template "type" dict "Type" $field.Type "TypeMap" $typeMap}}{{if .Optional}}?{{end}} {{template "fieldName" dict "Field" .}};
{{- end -}}
{{- end}}
{{.Name}}.fromJson(Map<String, dynamic> json)
: {{range $i, $field := $type.Fields}}
{{- template "fieldName" dict "Field" .}} = _{{.Name}}(json['{{template "fieldName" dict "Field" .}}'])
{{- if lt $i (lastIndex $type.Fields)}},{{else}};{{end}}
{{end -}}
{{range $i, $field := $type.Fields}}
static {{template "type" dict "Type" $field.Type "TypeMap" $typeMap}}{{if .Optional}}?{{end}} _{{.Name}}(dynamic v0) {
{{- if (and (isCoreType $field.Type) (eq (get $typeMap $field.Type) "Null"))}}
return null;
{{- else}}
{{- if .Optional}}
if (v0 == null) return null;
{{- else}}
if (v0 == null) throw WebrpcException.fromCode(ErrorId.webrpcBadResponse.code);
{{- end}}
{{- template "fromJson" dict "Type" $field.Type "TypeMap" $typeMap "Optional" .Optional}}
return r0;
{{- end}}
}
{{end -}}
{{if true}}{{end}}
@override
Map<String, dynamic> toJson() {
return {
{{- range $i, $field := $type.Fields}}
'{{- template "fieldName" dict "Field" .}}': toJsonObject({{template "fieldName" dict "Field" .}}),
{{- end}}
};
}
}
{{end -}}
{{end -}}
{{end}}
T _cast<T>(x) {
if ((x == null) && (null is T)) {
return x;
} else if (x is T) {
return x;
} else {
throw ArgumentError.value(x);
}
}
dynamic toJsonObject(dynamic v) {
if (v == null) return null;
if (v is DateTime) return v.toIso8601String();
if (v is BigInt) return v.toString();
// records are impossible to JSON serialize accurately because they do not
// retain runtime info about their structure
// see https://github.com/dart-lang/language/issues/2826
if (v is Record) return v.toString();
if (v is List) return v.map(toJsonObject).toList();
if (v is Map) return v.map((key, value) => MapEntry(key.toString(), toJsonObject(value)));
if (v is JsonSerializable) return v.toJson();
return v;
}
DateTime? _dateTimeFromJsonOptional(dynamic v0) {
if (v0 == null) return null;
return _dateTimeFromJson(v0);
}
DateTime _dateTimeFromJson(dynamic v0) {
if ((v0 != null) && (v0 is String)) {
return DateTime.parse(v0);
} else {
throw ArgumentError.value(v0, "v0", "Cannot parse to DateTime");
}
}
BigInt? _bigIntFromJsonOptional(dynamic v0) {
if (v0 == null) return null;
return _bigIntFromJson(v0);
}
BigInt _bigIntFromJson(dynamic v0) {
if (v0 is String) {
return BigInt.parse(v0);
} else if (v0 is int) {
return BigInt.from(v0);
} else {
throw ArgumentError.value(v0, "v0", "Required non-null BigInt");
}
}
abstract interface class JsonSerializable {
dynamic toJson();
}
{{if $services}}
{{- range $_, $service := $services}}
abstract interface class {{$service.Name}} {
{{- range $_, $method := $service.Methods}}
Future<{{template "methodOutputs" dict "Method" . "TypeMap" $typeMap}}> {{firstLetterToLower .Name}}({{template "methodInputs" dict "Method" . "TypeMap" $typeMap}});
{{- end}}
}
{{- end}}
{{end -}}
{{end}}