forked from zc2638/swag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect.go
253 lines (211 loc) · 5.55 KB
/
reflect.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright © 2022 zc2638 <[email protected]>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package swag
import (
"reflect"
"strings"
"github.com/zc2638/swag/types"
)
func inspect(t reflect.Type, jsonTag string) Property {
p := Property{
GoType: t,
}
if strings.Contains(jsonTag, ",string") {
p.Type = "string"
return p
}
if p.GoType.Kind() == reflect.Ptr {
p.GoType = p.GoType.Elem()
}
switch p.GoType.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32:
p.Type = types.Integer.String()
p.Format = "int32"
case reflect.Int64, reflect.Uint64:
p.Type = types.Integer.String()
p.Format = "int64"
case reflect.Float64:
p.Type = types.Number.String()
p.Format = "double"
case reflect.Float32:
p.Type = types.Number.String()
p.Format = "float"
case reflect.Bool:
p.Type = types.Boolean.String()
case reflect.String:
p.Type = types.String.String()
case reflect.Struct:
name := makeName(p.GoType)
p.Ref = makeRef(name)
case reflect.Ptr:
p.GoType = t.Elem()
name := makeName(p.GoType)
p.Ref = makeRef(name)
case reflect.Slice:
p.Type = types.Array.String()
p.Items = &Items{}
p.GoType = t.Elem() // dereference the slice
switch p.GoType.Kind() {
case reflect.Ptr:
p.GoType = p.GoType.Elem()
name := makeName(p.GoType)
p.Items.Ref = makeRef(name)
case reflect.Struct:
name := makeName(p.GoType)
p.Items.Ref = makeRef(name)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32:
p.Items.Type = types.Integer.String()
p.Items.Format = "int32"
case reflect.Int64, reflect.Uint64:
p.Items.Type = types.Integer.String()
p.Items.Format = "int64"
case reflect.Float64:
p.Items.Type = types.Number.String()
p.Items.Format = "double"
case reflect.Float32:
p.Items.Type = types.Number.String()
p.Items.Format = "float"
case reflect.String:
p.Items.Type = types.String.String()
}
}
return p
}
func buildProperty(t reflect.Type) (map[string]Property, []string) {
properties := make(map[string]Property)
required := make([]string, 0)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
// skip unexported fields
if strings.ToLower(field.Name[0:1]) == field.Name[0:1] {
continue
}
if field.Anonymous {
// 暂不处理匿名结构的required
ps, _ := buildProperty(field.Type)
for name, p := range ps {
properties[name] = p
}
continue
}
// determine the json name of the field
name := strings.TrimSpace(field.Tag.Get("json"))
if name == "" || strings.HasPrefix(name, ",") {
name = field.Name
} else {
// strip out things like , omitempty
parts := strings.Split(name, ",")
name = parts[0]
}
parts := strings.Split(name, ",") // foo,omitempty => foo
name = parts[0]
if name == "-" {
// honor json ignore tag
continue
}
p := inspect(field.Type, field.Tag.Get("json"))
// determine the extra info of the field
if _, ok := field.Tag.Lookup("required"); ok {
required = append(required, name)
}
if example := field.Tag.Get("example"); example != "" {
p.Example = example
}
if description := field.Tag.Get("description"); description != "" {
p.Description = description
}
if desc := field.Tag.Get("desc"); desc != "" {
p.Description = desc
}
if enum := field.Tag.Get("enum"); enum != "" {
p.Enum = strings.Split(enum, ",")
}
properties[name] = p
}
return properties, required
}
func defineObject(v interface{}, desc string) Object {
var t reflect.Type
switch value := v.(type) {
case reflect.Type:
t = value
default:
t = reflect.TypeOf(v)
}
isArray := t.Kind() == reflect.Slice
if isArray {
t = t.Elem()
}
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
p := inspect(t, "")
return Object{
IsArray: isArray,
GoType: t,
Type: p.Type,
Format: p.Format,
Name: t.Kind().String(),
}
}
properties, required := buildProperty(t)
return Object{
IsArray: isArray,
GoType: t,
Type: "object",
Name: makeName(t),
Required: required,
Properties: properties,
Description: desc,
}
}
func define(v interface{}) map[string]Object {
objMap := map[string]Object{}
obj := defineObject(v, "")
objMap[obj.Name] = obj
dirty := true
for dirty {
dirty = false
for _, d := range objMap {
for _, p := range d.Properties {
if p.GoType.Kind() == reflect.Struct {
name := makeName(p.GoType)
if _, exists := objMap[name]; !exists {
child := defineObject(p.GoType, p.Description)
objMap[child.Name] = child
dirty = true
}
}
}
}
}
return objMap
}
// MakeSchema takes struct or pointer to a struct and returns a Schema instance suitable for use by the swagger doc
func MakeSchema(prototype interface{}) *Schema {
schema := &Schema{
Prototype: prototype,
}
obj := defineObject(prototype, "")
if obj.IsArray {
schema.Type = "array"
schema.Items = &Items{
Ref: makeRef(obj.Name),
}
} else {
schema.Ref = makeRef(obj.Name)
}
return schema
}