Skip to content

Commit

Permalink
Merge remote-tracking branch 'tnd/reflect-recursive'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoiron committed Dec 2, 2016
2 parents 0b01d1c + 059ea5d commit 43975e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions reflectx/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,19 @@ func getMapping(t reflect.Type, tagName string, mapFunc, tagMapFunc mapf) *Struc
queue := []typeQueue{}
queue = append(queue, typeQueue{Deref(t), root, ""})

QueueLoop:
for len(queue) != 0 {
// pop the first item off of the queue
tq := queue[0]
queue = queue[1:]

// ignore recursive field
for p := tq.fi.Parent; p != nil; p = p.Parent {
if tq.fi.Field.Type == p.Field.Type {
continue QueueLoop
}
}

nChildren := 0
if tq.t.Kind() == reflect.Struct {
nChildren = tq.t.NumField()
Expand Down
9 changes: 9 additions & 0 deletions reflectx/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ func TestInlineStruct(t *testing.T) {
}
}

func TestRecursiveStruct(t *testing.T) {
type Person struct {
Parent *Person
}
m := NewMapperFunc("db", strings.ToLower)
var p *Person
m.TypeMap(reflect.TypeOf(p))
}

func TestFieldsEmbedded(t *testing.T) {
m := NewMapper("db")

Expand Down

0 comments on commit 43975e6

Please sign in to comment.