Skip to content

Commit

Permalink
add default fields tag
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ramon authored and gstarikov committed Dec 14, 2020
2 parents dd05d5e + 5088e2a commit 675936e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
3 changes: 3 additions & 0 deletions examples/sql-nullstring/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ query {
log.Fatal(r1)
}
b1, err := json.MarshalIndent(r1, "", " ")
if err != nil {
log.Fatal(err)
}
b2, err := json.MarshalIndent(r2, "", " ")
if err != nil {
log.Fatal(err)
Expand Down
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

0 comments on commit 675936e

Please sign in to comment.