config/module: Can look up Child with Tree.Child
This commit is contained in:
parent
679ab1d515
commit
267d45df86
|
@ -0,0 +1,2 @@
|
|||
# Hello
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
# Hello
|
||||
|
||||
module "bar" {
|
||||
source = "./bar"
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
# Hello
|
||||
|
||||
module "foo" {
|
||||
source = "./foo"
|
||||
}
|
|
@ -67,6 +67,24 @@ func (t *Tree) Config() *config.Config {
|
|||
return t.config
|
||||
}
|
||||
|
||||
// Child returns the child with the given path (by name).
|
||||
func (t *Tree) Child(path []string) *Tree {
|
||||
if len(path) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
c := t.Children()[path[0]]
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(path) == 1 {
|
||||
return c
|
||||
}
|
||||
|
||||
return c.Child(path[1:])
|
||||
}
|
||||
|
||||
// Children returns the children of this tree (the modules that are
|
||||
// imported by this root).
|
||||
//
|
||||
|
|
|
@ -6,6 +6,28 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestTreeChild(t *testing.T) {
|
||||
storage := testStorage(t)
|
||||
tree := NewTree("", testConfig(t, "child"))
|
||||
if err := tree.Load(storage, GetModeGet); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Should be able to get the foo child
|
||||
if c := tree.Child([]string{"foo"}); c == nil {
|
||||
t.Fatal("should not be nil")
|
||||
} else if c.Name() != "foo" {
|
||||
t.Fatalf("bad: %#v", c.Name())
|
||||
}
|
||||
|
||||
// Should be able to get the nested child
|
||||
if c := tree.Child([]string{"foo", "bar"}); c == nil {
|
||||
t.Fatal("should not be nil")
|
||||
} else if c.Name() != "bar" {
|
||||
t.Fatalf("bad: %#v", c.Name())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreeLoad(t *testing.T) {
|
||||
storage := testStorage(t)
|
||||
tree := NewTree("", testConfig(t, "basic"))
|
||||
|
|
Loading…
Reference in New Issue