-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathformatTypes.go
143 lines (132 loc) · 2.63 KB
/
formatTypes.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
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
package astra
import "maps"
// TypeFormat is the types of the standard go types that are accepted by OpenAPI.
type TypeFormat struct {
Type string
Format string
}
// PredefinedTypeMap is the map of the standard go types that are accepted by OpenAPI.
// It contains the go type as a string and the corresponding OpenAPI type as the value - also including the format.
var PredefinedTypeMap = map[string]TypeFormat{
"string": {
Type: "string",
},
"int": {
Type: "integer",
Format: "int32",
},
"int8": {
Type: "integer",
Format: "int8",
},
"int16": {
Type: "integer",
Format: "int16",
},
"int32": {
Type: "integer",
Format: "int32",
},
"int64": {
Type: "integer",
Format: "int64",
},
"uint": {
Type: "integer",
Format: "uint",
},
"uint8": {
Type: "integer",
Format: "uint8",
},
"uint16": {
Type: "integer",
Format: "uint16",
},
"uint32": {
Type: "integer",
Format: "uint32",
},
"uint64": {
Type: "integer",
Format: "uint64",
},
"float32": {
Type: "number",
Format: "float32",
},
"float64": {
Type: "number",
Format: "float64",
},
"bool": {
Type: "boolean",
},
"byte": {
Type: "string",
Format: "byte",
},
"rune": {
Type: "string",
Format: "rune",
},
"struct": {
Type: "object",
},
"map": {
Type: "object",
},
"slice": {
Type: "array",
},
"any": {
Type: "",
},
"nil": {
Type: "",
},
// Passthrough types
"time.Time": {
Type: "string",
Format: "date-time",
},
"github.com/google/uuid.UUID": {
Type: "string",
Format: "uuid",
},
// Custom handlers
"file": {
Type: "string",
Format: "binary",
},
}
// WithCustomTypeMapping adds a custom type mapping to the predefined type map.
func WithCustomTypeMapping(customTypeMap map[string]TypeFormat) Option {
return func(service *Service) {
for k, v := range customTypeMap {
service.CustomTypeMapping[k] = v
}
}
}
// WithCustomTypeMappingSingle adds a custom type mapping to the predefined type map.
func WithCustomTypeMappingSingle(key string, valueType string, valueFormat string) Option {
return func(service *Service) {
service.CustomTypeMapping[key] = TypeFormat{
Type: valueType,
Format: valueFormat,
}
}
}
// GetTypeMapping returns the type mapping for the given key.
func (s *Service) GetTypeMapping(key string, pkg string) (TypeFormat, bool) {
if s.fullTypeMapping == nil {
s.fullTypeMapping = make(map[string]TypeFormat)
maps.Copy(s.fullTypeMapping, PredefinedTypeMap)
maps.Copy(s.fullTypeMapping, s.CustomTypeMapping)
}
if !IsAcceptedType(key) {
key = pkg + "." + key
}
value, exists := s.fullTypeMapping[key]
return value, exists
}