diff --git a/config/module/tree.go b/config/module/tree.go index fbc467317..08f73b0e4 100644 --- a/config/module/tree.go +++ b/config/module/tree.go @@ -23,6 +23,7 @@ type Tree struct { name string config *config.Config children map[string]*Tree + path []string lock sync.RWMutex } @@ -187,6 +188,11 @@ func (t *Tree) Load(s Storage, mode GetMode) error { return fmt.Errorf( "module %s: %s", m.Name, err) } + + // Set the path of this child + path := make([]string, len(t.path), len(t.path)+1) + copy(path, t.path) + children[m.Name].path = append(path, m.Name) } // Go through all the children and load them. @@ -202,10 +208,19 @@ func (t *Tree) Load(s Storage, mode GetMode) error { return nil } +// Path is the full path to this tree. +func (t *Tree) Path() []string { + return t.path +} + // String gives a nice output to describe the tree. func (t *Tree) String() string { var result bytes.Buffer - result.WriteString(t.Name() + "\n") + path := strings.Join(t.path, ", ") + if path != "" { + path = fmt.Sprintf(" (path: %s)", path) + } + result.WriteString(t.Name() + path + "\n") cs := t.Children() if cs == nil { diff --git a/config/module/tree_gob.go b/config/module/tree_gob.go index cbf8a25ed..fcd37f4e7 100644 --- a/config/module/tree_gob.go +++ b/config/module/tree_gob.go @@ -22,6 +22,7 @@ func (t *Tree) GobDecode(bs []byte) error { t.name = data.Name t.config = data.Config t.children = data.Children + t.path = data.Path return nil } @@ -31,6 +32,7 @@ func (t *Tree) GobEncode() ([]byte, error) { Config: t.config, Children: t.children, Name: t.name, + Path: t.path, } var buf bytes.Buffer @@ -51,4 +53,5 @@ type treeGob struct { Config *config.Config Children map[string]*Tree Name string + Path []string } diff --git a/config/module/tree_test.go b/config/module/tree_test.go index 9ac0c582d..9667c0157 100644 --- a/config/module/tree_test.go +++ b/config/module/tree_test.go @@ -18,6 +18,8 @@ func TestTreeChild(t *testing.T) { t.Fatal("should not be nil") } else if c.Name() != "root" { t.Fatalf("bad: %#v", c.Name()) + } else if !reflect.DeepEqual(c.Path(), []string(nil)) { + t.Fatalf("bad: %#v", c.Path()) } // Should be able to get the root child @@ -25,6 +27,8 @@ func TestTreeChild(t *testing.T) { t.Fatal("should not be nil") } else if c.Name() != "root" { t.Fatalf("bad: %#v", c.Name()) + } else if !reflect.DeepEqual(c.Path(), []string(nil)) { + t.Fatalf("bad: %#v", c.Path()) } // Should be able to get the foo child @@ -32,6 +36,8 @@ func TestTreeChild(t *testing.T) { t.Fatal("should not be nil") } else if c.Name() != "foo" { t.Fatalf("bad: %#v", c.Name()) + } else if !reflect.DeepEqual(c.Path(), []string{"foo"}) { + t.Fatalf("bad: %#v", c.Path()) } // Should be able to get the nested child @@ -39,6 +45,8 @@ func TestTreeChild(t *testing.T) { t.Fatal("should not be nil") } else if c.Name() != "bar" { t.Fatalf("bad: %#v", c.Name()) + } else if !reflect.DeepEqual(c.Path(), []string{"foo", "bar"}) { + t.Fatalf("bad: %#v", c.Path()) } } @@ -274,16 +282,16 @@ func TestTreeValidate_requiredChildVar(t *testing.T) { const treeLoadStr = ` root - foo + foo (path: foo) ` const treeLoadParentStr = ` root - a - b + a (path: a) + b (path: a, b) ` const treeLoadSubdirStr = ` root - foo - bar + foo (path: foo) + bar (path: foo, bar) `