forked from emicklei/go-restful-swagger12
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodel_builder.go
executable file
·185 lines (171 loc) · 5.32 KB
/
model_builder.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
185
package swagger
import (
"reflect"
"regexp"
"strings"
)
func (b modelBuilder) addModel(st reflect.Type, nameOverride string) *Items {
// Turn pointers into simpler types so further checks are
if st.Kind() == reflect.Ptr {
st = st.Elem()
}
modelName := b.keyFrom(st)
if nameOverride != "" {
modelName = nameOverride
}
reg := regexp.MustCompile("[a-z]+\\.")
name := reg.ReplaceAllString(modelName, "")
// no models needed for primitive types
if b.isPrimitiveType(modelName) {
return nil
}
if (st.Kind() == reflect.Slice || st.Kind() == reflect.Array) &&
st.Elem().Kind() == reflect.Uint8 {
return nil
}
// see if we already have visited this model
if _, ok := atMap(name, b.Definitions); ok {
return nil
}
sm := Items{
Type: "object",
Properties: map[string]*Items{}}
(*b.Definitions)[name] = &sm
// check for slice or array
if st.Kind() == reflect.Slice || st.Kind() == reflect.Array {
b.addModel(st.Elem(), "")
return &sm
}
// check for structure or primitive type
if st.Kind() != reflect.Struct {
return &sm
}
for i := 0; i < st.NumField(); i++ {
field := st.Field(i)
//add tag,if =="" ignore
if b.nameOfField(field) == "" {
continue
} else {
field.Name = b.nameOfField(field)
}
sm.Properties[field.Name] = &Items{}
ft := field.Type
isCollection, ft := detectCollectionType(ft)
fieldName := modelBuilder{Config: b.Config}.keyFrom(ft)
if !isCollection {
if ft.Kind() == reflect.Struct {
if fieldName == "time.Time" {
sm.Properties[field.Name].Type = getOtherName(fieldName)
sm.Properties[field.Name].Format = getFormat(fieldName)
} else if len(ft.Name()) == 0 {
anonType := name + "." + field.Name
sm.Properties[field.Name].Ref = "#/definitions/" + anonType
b.addModel(ft, anonType)
} else {
sm.Properties[field.Name].Ref = getModelName(fieldName)
b.addModel(ft, "")
}
} else if ft.Kind() == reflect.Map {
ft = ft.Elem()
if ft.Kind() == reflect.Struct {
sm.Properties[field.Name].Type = "object"
sm.Properties[field.Name].AdditionalProperties = &Items{}
modelName = modelBuilder{Config: b.Config}.keyFrom(ft)
sm.Properties[field.Name].AdditionalProperties.Ref = getModelName(modelName)
b.addModel(ft, "")
} else {
sm.Properties[field.Name].Type = "object"
sm.Properties[field.Name].AdditionalProperties = &Items{}
modelName = modelBuilder{Config: b.Config}.keyFrom(ft)
sm.Properties[field.Name].AdditionalProperties.Type = getOtherName(modelName)
if getOtherName(modelName) == "integer" || getOtherName(modelName) == "number" {
sm.Properties[field.Name].AdditionalProperties.Format = getFormat(modelName)
}
}
} else {
sm.Properties[field.Name].Type = getOtherName(fieldName)
if getOtherName(fieldName) == "integer" || getOtherName(fieldName) == "number" {
sm.Properties[field.Name].Format = getFormat(fieldName)
}
}
} else {
if ft.Kind() == reflect.Struct {
sm.Properties[field.Name].Type = "array"
sm.Properties[field.Name].Items = &Items{}
sm.Properties[field.Name].Items.Ref = getModelName(fieldName)
b.addModel(ft, "")
} else {
sm.Properties[field.Name].Type = "array"
sm.Properties[field.Name].Items = &Items{}
sm.Properties[field.Name].Items.Type = getOtherName(fieldName)
if getOtherName(fieldName) == "integer" || getOtherName(fieldName) == "number" {
sm.Properties[field.Name].Items.Format = getFormat(fieldName)
}
}
}
if b.typeOfField(field) != "" {
sm.Properties[field.Name].Type = b.typeOfField(field)
}
}
(*b.Definitions)[name] = &sm
return &sm
}
type modelBuilder struct {
Definitions *map[string]*Items
Config *Config
}
func atMap(name string, mapItem *map[string]*Items) (m *Items, ok bool) {
for key, value := range *mapItem {
if key == name {
return value, true
}
}
return m, false
}
func (b modelBuilder) keyFrom(st reflect.Type) string {
key := st.String()
if b.Config != nil && b.Config.ModelTypeNameHandler != nil {
if name, ok := b.Config.ModelTypeNameHandler(st); ok {
key = name
}
}
if len(st.Name()) == 0 {
key = strings.Replace(key, "[]", "", -1)
}
return key
}
////// see also https://golang.org/ref/spec#Numeric_types
func (b modelBuilder) isPrimitiveType(modelName string) bool {
if len(modelName) == 0 {
return false
}
return strings.Contains("uint uint8 uint16 uint32 uint64 int int8 int16 int32 int64 float32 float64 bool string byte rune time.Time", modelName)
}
// nameOfField returns the name of the field as it should appear in JSON format
// An empty string indicates that this field is not part of the JSON representation
func (b modelBuilder) nameOfField(field reflect.StructField) string {
if tag := field.Tag.Get("json"); tag != "" {
s := strings.Split(tag, ",")
if s[0] == "-" {
// empty name signals skip property
return ""
} else if s[0] != "" {
return s[0]
}
}
return field.Name
}
// typeOfField returns the type of the field as it should appear in JSON format
// An empty string indicates that this field is not part of the JSON representation
func (b modelBuilder) typeOfField(field reflect.StructField) string {
if tag := field.Tag.Get("swag"); tag != "" {
s := strings.Split(tag, ",")
if s[0] == "-" {
// empty name signals skip property
return ""
} else if s[0] != "" {
return s[0]
}
}
return ""
}