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

BindFields: added default fields tag #589

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func BindFields(obj interface{}) Fields {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)

tag := extractTag(field.Tag)
tag := extractTag(field)
if tag == "-" {
continue
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func extractValue(originTag string, obj interface{}) interface{} {

for j := 0; j < val.NumField(); j++ {
field := val.Type().Field(j)
found := originTag == extractTag(field.Tag)
found := originTag == extractTag(field)
if field.Type.Kind() == reflect.Struct {
itf := val.Field(j).Interface()

Expand All @@ -152,10 +152,14 @@ func extractValue(originTag string, obj interface{}) interface{} {
return nil
}

func extractTag(tag reflect.StructTag) string {
t := tag.Get(TAG)
if t != "" {
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
}
Expand All @@ -167,7 +171,7 @@ func BindArg(obj interface{}, tags ...string) FieldConfigArgument {
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)

mytag := extractTag(field.Tag)
mytag := extractTag(field)
if inArray(tags, mytag) {
config[mytag] = &ArgumentConfig{
Type: getGraphType(field.Type),
Expand Down
2 changes: 1 addition & 1 deletion util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type Person struct {
Human
Name string `json:"name"`
Name string //`json:"name"` - no tag, test default field name
Home Address `json:"home"`
Hobbies []string `json:"hobbies"`
Friends []Friend `json:"friends"`
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.