Skip to content

Commit

Permalink
fix: field pointers in fieldIndexByName (#452)
Browse files Browse the repository at this point in the history
Allow pointers to structs in fieldIndexByName.

Also:
* Use reflect.Type.Elem() vs reflect.ValueOf(t).Elem().Type().
* Format bug_test.go to fix lint failure.
  • Loading branch information
stevenh authored Nov 25, 2022
1 parent 37f2928 commit 041379f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ func Test_issue266(t *testing.T) {
})
}

func Test_issue369 (t *testing.T) {
func Test_issue369(t *testing.T) {
tt(t, func() {
test, tester := test()

Expand Down
15 changes: 11 additions & 4 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,25 @@ func (self *_runtime) convertNumeric(v Value, t reflect.Type) reflect.Value {

func fieldIndexByName(t reflect.Type, name string) []int {
for t.Kind() == reflect.Ptr {
t = reflect.ValueOf(t).Elem().Type()
t = t.Elem()
}

for i := 0; i < t.NumField(); i++ {
f := t.Field(i)

if !validGoStructName(f.Name) {
continue
}

if f.Anonymous && f.Type.Kind() == reflect.Struct {
if a := fieldIndexByName(f.Type, name); a != nil {
return append([]int{i}, a...)
if f.Anonymous {
for t.Kind() == reflect.Ptr {
t = t.Elem()
}

if f.Type.Kind() == reflect.Struct {
if a := fieldIndexByName(f.Type, name); a != nil {
return append([]int{i}, a...)
}
}
}

Expand Down

0 comments on commit 041379f

Please sign in to comment.