config/module: NewTreeModule is easier to use

This commit is contained in:
Mitchell Hashimoto 2014-09-15 15:49:07 -07:00
parent c0a30d3337
commit 7bbf6a0d3a
2 changed files with 26 additions and 16 deletions

View File

@ -42,8 +42,20 @@ const (
) )
// NewTree returns a new Tree for the given config structure. // NewTree returns a new Tree for the given config structure.
func NewTree(c *config.Config) *Tree { func NewTree(name string, c *config.Config) *Tree {
return &Tree{config: c} return &Tree{config: c, name: name}
}
// NewTreeModule is like NewTree except it parses the configuration in
// the directory and gives it a specific name. Use a blank name "" to specify
// the root module.
func NewTreeModule(name, dir string) (*Tree, error) {
c, err := config.LoadDir(dir)
if err != nil {
return nil, err
}
return NewTree(name, c), nil
} }
// Children returns the children of this tree (the modules that are // Children returns the children of this tree (the modules that are
@ -141,14 +153,11 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
} }
// Load the configuration // Load the configuration
c, err := config.LoadDir(dir) children[m.Name], err = NewTreeModule(m.Name, dir)
if err != nil { if err != nil {
return fmt.Errorf( return fmt.Errorf(
"module %s: %s", m.Name, err) "module %s: %s", m.Name, err)
} }
children[m.Name] = NewTree(c)
children[m.Name].name = m.Name
} }
// Go through all the children and load them. // Go through all the children and load them.

View File

@ -8,7 +8,7 @@ import (
func TestTreeLoad(t *testing.T) { func TestTreeLoad(t *testing.T) {
storage := testStorage(t) storage := testStorage(t)
tree := NewTree(testConfig(t, "basic")) tree := NewTree("", testConfig(t, "basic"))
if tree.Loaded() { if tree.Loaded() {
t.Fatal("should not be loaded") t.Fatal("should not be loaded")
@ -46,7 +46,7 @@ func TestTreeLoad(t *testing.T) {
func TestTreeLoad_duplicate(t *testing.T) { func TestTreeLoad_duplicate(t *testing.T) {
storage := testStorage(t) storage := testStorage(t)
tree := NewTree(testConfig(t, "dup")) tree := NewTree("", testConfig(t, "dup"))
if tree.Loaded() { if tree.Loaded() {
t.Fatal("should not be loaded") t.Fatal("should not be loaded")
@ -59,7 +59,7 @@ func TestTreeLoad_duplicate(t *testing.T) {
} }
func TestTreeModules(t *testing.T) { func TestTreeModules(t *testing.T) {
tree := NewTree(testConfig(t, "basic")) tree := NewTree("", testConfig(t, "basic"))
actual := tree.Modules() actual := tree.Modules()
expected := []*Module{ expected := []*Module{
@ -72,7 +72,7 @@ func TestTreeModules(t *testing.T) {
} }
func TestTreeName(t *testing.T) { func TestTreeName(t *testing.T) {
tree := NewTree(testConfig(t, "basic")) tree := NewTree("", testConfig(t, "basic"))
actual := tree.Name() actual := tree.Name()
if actual != "<root>" { if actual != "<root>" {
@ -81,7 +81,7 @@ func TestTreeName(t *testing.T) {
} }
func TestTreeValidate_badChild(t *testing.T) { func TestTreeValidate_badChild(t *testing.T) {
tree := NewTree(testConfig(t, "validate-child-bad")) tree := NewTree("", testConfig(t, "validate-child-bad"))
if err := tree.Load(testStorage(t), GetModeGet); err != nil { if err := tree.Load(testStorage(t), GetModeGet); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -93,7 +93,7 @@ func TestTreeValidate_badChild(t *testing.T) {
} }
func TestTreeValidate_badChildOutput(t *testing.T) { func TestTreeValidate_badChildOutput(t *testing.T) {
tree := NewTree(testConfig(t, "validate-bad-output")) tree := NewTree("", testConfig(t, "validate-bad-output"))
if err := tree.Load(testStorage(t), GetModeGet); err != nil { if err := tree.Load(testStorage(t), GetModeGet); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -105,7 +105,7 @@ func TestTreeValidate_badChildOutput(t *testing.T) {
} }
func TestTreeValidate_badChildVar(t *testing.T) { func TestTreeValidate_badChildVar(t *testing.T) {
tree := NewTree(testConfig(t, "validate-bad-var")) tree := NewTree("", testConfig(t, "validate-bad-var"))
if err := tree.Load(testStorage(t), GetModeGet); err != nil { if err := tree.Load(testStorage(t), GetModeGet); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -117,7 +117,7 @@ func TestTreeValidate_badChildVar(t *testing.T) {
} }
func TestTreeValidate_badRoot(t *testing.T) { func TestTreeValidate_badRoot(t *testing.T) {
tree := NewTree(testConfig(t, "validate-root-bad")) tree := NewTree("", testConfig(t, "validate-root-bad"))
if err := tree.Load(testStorage(t), GetModeGet); err != nil { if err := tree.Load(testStorage(t), GetModeGet); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -129,7 +129,7 @@ func TestTreeValidate_badRoot(t *testing.T) {
} }
func TestTreeValidate_good(t *testing.T) { func TestTreeValidate_good(t *testing.T) {
tree := NewTree(testConfig(t, "validate-child-good")) tree := NewTree("", testConfig(t, "validate-child-good"))
if err := tree.Load(testStorage(t), GetModeGet); err != nil { if err := tree.Load(testStorage(t), GetModeGet); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -141,13 +141,14 @@ func TestTreeValidate_good(t *testing.T) {
} }
func TestTreeValidate_notLoaded(t *testing.T) { func TestTreeValidate_notLoaded(t *testing.T) {
tree := NewTree(testConfig(t, "basic")) tree := NewTree("", testConfig(t, "basic"))
if err := tree.Validate(); err == nil { if err := tree.Validate(); err == nil {
t.Fatal("should error") t.Fatal("should error")
} }
} }
const treeLoadStr = ` const treeLoadStr = `
<root> <root>
foo foo