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

Fix utility function #264

Merged
merged 3 commits into from
Dec 23, 2017
Merged
Show file tree
Hide file tree
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
28 changes: 24 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ func BindFields(obj interface{}) Fields {
for i := 0; i < v.NumField(); i++ {
typeField := v.Type().Field(i)

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

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

Expand Down Expand Up @@ -63,8 +64,15 @@ func getGraphType(tipe reflect.Type) Output {
case reflect.String:
return String
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
return Int
case reflect.Float32:
fallthrough
case reflect.Float64:
return Float
case reflect.Bool:
Expand All @@ -79,13 +87,17 @@ func getGraphList(tipe reflect.Type) *List {
if tipe.Kind() == reflect.Slice {
switch tipe.Elem().Kind() {
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
return NewList(Int)
case reflect.Bool:
return NewList(Boolean)
case reflect.Float32:
fallthrough
case reflect.Float64:
return NewList(Float)
case reflect.String:
Expand Down Expand Up @@ -120,22 +132,30 @@ func extractValue(originTag string, obj interface{}) interface{} {
return res
}
}
curTag := typeField.Tag
if originTag == curTag.Get(TAG) {

if originTag == extractTag(typeField.Tag) {
return 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.ValueOf(obj)
var config = make(FieldConfigArgument)
for i := 0; i < v.NumField(); i++ {
typeField := v.Type().Field(i)

mytag := typeField.Tag.Get(TAG)
mytag := extractTag(typeField.Tag)
if inArray(tags, mytag) {
config[mytag] = &ArgumentConfig{
Type: getGraphType(typeField.Type),
Expand Down
7 changes: 4 additions & 3 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Person struct {
}

type Human struct {
Alive bool `json:"alive"`
Alive bool `json:"alive,omitempty"`
Age int `json:"age"`
Weight float64 `json:"weight"`
}
Expand All @@ -32,6 +32,7 @@ type Friend struct {
type Address struct {
Street string `json:"street"`
City string `json:"city"`
Test string `json:",omitempty"`
}

var personSource = Person{
Expand All @@ -46,7 +47,7 @@ var personSource = Person{
City: "Jakarta",
},
Friends: friendSource,
Hobbies:[]string{"eat","sleep","code"},
Hobbies: []string{"eat", "sleep", "code"},
}

var friendSource = []Friend{
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestBindFields(t *testing.T) {

newPerson := data.Data.Person
if !reflect.DeepEqual(newPerson, personSource) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(newPerson, personSource))
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(personSource, newPerson))
}
}

Expand Down