config/module: store the path with the module
This commit is contained in:
parent
4888c18b61
commit
f084d8d932
|
@ -23,6 +23,7 @@ type Tree struct {
|
||||||
name string
|
name string
|
||||||
config *config.Config
|
config *config.Config
|
||||||
children map[string]*Tree
|
children map[string]*Tree
|
||||||
|
path []string
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,6 +188,11 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"module %s: %s", m.Name, err)
|
"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.
|
// Go through all the children and load them.
|
||||||
|
@ -202,10 +208,19 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
|
||||||
return nil
|
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.
|
// String gives a nice output to describe the tree.
|
||||||
func (t *Tree) String() string {
|
func (t *Tree) String() string {
|
||||||
var result bytes.Buffer
|
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()
|
cs := t.Children()
|
||||||
if cs == nil {
|
if cs == nil {
|
||||||
|
|
|
@ -22,6 +22,7 @@ func (t *Tree) GobDecode(bs []byte) error {
|
||||||
t.name = data.Name
|
t.name = data.Name
|
||||||
t.config = data.Config
|
t.config = data.Config
|
||||||
t.children = data.Children
|
t.children = data.Children
|
||||||
|
t.path = data.Path
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -31,6 +32,7 @@ func (t *Tree) GobEncode() ([]byte, error) {
|
||||||
Config: t.config,
|
Config: t.config,
|
||||||
Children: t.children,
|
Children: t.children,
|
||||||
Name: t.name,
|
Name: t.name,
|
||||||
|
Path: t.path,
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
@ -51,4 +53,5 @@ type treeGob struct {
|
||||||
Config *config.Config
|
Config *config.Config
|
||||||
Children map[string]*Tree
|
Children map[string]*Tree
|
||||||
Name string
|
Name string
|
||||||
|
Path []string
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ func TestTreeChild(t *testing.T) {
|
||||||
t.Fatal("should not be nil")
|
t.Fatal("should not be nil")
|
||||||
} else if c.Name() != "root" {
|
} else if c.Name() != "root" {
|
||||||
t.Fatalf("bad: %#v", c.Name())
|
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
|
// Should be able to get the root child
|
||||||
|
@ -25,6 +27,8 @@ func TestTreeChild(t *testing.T) {
|
||||||
t.Fatal("should not be nil")
|
t.Fatal("should not be nil")
|
||||||
} else if c.Name() != "root" {
|
} else if c.Name() != "root" {
|
||||||
t.Fatalf("bad: %#v", c.Name())
|
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
|
// Should be able to get the foo child
|
||||||
|
@ -32,6 +36,8 @@ func TestTreeChild(t *testing.T) {
|
||||||
t.Fatal("should not be nil")
|
t.Fatal("should not be nil")
|
||||||
} else if c.Name() != "foo" {
|
} else if c.Name() != "foo" {
|
||||||
t.Fatalf("bad: %#v", c.Name())
|
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
|
// Should be able to get the nested child
|
||||||
|
@ -39,6 +45,8 @@ func TestTreeChild(t *testing.T) {
|
||||||
t.Fatal("should not be nil")
|
t.Fatal("should not be nil")
|
||||||
} else if c.Name() != "bar" {
|
} else if c.Name() != "bar" {
|
||||||
t.Fatalf("bad: %#v", c.Name())
|
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 = `
|
const treeLoadStr = `
|
||||||
root
|
root
|
||||||
foo
|
foo (path: foo)
|
||||||
`
|
`
|
||||||
|
|
||||||
const treeLoadParentStr = `
|
const treeLoadParentStr = `
|
||||||
root
|
root
|
||||||
a
|
a (path: a)
|
||||||
b
|
b (path: a, b)
|
||||||
`
|
`
|
||||||
const treeLoadSubdirStr = `
|
const treeLoadSubdirStr = `
|
||||||
root
|
root
|
||||||
foo
|
foo (path: foo)
|
||||||
bar
|
bar (path: foo, bar)
|
||||||
`
|
`
|
||||||
|
|
Loading…
Reference in New Issue