-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathtypemapping.go
More file actions
109 lines (98 loc) · 3.25 KB
/
typemapping.go
File metadata and controls
109 lines (98 loc) · 3.25 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
package codegen
// SimpleTypeSpec defines the Go type for an OpenAPI type/format combination,
// along with any import required to use it.
type SimpleTypeSpec struct {
Type string `yaml:"type" json:"type"`
Import string `yaml:"import,omitempty" json:"import,omitempty"`
}
// FormatMapping defines the default Go type and format-specific overrides
// for an OpenAPI type.
type FormatMapping struct {
Default SimpleTypeSpec `yaml:"default" json:"default"`
Formats map[string]SimpleTypeSpec `yaml:"formats,omitempty" json:"formats,omitempty"`
}
// TypeMapping defines the mapping from OpenAPI types to Go types.
type TypeMapping struct {
Integer FormatMapping `yaml:"integer,omitempty" json:"integer,omitempty"`
Number FormatMapping `yaml:"number,omitempty" json:"number,omitempty"`
Boolean FormatMapping `yaml:"boolean,omitempty" json:"boolean,omitempty"`
String FormatMapping `yaml:"string,omitempty" json:"string,omitempty"`
}
// Merge returns a new TypeMapping with user overrides applied on top of base.
func (base TypeMapping) Merge(user TypeMapping) TypeMapping {
return TypeMapping{
Integer: base.Integer.merge(user.Integer),
Number: base.Number.merge(user.Number),
Boolean: base.Boolean.merge(user.Boolean),
String: base.String.merge(user.String),
}
}
func (base FormatMapping) merge(user FormatMapping) FormatMapping {
result := FormatMapping{
Default: base.Default,
Formats: make(map[string]SimpleTypeSpec),
}
// Copy base formats
for k, v := range base.Formats {
result.Formats[k] = v
}
// Override with user default if specified
if user.Default.Type != "" {
result.Default = user.Default
}
// Override/add user formats
for k, v := range user.Formats {
result.Formats[k] = v
}
return result
}
// Resolve returns the SimpleTypeSpec for a given format string.
// If the format has a specific mapping, that is returned; otherwise the default is used.
func (fm FormatMapping) Resolve(format string) SimpleTypeSpec {
if format != "" {
if spec, ok := fm.Formats[format]; ok {
return spec
}
}
return fm.Default
}
// DefaultTypeMapping provides the default OpenAPI type/format to Go type mappings.
var DefaultTypeMapping = TypeMapping{
Integer: FormatMapping{
Default: SimpleTypeSpec{Type: "int"},
Formats: map[string]SimpleTypeSpec{
"int": {Type: "int"},
"int8": {Type: "int8"},
"int16": {Type: "int16"},
"int32": {Type: "int32"},
"int64": {Type: "int64"},
"uint": {Type: "uint"},
"uint8": {Type: "uint8"},
"uint16": {Type: "uint16"},
"uint32": {Type: "uint32"},
"uint64": {Type: "uint64"},
},
},
Number: FormatMapping{
Default: SimpleTypeSpec{Type: "float32"},
Formats: map[string]SimpleTypeSpec{
"float": {Type: "float32"},
"double": {Type: "float64"},
},
},
Boolean: FormatMapping{
Default: SimpleTypeSpec{Type: "bool"},
},
String: FormatMapping{
Default: SimpleTypeSpec{Type: "string"},
Formats: map[string]SimpleTypeSpec{
"byte": {Type: "[]byte"},
"email": {Type: "openapi_types.Email"},
"date": {Type: "openapi_types.Date"},
"date-time": {Type: "time.Time", Import: "time"},
"json": {Type: "json.RawMessage", Import: "encoding/json"},
"uuid": {Type: "openapi_types.UUID"},
"binary": {Type: "openapi_types.File"},
},
},
}