config/module: store the path with the module

This commit is contained in:
Mitchell Hashimoto 2015-04-07 16:37:46 -07:00
parent 4888c18b61
commit f084d8d932
3 changed files with 32 additions and 6 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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)
`