config/module: Child(nil) or empty will return self

This commit is contained in:
Mitchell Hashimoto 2014-10-07 20:02:18 -07:00
parent 267d45df86
commit 1e00b4386c
2 changed files with 15 additions and 5 deletions

View File

@ -70,7 +70,7 @@ func (t *Tree) Config() *config.Config {
// Child returns the child with the given path (by name).
func (t *Tree) Child(path []string) *Tree {
if len(path) == 0 {
return nil
return t
}
c := t.Children()[path[0]]
@ -78,10 +78,6 @@ func (t *Tree) Child(path []string) *Tree {
return nil
}
if len(path) == 1 {
return c
}
return c.Child(path[1:])
}

View File

@ -13,6 +13,20 @@ func TestTreeChild(t *testing.T) {
t.Fatalf("err: %s", err)
}
// Should be able to get the root child
if c := tree.Child([]string{}); c == nil {
t.Fatal("should not be nil")
} else if c.Name() != "root" {
t.Fatalf("bad: %#v", c.Name())
}
// Should be able to get the root child
if c := tree.Child(nil); c == nil {
t.Fatal("should not be nil")
} else if c.Name() != "root" {
t.Fatalf("bad: %#v", c.Name())
}
// Should be able to get the foo child
if c := tree.Child([]string{"foo"}); c == nil {
t.Fatal("should not be nil")