@@ -15,21 +15,33 @@ const TAG = "json"
15
15
// }
16
16
// it will throw panic stack-overflow
17
17
func BindFields (obj interface {}) Fields {
18
+ t := reflect .TypeOf (obj )
18
19
v := reflect .ValueOf (obj )
19
20
fields := make (map [string ]* Field )
20
21
21
- for i := 0 ; i < v .NumField (); i ++ {
22
- typeField := v .Type ().Field (i )
22
+ if t .Kind () == reflect .Ptr {
23
+ t = t .Elem ()
24
+ v = v .Elem ()
25
+ }
26
+
27
+ for i := 0 ; i < t .NumField (); i ++ {
28
+ field := t .Field (i )
23
29
24
- tag := extractTag (typeField .Tag )
30
+ tag := extractTag (field .Tag )
25
31
if tag == "-" {
26
32
continue
27
33
}
28
34
29
- var graphType Output
30
- if typeField .Type .Kind () == reflect .Struct {
35
+ fieldType := field .Type
31
36
37
+ if fieldType .Kind () == reflect .Ptr {
38
+ fieldType = fieldType .Elem ()
39
+ }
40
+
41
+ var graphType Output
42
+ if fieldType .Kind () == reflect .Struct {
32
43
structFields := BindFields (v .Field (i ).Interface ())
44
+
33
45
if tag == "" {
34
46
fields = appendFields (fields , structFields )
35
47
continue
@@ -46,7 +58,7 @@ func BindFields(obj interface{}) Fields {
46
58
}
47
59
48
60
if graphType == nil {
49
- graphType = getGraphType (typeField . Type )
61
+ graphType = getGraphType (fieldType )
50
62
}
51
63
fields [tag ] = & Field {
52
64
Type : graphType ,
@@ -122,19 +134,19 @@ func appendFields(dest, origin Fields) Fields {
122
134
}
123
135
124
136
func extractValue (originTag string , obj interface {}) interface {} {
125
- val := reflect .ValueOf (obj )
137
+ val := reflect .Indirect ( reflect . ValueOf (obj ) )
126
138
127
139
for j := 0 ; j < val .NumField (); j ++ {
128
- typeField := val .Type ().Field (j )
129
- if typeField .Type .Kind () == reflect .Struct {
140
+ field := val .Type ().Field (j )
141
+ if field .Type .Kind () == reflect .Struct {
130
142
res := extractValue (originTag , val .Field (j ).Interface ())
131
143
if res != nil {
132
144
return res
133
145
}
134
146
}
135
147
136
- if originTag == extractTag (typeField .Tag ) {
137
- return val .Field (j ).Interface ()
148
+ if originTag == extractTag (field .Tag ) {
149
+ return reflect . Indirect ( val .Field (j ) ).Interface ()
138
150
}
139
151
}
140
152
return nil
@@ -150,15 +162,15 @@ func extractTag(tag reflect.StructTag) string {
150
162
151
163
// lazy way of binding args
152
164
func BindArg (obj interface {}, tags ... string ) FieldConfigArgument {
153
- v := reflect .ValueOf (obj )
165
+ v := reflect .Indirect ( reflect . ValueOf (obj ) )
154
166
var config = make (FieldConfigArgument )
155
167
for i := 0 ; i < v .NumField (); i ++ {
156
- typeField := v .Type ().Field (i )
168
+ field := v .Type ().Field (i )
157
169
158
- mytag := extractTag (typeField .Tag )
170
+ mytag := extractTag (field .Tag )
159
171
if inArray (tags , mytag ) {
160
172
config [mytag ] = & ArgumentConfig {
161
- Type : getGraphType (typeField .Type ),
173
+ Type : getGraphType (field .Type ),
162
174
}
163
175
}
164
176
}
0 commit comments