This repository was archived by the owner on Apr 14, 2026. It is now read-only.
forked from oapi-codegen/oapi-codegen
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextension.go
More file actions
128 lines (115 loc) · 3.92 KB
/
extension.go
File metadata and controls
128 lines (115 loc) · 3.92 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
package codegen
import (
"fmt"
)
const (
// extPropGoType overrides the generated type definition. When
// resolve-type-name-collisions is enabled, the collision resolver
// controls the final Go type name; this extension controls what
// that name aliases or refers to.
extPropGoType = "x-go-type"
// extPropGoTypeSkipOptionalPointer specifies that optional fields should
// be the type itself instead of a pointer to the type.
extPropGoTypeSkipOptionalPointer = "x-go-type-skip-optional-pointer"
// extPropGoImport specifies the module to import which provides above type
extPropGoImport = "x-go-type-import"
// extGoName is used to override a field name
extGoName = "x-go-name"
// extGoTypeName overrides a generated typename. When
// resolve-type-name-collisions is enabled, the collision resolver
// controls the top-level Go type name; this extension controls
// the name of the underlying type definition that gets aliased.
extGoTypeName = "x-go-type-name"
extPropGoJsonIgnore = "x-go-json-ignore"
extPropOmitEmpty = "x-omitempty"
extPropOmitZero = "x-omitzero"
extPropExtraTags = "x-oapi-codegen-extra-tags"
extEnumVarNames = "x-enum-varnames"
extEnumNames = "x-enumNames"
extDeprecationReason = "x-deprecated-reason"
extOrder = "x-order"
// extOapiCodegenOnlyHonourGoName is to be used to explicitly enforce the generation of a field as the `x-go-name` extension has describe it.
// This is intended to be used alongside the `allow-unexported-struct-field-names` Compatibility option
extOapiCodegenOnlyHonourGoName = "x-oapi-codegen-only-honour-go-name"
)
func extString(extPropValue any) (string, error) {
str, ok := extPropValue.(string)
if !ok {
return "", fmt.Errorf("failed to convert type: %T", extPropValue)
}
return str, nil
}
func extTypeName(extPropValue any) (string, error) {
return extString(extPropValue)
}
func extParsePropGoTypeSkipOptionalPointer(extPropValue any) (bool, error) {
goTypeSkipOptionalPointer, ok := extPropValue.(bool)
if !ok {
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
}
return goTypeSkipOptionalPointer, nil
}
func extParseGoFieldName(extPropValue any) (string, error) {
return extString(extPropValue)
}
func extParseOmitEmpty(extPropValue any) (bool, error) {
omitEmpty, ok := extPropValue.(bool)
if !ok {
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
}
return omitEmpty, nil
}
func extParseOmitZero(extPropValue any) (bool, error) {
omitZero, ok := extPropValue.(bool)
if !ok {
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
}
return omitZero, nil
}
func extExtraTags(extPropValue any) (map[string]string, error) {
tagsI, ok := extPropValue.(map[string]any)
if !ok {
return nil, fmt.Errorf("failed to convert type: %T", extPropValue)
}
tags := make(map[string]string, len(tagsI))
for k, v := range tagsI {
vs, ok := v.(string)
if !ok {
return nil, fmt.Errorf("failed to convert type: %T", v)
}
tags[k] = vs
}
return tags, nil
}
func extParseGoJsonIgnore(extPropValue any) (bool, error) {
goJsonIgnore, ok := extPropValue.(bool)
if !ok {
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
}
return goJsonIgnore, nil
}
func extParseEnumVarNames(extPropValue any) ([]string, error) {
namesI, ok := extPropValue.([]any)
if !ok {
return nil, fmt.Errorf("failed to convert type: %T", extPropValue)
}
names := make([]string, len(namesI))
for i, v := range namesI {
vs, ok := v.(string)
if !ok {
return nil, fmt.Errorf("failed to convert type: %T", v)
}
names[i] = vs
}
return names, nil
}
func extParseDeprecationReason(extPropValue any) (string, error) {
return extString(extPropValue)
}
func extParseOapiCodegenOnlyHonourGoName(extPropValue any) (bool, error) {
onlyHonourGoName, ok := extPropValue.(bool)
if !ok {
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
}
return onlyHonourGoName, nil
}