-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoapi-codegen.yaml
More file actions
124 lines (117 loc) · 5.14 KB
/
oapi-codegen.yaml
File metadata and controls
124 lines (117 loc) · 5.14 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
package: api
generate:
models: true
output-options:
initialism-overrides: false
user-templates:
# provide template for union types to avoid pulling in the 'runtime' dependency
union.tmpl: |
{{range .Types}}
{{$typeName := .TypeName -}}
{{$discriminator := .Schema.Discriminator}}
{{$properties := .Schema.Properties -}}
{{range .Schema.UnionElements}}
{{$element := . -}}
// As{{ .Method }} returns the union data inside the {{$typeName}} as a {{.}}
func (t {{$typeName}}) As{{ .Method }}() ({{.}}, error) {
var body {{.}}
err := json.Unmarshal(t.union, &body)
return body, err
}
// From{{ .Method }} overwrites any union data inside the {{$typeName}} as the provided {{.}}
func (t *{{$typeName}}) From{{ .Method }} (v {{.}}) error {
{{if $discriminator -}}
{{range $value, $type := $discriminator.Mapping -}}
{{if eq $type $element -}}
{{$hasProperty := false -}}
{{range $properties -}}
{{if eq .GoFieldName $discriminator.PropertyName -}}
t.{{$discriminator.PropertyName}} = "{{$value}}"
{{$hasProperty = true -}}
{{end -}}
{{end -}}
{{if not $hasProperty}}v.{{$discriminator.PropertyName}} = "{{$value}}"{{end}}
{{end -}}
{{end -}}
{{end -}}
b, err := json.Marshal(v)
t.union = b
return err
}
// Removed `func (t *{{$typeName}}) Merge{{ .Method }} (v {{.}}) error` (see custom oapi.yaml config)
{{end}}
{{if $discriminator}}
func (t {{.TypeName}}) Discriminator() (string, error) {
var discriminator struct {
Discriminator string {{$discriminator.JSONTag}}
}
err := json.Unmarshal(t.union, &discriminator)
return discriminator.Discriminator, err
}
{{if ne 0 (len $discriminator.Mapping)}}
func (t {{.TypeName}}) ValueByDiscriminator() (interface{}, error) {
discriminator, err := t.Discriminator()
if err != nil {
return nil, err
}
switch discriminator{
{{range $value, $type := $discriminator.Mapping -}}
case "{{$value}}":
return t.As{{$type}}()
{{end -}}
default:
return nil, errors.New("unknown discriminator value: "+discriminator)
}
}
{{end}}
{{end}}
{{if not .Schema.HasAdditionalProperties}}
func (t {{.TypeName}}) MarshalJSON() ([]byte, error) {
b, err := t.union.MarshalJSON()
{{if ne 0 (len .Schema.Properties) -}}
if err != nil {
return nil, err
}
object := make(map[string]json.RawMessage)
if t.union != nil {
err = json.Unmarshal(b, &object)
if err != nil {
return nil, err
}
}
{{range .Schema.Properties}}
{{if not .Required}}if t.{{.GoFieldName}} != nil { {{end}}
object["{{.JsonFieldName}}"], err = json.Marshal(t.{{.GoFieldName}})
if err != nil {
return nil, fmt.Errorf("error marshaling '{{.JsonFieldName}}': %w", err)
}
{{if not .Required}} }{{end}}
{{end -}}
b, err = json.Marshal(object)
{{end -}}
return b, err
}
func (t *{{.TypeName}}) UnmarshalJSON(b []byte) error {
err := t.union.UnmarshalJSON(b)
{{if ne 0 (len .Schema.Properties) -}}
if err != nil {
return err
}
object := make(map[string]json.RawMessage)
err = json.Unmarshal(b, &object)
if err != nil {
return err
}
{{range .Schema.Properties}}
if raw, found := object["{{.JsonFieldName}}"]; found {
err = json.Unmarshal(raw, &t.{{.GoFieldName}})
if err != nil {
return fmt.Errorf("error reading '{{.JsonFieldName}}': %w", err)
}
}
{{end}}
{{end -}}
return err
}
{{end}}
{{end}}