Skip to content

Commit

Permalink
fixes field selection priority
Browse files Browse the repository at this point in the history
  • Loading branch information
codyleyhan committed Aug 24, 2018
1 parent 9ec385d commit 7cfd977
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func findMethod(typ *types.Named, name string) *types.Func {
// 3. Actual Field name
func findField(typ *types.Struct, name, structTag string) (*types.Var, error) {
var foundField *types.Var
foundFieldWasTag := false

for i := 0; i < typ.NumFields(); i++ {
field := typ.Field(i)
Expand All @@ -120,9 +121,12 @@ func findField(typ *types.Struct, name, structTag string) (*types.Var, error) {
tags := reflect.StructTag(typ.Tag(i))
if val, ok := tags.Lookup(structTag); ok {
if strings.EqualFold(val, name) {
if foundField != nil {
if foundField != nil && foundFieldWasTag {
return nil, errors.Errorf("tag %s is ambigious; multiple fields have the same tag value of %s", structTag, val)
}

foundField = field
foundFieldWasTag = true
}
}
}
Expand Down Expand Up @@ -156,14 +160,6 @@ func findField(typ *types.Struct, name, structTag string) (*types.Var, error) {
if strings.EqualFold(field.Name(), name) && foundField == nil {
foundField = field
}

tags := reflect.StructTag(typ.Tag(i))

if val, ok := tags.Lookup("json"); ok {
if strings.EqualFold(val, name) {
return field
}
}
}

if foundField == nil {
Expand Down

0 comments on commit 7cfd977

Please sign in to comment.