Skip to content

Commit f5609c8

Browse files
author
Joakim Lundborg
committed
Fix NumField panic when handling embedded structs
1 parent 344b1e9 commit f5609c8

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

reflectx/reflect.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,14 @@ func getMapping(t reflect.Type, tagName string, mapFunc, tagMapFunc func(string)
270270
// pop the first item off of the queue
271271
tq := queue[0]
272272
queue = queue[1:]
273-
tq.fi.Children = make([]*FieldInfo, tq.t.NumField())
273+
nChildren := 0
274+
if tq.t.Kind() == reflect.Struct {
275+
nChildren = tq.t.NumField()
276+
}
277+
tq.fi.Children = make([]*FieldInfo, nChildren)
274278

275279
// iterate through all of its fields
276-
for fieldPos := 0; fieldPos < tq.t.NumField(); fieldPos++ {
280+
for fieldPos := 0; fieldPos < nChildren; fieldPos++ {
277281
f := tq.t.Field(fieldPos)
278282

279283
fi := FieldInfo{}
@@ -335,7 +339,12 @@ func getMapping(t reflect.Type, tagName string, mapFunc, tagMapFunc func(string)
335339

336340
fi.Embedded = true
337341
fi.Index = apnd(tq.fi.Index, fieldPos)
338-
fi.Children = make([]*FieldInfo, Deref(f.Type).NumField())
342+
nChildren := 0
343+
ft := Deref(f.Type)
344+
if ft.Kind() == reflect.Struct {
345+
nChildren = ft.NumField()
346+
}
347+
fi.Children = make([]*FieldInfo, nChildren)
339348
queue = append(queue, typeQueue{Deref(f.Type), &fi, pp})
340349
} else if fi.Zero.Kind() == reflect.Struct {
341350
fi.Index = apnd(tq.fi.Index, fieldPos)

0 commit comments

Comments
 (0)