forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
234 lines (203 loc) · 4.69 KB
/
util.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
package graphql
import (
"encoding"
"fmt"
"reflect"
"strings"
"time"
)
const TAG = "json"
var boundTypes = map[string]*Object{}
var anonTypes = 0
var timeType = reflect.TypeOf(time.Time{})
func BindType(tipe reflect.Type) Type {
if tipe.Kind() == reflect.Ptr {
tipe = tipe.Elem()
}
kind := tipe.Kind()
switch kind {
case reflect.String:
return String
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64:
return Int
case reflect.Float32, reflect.Float64:
return Float
case reflect.Bool:
return Boolean
case reflect.Slice:
return getGraphList(tipe)
}
typeName := safeName(tipe)
object, ok := boundTypes[typeName]
if !ok {
// Allows for recursion
object = &Object{}
boundTypes[typeName] = object
*object = *NewObject(ObjectConfig{
Name: typeName,
Fields: BindFields(reflect.New(tipe).Interface()),
})
}
return object
}
func safeName(tipe reflect.Type) string {
name := fmt.Sprint(tipe)
if strings.HasPrefix(name, "struct ") {
anonTypes++
name = fmt.Sprintf("Anon%d", anonTypes)
} else {
name = strings.Replace(fmt.Sprint(tipe), ".", "_", -1)
}
return name
}
func BindFields(obj interface{}) Fields {
t := reflect.TypeOf(obj)
v := reflect.ValueOf(obj)
fields := make(map[string]*Field)
if t.Kind() == reflect.Ptr {
t = t.Elem()
v = v.Elem()
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tag := extractTag(field)
if tag == "-" {
continue
}
fieldType := field.Type
if fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
}
var graphType Output
if fieldType == timeType {
graphType = DateTime
} else if fieldType.Kind() == reflect.Struct {
itf := v.Field(i).Interface()
if _, ok := itf.(encoding.TextMarshaler); ok {
fieldType = reflect.TypeOf("")
goto nonStruct
}
structFields := BindFields(itf)
if tag == "" {
fields = appendFields(fields, BindFields(structFields))
continue
} else {
graphType = BindType(fieldType)
}
}
nonStruct:
if tag == "" {
continue
}
if graphType == nil {
graphType = getGraphType(fieldType)
}
fields[tag] = &Field{
Type: graphType,
Resolve: func(p ResolveParams) (interface{}, error) {
return extractValue(tag, p.Source), nil
},
}
}
return fields
}
func getGraphType(tipe reflect.Type) Output {
kind := tipe.Kind()
switch kind {
case reflect.String:
return String
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64:
return Int
case reflect.Float32, reflect.Float64:
return Float
case reflect.Bool:
return Boolean
case reflect.Slice:
return getGraphList(tipe)
}
return String
}
func getGraphList(tipe reflect.Type) *List {
if tipe.Kind() == reflect.Slice {
switch tipe.Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64:
return NewList(Int)
case reflect.Bool:
return NewList(Boolean)
case reflect.Float32, reflect.Float64:
return NewList(Float)
case reflect.String:
return NewList(String)
}
}
// finally bind object
t := reflect.New(tipe.Elem())
obj := BindType(t.Elem().Type())
return NewList(obj)
}
func appendFields(dest, origin Fields) Fields {
for key, value := range origin {
dest[key] = value
}
return dest
}
func extractValue(originTag string, obj interface{}) interface{} {
val := reflect.Indirect(reflect.ValueOf(obj))
for j := 0; j < val.NumField(); j++ {
field := val.Type().Field(j)
found := originTag == extractTag(field)
if field.Type.Kind() == reflect.Struct {
itf := val.Field(j).Interface()
if str, ok := itf.(encoding.TextMarshaler); ok && found {
byt, _ := str.MarshalText()
return string(byt)
}
res := extractValue(originTag, itf)
if res != nil {
return res
}
}
if found {
return reflect.Indirect(val.Field(j)).Interface()
}
}
return nil
}
func extractTag(strct reflect.StructField) string {
t := strct.Tag.Get(TAG)
switch {
case t != "": //work as tags specified
t = strings.Split(t, ",")[0]
case strct.Anonymous: //embedding
default:
t = strings.ToLower(strct.Name)
}
return t
}
// lazy way of binding args
func BindArg(obj interface{}, tags ...string) FieldConfigArgument {
v := reflect.Indirect(reflect.ValueOf(obj))
var config = make(FieldConfigArgument)
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
mytag := extractTag(field)
if inArray(tags, mytag) {
config[mytag] = &ArgumentConfig{
Type: getGraphType(field.Type),
}
}
}
return config
}
func inArray(slice interface{}, item interface{}) bool {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic("inArray() given a non-slice type")
}
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(item, s.Index(i).Interface()) {
return true
}
}
return false
}