Repository navigation
Expand file tree
/
Copy pathstructtags.go
More file actions
217 lines (203 loc) · 8.12 KB
/
Copy pathstructtags.go
File metadata and controls
217 lines (203 loc) · 8.12 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package codegen
import (
"bytes"
"fmt"
"os"
"strings"
"text/template"
)
// StructTagInfo is the data made available to struct tag templates. The
// omit flags are fully computed before rendering: OmitEmpty folds in the
// required/readOnly/writeOnly logic, compatibility options and the
// x-omitempty extension; OmitZero folds in x-omitzero and
// prefer-skip-optional-pointer-with-omitzero. Templates therefore only
// need to decide whether to emit them, not re-derive them.
type StructTagInfo struct {
// FieldName is the property name from the OpenAPI spec.
FieldName string
// IsOptional is true when the field is not listed as required.
IsOptional bool
// OmitEmpty is true when the field should carry ",omitempty".
OmitEmpty bool
// OmitZero is true when the field should carry ",omitzero".
OmitZero bool
// NeedsFormTag is true for fields bound from form-style parameters or
// urlencoded request bodies.
NeedsFormTag bool
}
// StructTagTemplate defines a single struct tag as a name plus a Go
// text/template rendered against StructTagInfo. A template that renders
// to the empty string suppresses the tag on that field.
type StructTagTemplate struct {
// Name is the tag name (e.g. "json", "yaml", "db").
Name string `yaml:"name"`
// Template is a Go text/template producing the tag value.
// Available fields: .FieldName, .IsOptional, .OmitEmpty, .OmitZero,
// .NeedsFormTag.
Template string `yaml:"template"`
}
// StructTagsConfig configures struct tag generation for generated fields.
type StructTagsConfig struct {
// Tags lists the tags to generate. Entries are merged by name on top
// of the defaults: a matching name replaces the default template, a
// new name adds a tag. Output ordering is always alphabetical by name.
Tags []StructTagTemplate `yaml:"tags,omitempty"`
}
const (
defaultJSONTagTemplate = `{{.FieldName}}{{if .OmitEmpty}},omitempty{{end}}{{if .OmitZero}},omitzero{{end}}`
defaultFormTagTemplate = `{{if .NeedsFormTag}}{{.FieldName}}{{if .OmitEmpty}},omitempty{{end}}{{end}}`
defaultYamlTagTemplate = `{{.FieldName}}{{if .OmitEmpty}},omitempty{{end}}`
)
// defaultStructTagsConfig returns the built-in tag templates, which
// reproduce the historical hardcoded output. The yaml entry is only
// present when the legacy `output-options.yaml-tags` flag is enabled and
// applies only where it always did (schema properties); a user-supplied
// yaml entry in struct-tags supersedes the flag entirely.
func defaultStructTagsConfig(enableYamlTags bool) StructTagsConfig {
tags := []StructTagTemplate{
{Name: "json", Template: defaultJSONTagTemplate},
{Name: "form", Template: defaultFormTagTemplate},
}
if enableYamlTags {
tags = append(tags, StructTagTemplate{Name: "yaml", Template: defaultYamlTagTemplate})
}
return StructTagsConfig{Tags: tags}
}
// Merge merges user config on top of this config by tag name: matching
// names override the default template, new names are appended.
func (c StructTagsConfig) Merge(other StructTagsConfig) StructTagsConfig {
if len(other.Tags) == 0 {
return c
}
merged := make(map[string]StructTagTemplate, len(c.Tags)+len(other.Tags))
order := make([]string, 0, len(c.Tags)+len(other.Tags))
for _, t := range c.Tags {
merged[t.Name] = t
order = append(order, t.Name)
}
for _, t := range other.Tags {
if _, exists := merged[t.Name]; !exists {
order = append(order, t.Name)
}
merged[t.Name] = t
}
result := StructTagsConfig{Tags: make([]StructTagTemplate, 0, len(order))}
for _, name := range order {
result.Tags = append(result.Tags, merged[name])
}
return result
}
// structTagGenerator renders struct tag values from parsed templates.
type structTagGenerator struct {
templates []tagTemplate
}
type tagTemplate struct {
name string
tmpl *template.Template
}
// newStructTagGenerator parses the configured templates. Each template is
// also rendered against every combination of StructTagInfo's boolean
// fields so that execute-time errors (e.g. references to unknown fields),
// including ones gated behind conditionals, are reported at configuration
// time rather than silently dropping tags during generation.
func newStructTagGenerator(config StructTagsConfig) (*structTagGenerator, error) {
g := &structTagGenerator{
templates: make([]tagTemplate, 0, len(config.Tags)),
}
// Keep numFlags in sync with the boolean fields of StructTagInfo.
const numFlags = 4
probes := make([]StructTagInfo, 0, 1<<numFlags)
for mask := 0; mask < 1<<numFlags; mask++ {
probes = append(probes, StructTagInfo{
FieldName: "probe",
IsOptional: mask&1 != 0,
OmitEmpty: mask&2 != 0,
OmitZero: mask&4 != 0,
NeedsFormTag: mask&8 != 0,
})
}
for _, tag := range config.Tags {
tmpl, err := template.New(tag.Name).Parse(tag.Template)
if err != nil {
return nil, fmt.Errorf("invalid struct tag template for %q: %w", tag.Name, err)
}
for _, probe := range probes {
var buf bytes.Buffer
if err := tmpl.Execute(&buf, probe); err != nil {
return nil, fmt.Errorf("struct tag template for %q failed to render: %w", tag.Name, err)
}
}
g.templates = append(g.templates, tagTemplate{name: tag.Name, tmpl: tmpl})
}
return g, nil
}
// jsonTagKey returns the JSON key a json tag gives a field, as encoding/json
// reads it, and false when the tag leaves the field off the wire.
func jsonTagKey(tag, goFieldName string) (string, bool) {
if tag == "-" {
return "", false
}
key, _, _ := strings.Cut(tag, ",")
if key == "" {
return goFieldName, true
}
return key, true
}
// generateTagsMap renders every configured template for the given field
// and returns the non-empty results as tag name -> tag value. Extension
// driven overrides (x-go-json-ignore, x-oapi-codegen-extra-tags) are
// applied by the callers on top of this map.
func (g *structTagGenerator) generateTagsMap(info StructTagInfo) map[string]string {
result := make(map[string]string, len(g.templates))
for _, t := range g.templates {
var buf bytes.Buffer
if err := t.tmpl.Execute(&buf, info); err != nil {
// Templates are validated at construction time against every
// combination of boolean fields, so a failure here is
// unexpected. The tag is skipped rather than emitting a partial
// value, with a warning so the omission is not silent.
fmt.Fprintf(os.Stderr, "Warning: struct tag template %q failed for field %q, omitting the tag: %v\n", t.name, info.FieldName, err)
continue
}
if value := buf.String(); value != "" {
result[t.name] = value
}
}
return result
}
// schemaFieldTagGenerator returns the generator used for schema property
// fields, building it from the current configuration on first use. This
// lazy path keeps direct callers of GenFieldsFromProperties (tests)
// working without running Generate; Generate itself constructs the
// generators eagerly so configuration errors are reported.
func schemaFieldTagGenerator() *structTagGenerator {
if globalState.schemaFieldTagGenerator == nil {
cfg := defaultStructTagsConfig(globalState.options.OutputOptions.EnableYamlTags).
Merge(globalState.options.OutputOptions.StructTags)
g, err := newStructTagGenerator(cfg)
if err != nil {
// Generate() reports this error to the user; fall back to the
// defaults so lazy callers still produce standard tags.
g, _ = newStructTagGenerator(defaultStructTagsConfig(globalState.options.OutputOptions.EnableYamlTags))
}
globalState.schemaFieldTagGenerator = g
}
return globalState.schemaFieldTagGenerator
}
// paramFieldTagGenerator returns the generator used for path parameter
// fields on strict RequestObject structs (ParameterDefinition.JsonTag).
// It differs from the schema one in a single way: the legacy `yaml-tags`
// flag never applied to those fields, so its injected default is excluded
// here. Tags configured explicitly via struct-tags apply to them too.
func paramFieldTagGenerator() *structTagGenerator {
if globalState.paramFieldTagGenerator == nil {
cfg := defaultStructTagsConfig(false).
Merge(globalState.options.OutputOptions.StructTags)
g, err := newStructTagGenerator(cfg)
if err != nil {
g, _ = newStructTagGenerator(defaultStructTagsConfig(false))
}
globalState.paramFieldTagGenerator = g
}
return globalState.paramFieldTagGenerator
}