-
Notifications
You must be signed in to change notification settings - Fork 84
/
encode.tmpl
119 lines (108 loc) · 3 KB
/
encode.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
{{- /*gotype: github.com/ogen-go/ogen/gen.Elem*/ -}}
{{/* Encode field */}}
{{- define "json/enc_field" -}}
{{- if $.Tag.JSON }}
e.FieldStart({{ $.Tag.EscapedJSON }})
{{- end }}
{{- end -}}
{{/* Encode generic with respect of nullable/optional and special format */}}
{{- define "json/enc_generic" -}}
{{- $g := $.Type.GenericOf -}}
{{- $v := $.Type.GenericVariant -}}
{{- if $v.Optional -}}
if {{ $.Var }}.Set {
{{- template "json/enc_generic_field" $ }}
}
{{- else }}
{{- template "json/enc_generic_field" $ }}
{{- end -}}
{{- end }}
{{- define "json/enc_generic_field" -}}
{{- $g := $.Type.GenericOf -}}
{{- $v := $.Type.GenericVariant -}}
{{- template "json/enc_field" $ }}
{{- if $g.Format }}
{{ $.Var }}.Encode(e, json.Encode{{ $g.JSON.Format }})
{{- else }}
{{ $.Var }}.Encode(e)
{{- end }}
{{- end }}
{{/* Encode array with all elements */}}
{{- define "json/enc_array_elems" }}
e.ArrStart()
for _, elem := range {{ $.Var }} {
{{- template "json/enc" array_elem $.Type.Item }}
}
e.ArrEnd()
{{- end }}
{{/* Encode array with respect for nil semantic */}}
{{- define "json/enc_array" -}}
{{- $t := $.Type }}
{{- if $t.NilSemantic.Invalid -}}
{{- template "json/enc_field" $ -}}
{{- template "json/enc_array_elems" $ -}}
{{- else if $t.NilSemantic.Optional -}}
if {{ $.Var }} != nil {
{{- template "json/enc_field" $ -}}
{{- template "json/enc_array_elems" $ -}}
}
{{- else if $t.NilSemantic.Null -}}
{{- template "json/enc_field" $ }}
if {{ $.Var }} == nil {
e.Null()
} else {
{{- template "json/enc_array_elems" $ -}}
}
{{- else -}}
{{ errorf "unexpected nil semantic %s" $t.NilSemantic }}
{{- end -}}
{{- end }}
{{/* Encode value that implements jx.Encoder with respect to nil semantic */}}
{{- define "json/enc_value" -}}
{{- $t := $.Type -}}
{{- if or ($t.NilSemantic.Invalid) ($t.NilSemantic.Optional) -}}
if {{ $.Var }} != nil {
{{- template "json/enc_field" $ }}
{{ $.Var }}.Encode(e)
}
{{- else if $t.NilSemantic.Null -}}
{{- template "json/enc_field" $ }}
if {{ $.Var }} == nil {
e.Null()
} else {
{{ $.Var }}.Encode(e)
}
{{- else -}}
{{- template "json/enc_field" $ }}
{{ $.Var }}.Encode(e)
{{- end -}}
{{- end }}
{{/* Encode any Elem as json */}}
{{- define "json/enc" -}}
{{- $t := $.Type }}
{{- $j := $t.JSON }}
{{- if or ($t.IsStruct) ($t.IsMap) ($t.IsEnum) ($t.IsPointer) ($t.IsSum) ($t.IsAlias) -}}
{{- template "json/enc_value" $ }}
{{- else if $t.IsGeneric -}}
{{- template "json/enc_generic" $ }}
{{- else if $t.IsArray -}}
{{- template "json/enc_array" $ }}
{{- else if $t.IsAny }}
if len({{ $.Var }}) != 0 {
{{- template "json/enc_field" $ }}
e.{{ $j.Fn }}({{ $.Var }})
}
{{- else if $t.IsNull }}
_ = {{ $.Var }}
{{- template "json/enc_field" $ }}
e.Null()
{{- else if $j.Format -}}
{{- template "json/enc_field" $ }}
json.Encode{{ $j.Format }}(e, {{ $.Var }})
{{- else if $j.Fn -}}
{{- template "json/enc_field" $ }}
e.{{ $j.Fn }}({{ $.Var }})
{{- else -}}
{{ errorf "unsupported: %s" $t }}
{{- end }}
{{- end -}}