forked from oapi-codegen/oapi-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.go
111 lines (99 loc) · 3.17 KB
/
extension.go
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
package codegen
import (
"fmt"
)
const (
// extPropGoType overrides the generated type definition.
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 is used to override a generated typename for something.
extGoTypeName = "x-go-type-name"
extPropGoJsonIgnore = "x-go-json-ignore"
extPropOmitEmpty = "x-omitempty"
extPropExtraTags = "x-oapi-codegen-extra-tags"
extEnumVarNames = "x-enum-varnames"
extEnumNames = "x-enumNames"
extDeprecationReason = "x-deprecated-reason"
// extGoRuterGroup is used to mark tag as a router group.
extGoRouterGroup = "x-go-router-group"
)
func extParseGoRouterGroup(extPropValue interface{}) (bool, error) {
goRouterGroup, ok := extPropValue.(bool)
if !ok {
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
}
return goRouterGroup, nil
}
func extString(extPropValue interface{}) (string, error) {
str, ok := extPropValue.(string)
if !ok {
return "", fmt.Errorf("failed to convert type: %T", extPropValue)
}
return str, nil
}
func extTypeName(extPropValue interface{}) (string, error) {
return extString(extPropValue)
}
func extParsePropGoTypeSkipOptionalPointer(extPropValue interface{}) (bool, error) {
goTypeSkipOptionalPointer, ok := extPropValue.(bool)
if !ok {
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
}
return goTypeSkipOptionalPointer, nil
}
func extParseGoFieldName(extPropValue interface{}) (string, error) {
return extString(extPropValue)
}
func extParseOmitEmpty(extPropValue interface{}) (bool, error) {
omitEmpty, ok := extPropValue.(bool)
if !ok {
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
}
return omitEmpty, nil
}
func extExtraTags(extPropValue interface{}) (map[string]string, error) {
tagsI, ok := extPropValue.(map[string]interface{})
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 interface{}) (bool, error) {
goJsonIgnore, ok := extPropValue.(bool)
if !ok {
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
}
return goJsonIgnore, nil
}
func extParseEnumVarNames(extPropValue interface{}) ([]string, error) {
namesI, ok := extPropValue.([]interface{})
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 interface{}) (string, error) {
return extString(extPropValue)
}