Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pointer support to BindFields #311

Merged
merged 1 commit into from
Apr 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,33 @@ const TAG = "json"
// }
// it will throw panic stack-overflow
func BindFields(obj interface{}) Fields {
t := reflect.TypeOf(obj)
v := reflect.ValueOf(obj)
fields := make(map[string]*Field)

for i := 0; i < v.NumField(); i++ {
typeField := v.Type().Field(i)
if t.Kind() == reflect.Ptr {
t = t.Elem()
v = v.Elem()
}

for i := 0; i < t.NumField(); i++ {
field := t.Field(i)

tag := extractTag(typeField.Tag)
tag := extractTag(field.Tag)
if tag == "-" {
continue
}

var graphType Output
if typeField.Type.Kind() == reflect.Struct {
fieldType := field.Type

if fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
}

var graphType Output
if fieldType.Kind() == reflect.Struct {
structFields := BindFields(v.Field(i).Interface())

if tag == "" {
fields = appendFields(fields, structFields)
continue
Expand All @@ -46,7 +58,7 @@ func BindFields(obj interface{}) Fields {
}

if graphType == nil {
graphType = getGraphType(typeField.Type)
graphType = getGraphType(fieldType)
}
fields[tag] = &Field{
Type: graphType,
Expand Down Expand Up @@ -122,19 +134,19 @@ func appendFields(dest, origin Fields) Fields {
}

func extractValue(originTag string, obj interface{}) interface{} {
val := reflect.ValueOf(obj)
val := reflect.Indirect(reflect.ValueOf(obj))

for j := 0; j < val.NumField(); j++ {
typeField := val.Type().Field(j)
if typeField.Type.Kind() == reflect.Struct {
field := val.Type().Field(j)
if field.Type.Kind() == reflect.Struct {
res := extractValue(originTag, val.Field(j).Interface())
if res != nil {
return res
}
}

if originTag == extractTag(typeField.Tag) {
return val.Field(j).Interface()
if originTag == extractTag(field.Tag) {
return reflect.Indirect(val.Field(j)).Interface()
}
}
return nil
Expand All @@ -150,15 +162,15 @@ func extractTag(tag reflect.StructTag) string {

// lazy way of binding args
func BindArg(obj interface{}, tags ...string) FieldConfigArgument {
v := reflect.ValueOf(obj)
v := reflect.Indirect(reflect.ValueOf(obj))
var config = make(FieldConfigArgument)
for i := 0; i < v.NumField(); i++ {
typeField := v.Type().Field(i)
field := v.Type().Field(i)

mytag := extractTag(typeField.Tag)
mytag := extractTag(field.Tag)
if inArray(tags, mytag) {
config[mytag] = &ArgumentConfig{
Type: getGraphType(typeField.Type),
Type: getGraphType(field.Type),
}
}
}
Expand Down