-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreflect.go
executable file
·217 lines (208 loc) · 5.5 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
package swaggos
import (
"fmt"
"reflect"
"github.com/go-openapi/spec"
)
// Define defines a object or a array to swagger definitions area.
// it will find all sub-items and build them into swagger tree.
// it returns the definitions ref.
func (swaggos *Swaggos) Define(v interface{}) spec.Ref {
schema := swaggos.buildSchema(v)
return swaggos.addDefinition(v, schema)
}
// addDefinition add a definition to swagger definitions.
// the name will get from the given type.
// if name's name is repeated, will add package path prefix to the name until name is unique.
func (swaggos *Swaggos) addDefinition(t interface{}, v spec.Schema) spec.Ref {
var (
name string
typ reflect.Type
)
switch tt := t.(type) {
case reflect.Type:
typ = tt
case reflect.Value:
typ = tt.Type()
default:
typ = reflect.TypeOf(t)
if typ == nil {
typ = reflect.TypeOf(new(interface{}))
} else {
typ = reflect.Indirect(reflect.ValueOf(t)).Type()
}
if typ.Kind() == reflect.Slice || typ.Kind() == reflect.Array {
typ = typ.Elem()
for {
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
} else {
break
}
}
name = fmt.Sprintf("%sArray", typ.Name())
}
}
if name == "" {
name = typ.Name()
}
if swaggos.typeNames == nil {
swaggos.typeNames = make(map[reflect.Type]string)
}
if name, ok := swaggos.typeNames[typ]; ok {
return definitionRef(name)
}
pkgPath := pkgPath(typ)
subName := name
i := 1
for {
// create a newName. like pkgName
if _, ok := swaggos.definitions[subName]; ok {
var prefix string
if len(pkgPath)-i > 0 {
prefix = pkgPath[len(pkgPath)-i]
i++
} else {
name = subName
break
}
subName = fmt.Sprintf("%s.%s", prefix, name)
} else {
name = subName
break
}
}
swaggos.definitions[name] = v
swaggos.typeNames[typ] = name
return definitionRef(name)
}
func (swaggos *Swaggos) buildSchema(v interface{}) spec.Schema {
typ := reflect.TypeOf(v)
// if given nil interface{}, typ is nil, then we return a empty object schema
if typ == nil {
return emptyObjectSchema()
}
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
if isBasicType(typ) {
return basicSchema(typ)
}
switch typ.Kind() {
case reflect.Array, reflect.Slice:
elTyp := typ.Elem()
if elTyp.Kind() == reflect.Ptr {
elTyp = elTyp.Elem()
}
elVal := reflect.New(elTyp).Elem()
// basicArray
if isBasicType(elTyp) {
schema := basicSchema(elTyp)
return arraySchema(&schema)
}
// structArray
if elTyp.Kind() == reflect.Struct {
schema := swaggos.buildSchema(elVal.Interface())
ref := swaggos.addDefinition(elVal, schema)
return arraySchemaRef(ref)
}
var arraySchema = emptyArray()
childType, childSchema := arrayProps(elTyp, &arraySchema)
if isBasicType(childType) {
basic := basicSchema(childType)
childSchema.Items = &spec.SchemaOrArray{
Schema: &basic,
}
} else {
schema := swaggos.buildSchema(reflect.New(childType).Elem().Interface())
ref := swaggos.addDefinition(childType, schema)
childSchema.Items = refArraySchema(ref)
}
return arraySchema
case reflect.Struct:
return swaggos.buildStructSchema(v)
case reflect.Map, reflect.Interface:
// TODO:: handle map schema
return emptyObjectSchema()
}
return emptyObjectSchema()
}
// val is struct value
func (swaggos *Swaggos) buildStructSchema(v interface{}) spec.Schema {
typ := reflect.TypeOf(v)
val := reflect.ValueOf(v)
// if given nil interface{}, typ is nil, then we return a empty object schema
if typ == nil {
return emptyObjectSchema()
}
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = reflect.Indirect(reflect.New(typ))
}
if typ.Kind() != reflect.Struct {
panic(fmt.Errorf("interface{} is not struct: %T", v))
}
var schema spec.Schema
schema.Properties = make(spec.SchemaProperties)
schema.Type = spec.StringOrArray{_Object}
for i := 0; i < typ.NumField(); i++ {
field := val.Field(i)
fieldType := field.Type()
if !isExport(typ.Field(i).Name) {
continue
}
tg := newTags(typ.Field(i).Tag)
if tg.ignore() {
continue
}
// Anonymous field, if there are json tag on the field. then we say it's a object reference.
// if it's basic type, add it to properties directly, else we build it.
if typ.Field(i).Anonymous && tg.jsonTag() == "" {
if isBasicType(fieldType) {
prop := basicSchema(fieldType)
schema.Properties[typ.Field(i).Name] = prop
continue
} else {
// TODO:: if the field is a array type. what should we do here???
fieldSchema := swaggos.buildSchema(field.Interface())
for name, val := range fieldSchema.Properties {
schema.Properties[name] = val
}
schema.Required = append(schema.Required, fieldSchema.Required...)
}
continue
}
fieldName := typ.Field(i).Name
if name := tg.jsonName(); name != "" {
fieldName = name
}
if tg.required() {
schema.Required = append(schema.Required, fieldName)
}
var prop spec.Schema
if isBasicType(fieldType) {
prop = basicSchema(fieldType)
} else {
if isDeepEqualObject(val, field) {
prop = emptyObjectSchema()
} else {
prop = swaggos.buildSchema(field.Interface())
}
}
prop = tg.mergeSchema(prop)
schema.Properties[fieldName] = prop
}
return schema
}
func isDeepEqualObject(parent reflect.Value, child reflect.Value) bool {
var childTyp = child.Type()
switch child.Type().Kind() {
case reflect.Array, reflect.Slice:
childTyp = reflect.TypeOf(child.Interface()).Elem()
if childTyp.Kind() == reflect.Ptr {
childTyp = childTyp.Elem()
}
}
fmt.Println(parent.Type(), childTyp)
return parent.Type() == childTyp
}