Skip to content

Commit 675936e

Browse files
chris-ramongstarikov
authored andcommitted
add default fields tag
2 parents dd05d5e + 5088e2a commit 675936e

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

examples/sql-nullstring/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ query {
192192
log.Fatal(r1)
193193
}
194194
b1, err := json.MarshalIndent(r1, "", " ")
195+
if err != nil {
196+
log.Fatal(err)
197+
}
195198
b2, err := json.MarshalIndent(r2, "", " ")
196199
if err != nil {
197200
log.Fatal(err)

util.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func BindFields(obj interface{}) Fields {
2828
for i := 0; i < t.NumField(); i++ {
2929
field := t.Field(i)
3030

31-
tag := extractTag(field.Tag)
31+
tag := extractTag(field)
3232
if tag == "-" {
3333
continue
3434
}
@@ -130,7 +130,7 @@ func extractValue(originTag string, obj interface{}) interface{} {
130130

131131
for j := 0; j < val.NumField(); j++ {
132132
field := val.Type().Field(j)
133-
found := originTag == extractTag(field.Tag)
133+
found := originTag == extractTag(field)
134134
if field.Type.Kind() == reflect.Struct {
135135
itf := val.Field(j).Interface()
136136

@@ -152,10 +152,14 @@ func extractValue(originTag string, obj interface{}) interface{} {
152152
return nil
153153
}
154154

155-
func extractTag(tag reflect.StructTag) string {
156-
t := tag.Get(TAG)
157-
if t != "" {
155+
func extractTag(strct reflect.StructField) string {
156+
t := strct.Tag.Get(TAG)
157+
switch {
158+
case t != "": //work as tags specified
158159
t = strings.Split(t, ",")[0]
160+
case strct.Anonymous: //embedding
161+
default:
162+
t = strings.ToLower(strct.Name)
159163
}
160164
return t
161165
}
@@ -167,7 +171,7 @@ func BindArg(obj interface{}, tags ...string) FieldConfigArgument {
167171
for i := 0; i < v.NumField(); i++ {
168172
field := v.Type().Field(i)
169173

170-
mytag := extractTag(field.Tag)
174+
mytag := extractTag(field)
171175
if inArray(tags, mytag) {
172176
config[mytag] = &ArgumentConfig{
173177
Type: getGraphType(field.Type),

util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
type Person struct {
1515
Human
16-
Name string `json:"name"`
16+
Name string //`json:"name"` - no tag, test default field name
1717
Home Address `json:"home"`
1818
Hobbies []string `json:"hobbies"`
1919
Friends []Friend `json:"friends"`

0 commit comments

Comments
 (0)