This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
192 lines (166 loc) · 3.79 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
package graphql
import (
"encoding"
"fmt"
"reflect"
"strings"
)
const TAG = "json"
// can't take recursive slice type
// e.g
// type Person struct{
// Friends []Person
// }
// it will throw panic stack-overflow
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.Tag)
if tag == "-" {
continue
}
fieldType := field.Type
if fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
}
var graphType Output
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, structFields)
continue
} else {
graphType = NewObject(ObjectConfig{
Name: tag,
Fields: structFields,
})
}
}
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())
name := strings.Replace(fmt.Sprint(tipe.Elem()), ".", "_", -1)
obj := NewObject(ObjectConfig{
Name: name,
Fields: BindFields(t.Elem().Interface()),
})
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.Tag)
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(tag reflect.StructTag) string {
t := tag.Get(TAG)
if t != "" {
t = strings.Split(t, ",")[0]
}
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.Tag)
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
}