-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.go
184 lines (168 loc) · 4.4 KB
/
schema.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
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
package gomvc
import (
"encoding/json"
"fmt"
"strings"
"github.com/aymerick/raymond"
"github.com/getkin/kin-openapi/openapi3"
"github.com/iancoleman/strcase"
)
func CreateStructFromSchemaObject(o *SchemaObject) (string, error) {
tmplString := `
package models
{{#if GoType}}
type {{Name}} {{GoType}}
{{else}}
type {{Name}} struct {
{{#each Properties}}
{{#if this.description}}
// {{this.description}}{{/if}}
{{camelize @key}} {{this.GoType}}{{/each}}
}
{{/if}}
`
tmpl, err := raymond.Parse(tmplString)
if err != nil {
return "", err
}
tmpl.RegisterHelper("camelize", func(word string) string {
return strcase.ToCamel(word)
})
result := tmpl.MustExec(o)
return result, nil
}
type Property struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"`
Items Item `json:"items,omitempty"`
Format string `json:"format,omitempty"`
Required bool `json:"required,omitempty"`
GoType string `json:"go_type,omitempty"`
Ref string `json:"$ref,omitempty"`
AdditionalProperties AdditionalProperties `json:"additionalProperties,omitempty"`
}
type AdditionalProperties struct {
Type string
OneOf []Type
AnyOf []Type
}
type Type struct {
Type string
}
type Item struct {
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
}
type SchemaObject struct {
Name string
Description string `json:"description,omitempty"`
Properties map[string]Property `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
GoType string
Items Item
}
// todo: collect enums
func LoadSchemaObject(name string, r *openapi3.SchemaRef) SchemaObject {
// hack
b, _ := json.MarshalIndent(r, "", "\t")
var o SchemaObject
if err := json.Unmarshal(b, &o); err != nil {
panic(err)
}
if o.Type == "array" {
o.GoType = fmt.Sprintf("[]%s", o.Items.Type)
} else if objectType(o.Type) {
requiredMap := map[string]bool{}
for _, propertyName := range o.Required {
requiredMap[propertyName] = true
}
for name, property := range o.Properties {
property.Required = requiredMap[name]
// mutation
goType := GetGoType(name, &property)
property.GoType = goType
o.Properties[name] = property
}
} else {
o.GoType = GetGoPrimitiveType(o.Type)
}
o.Name = strcase.ToCamel(name)
return o
}
func objectType(t string) bool {
return t == "object"
}
func GetGoPrimitiveType(t string) string {
switch t {
case "boolean":
return "bool"
case "string":
return "string"
case "integer":
return "int"
default:
panic(fmt.Sprintf("invalid primitive: %s", t))
}
}
// GetGoType mutates the property type to determine GoType
func GetGoType(name string, p *Property) string {
if p.Format == "" {
switch p.Type {
case "boolean":
return "bool"
case "string":
return "string"
case "integer":
return "int"
case "array":
switch p.Items.Type {
case "integer":
return "[]int"
case "number", "double":
return "float"
default:
panic(fmt.Sprintf("unexpected format: %s", p.Format))
}
case "object":
if hasAdditionalProperties(p) {
return "map[string]interface{}"
}
refObjectName := GetLastPathPart(p.Ref)
return refObjectName
case "number", "double":
return "float64"
case "":
if p.Ref == "" {
panic(fmt.Sprintf("while your spec may be valid, we require properties to have a type (e.g. integer, array) for %s %s", name, p.Ref))
}
// is this enough? what if there's a ref and also properties
refObjectName := GetLastPathPart(p.Ref)
return refObjectName
default:
panic(fmt.Sprintf("unsupported type: %s for %s", p.Type, name))
}
} else {
switch p.Format {
case "date-time":
return "time.Time"
case "number", "double":
return "float64"
default:
return p.Format
}
}
}
func GetLastPathPart(path string) string {
parts := strings.Split(path, "/")
if len(parts) == 0 {
return ""
}
return parts[len(parts)-1]
}
func hasAdditionalProperties(p *Property) bool {
ap := &p.AdditionalProperties
return ap.Type != "" && len(ap.AnyOf) == 0 && len(ap.OneOf) == 0
}