/
fromJson.go.tmpl
48 lines (42 loc) · 2.48 KB
/
fromJson.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
{{define "fromJson"}}
{{- /* Recursively builds objects from dynamic json */ -}}
{{- $type := .Type}}
{{- $typeMap := .TypeMap}}
{{- $depth := (or .Depth 0)}}
{{- $inPrefix := (or .InPrefix "v")}}
{{- $outPrefix := (or .OutPrefix "r")}}
{{- $optional := (or .Optional false)}}
{{- $nIndents := (int (add $depth 2))}}
{{- $ind := (repeat $nIndents " ")}}
{{- if isMapType $type}}
{{$ind}}final {{template "type" dict "Type" $type "TypeMap" $typeMap}} {{$outPrefix}}{{$depth}} = {};
{{$ind}}for (MapEntry e{{$depth}} in v{{$depth}}.entries) {
{{$ind}} final dynamic v{{add $depth 1}} = e{{$depth}}.value;
{{/* JSON requires all object keys to be strings, so we must double decode any non-string map keys */}}
{{- if eq (get $typeMap (mapKeyType $type)) "String"}}
{{$ind}} final String k{{add $depth 1}} = e{{$depth}}.key;
{{- else}}
{{$ind}} final dynamic k{{add $depth 1}} = jsonDecode(e{{$depth}}.key);
{{- end}}
{{$ind}} {{- template "fromJson" dict "Type" (mapValueType $type) "TypeMap" $typeMap "Depth" (add $depth 1) "Optional" false "InPrefix" "v" "OutPrefix" "rv"}}
{{$ind}} {{- template "fromJson" dict "Type" (mapKeyType $type) "TypeMap" $typeMap "Depth" (add $depth 1) "Optional" false "InPrefix" "k" "OutPrefix" "rk"}}
{{$ind}} {{$outPrefix}}{{$depth}}[rk{{add $depth 1}}] = rv{{add $depth 1}};
{{$ind}}}
{{- else if isListType $type}}
{{$ind}}final {{template "type" dict "Type" $type "TypeMap" $typeMap}} {{$outPrefix}}{{$depth}} = [];
{{$ind}}for (dynamic {{$inPrefix}}{{add $depth 1}} in {{$inPrefix}}{{$depth}}) {
{{$ind}} {{- template "fromJson" dict "Type" (listElemType $type) "TypeMap" $typeMap "Depth" (add $depth 1) "Optional" false "InPrefix" $inPrefix "OutPrefix" $outPrefix}}
{{$ind}} {{$outPrefix}}{{$depth}}.add({{$outPrefix}}{{add $depth 1}});
{{$ind}}}
{{- else if isCoreType $type}}
{{- if (eq (get $typeMap $type) "DateTime")}}
{{$ind}}final {{$outPrefix}}{{$depth}} = {{ternary .Optional "_dateTimeFromJsonOptional" "_dateTimeFromJson"}}(v{{$depth}});
{{- else if (eq (get $typeMap $type) "BigInt")}}
{{$ind}}final {{$outPrefix}}{{$depth}} = {{ternary .Optional "_bigIntFromJsonOptional" "_bigIntFromJson"}}(v{{$depth}});
{{- else}}
{{$ind}}final {{get $typeMap $type}}{{if $optional}}?{{end}} {{$outPrefix}}{{$depth}} = _cast<{{get $typeMap $type}}{{if $optional}}?{{end}}>({{$inPrefix}}{{$depth}});
{{- end}}
{{- else}}
{{$ind}}final {{$type}} {{$outPrefix}}{{$depth}} = {{$type}}.fromJson({{$inPrefix}}{{$depth}});
{{- end -}}
{{- end -}}