Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit f084d8d

Browse files
committed
config/module: store the path with the module
1 parent 4888c18 commit f084d8d

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

config/module/tree.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Tree struct {
2323
name string
2424
config *config.Config
2525
children map[string]*Tree
26+
path []string
2627
lock sync.RWMutex
2728
}
2829

@@ -187,6 +188,11 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
187188
return fmt.Errorf(
188189
"module %s: %s", m.Name, err)
189190
}
191+
192+
// Set the path of this child
193+
path := make([]string, len(t.path), len(t.path)+1)
194+
copy(path, t.path)
195+
children[m.Name].path = append(path, m.Name)
190196
}
191197

192198
// Go through all the children and load them.
@@ -202,10 +208,19 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
202208
return nil
203209
}
204210

211+
// Path is the full path to this tree.
212+
func (t *Tree) Path() []string {
213+
return t.path
214+
}
215+
205216
// String gives a nice output to describe the tree.
206217
func (t *Tree) String() string {
207218
var result bytes.Buffer
208-
result.WriteString(t.Name() + "\n")
219+
path := strings.Join(t.path, ", ")
220+
if path != "" {
221+
path = fmt.Sprintf(" (path: %s)", path)
222+
}
223+
result.WriteString(t.Name() + path + "\n")
209224

210225
cs := t.Children()
211226
if cs == nil {

config/module/tree_gob.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (t *Tree) GobDecode(bs []byte) error {
2222
t.name = data.Name
2323
t.config = data.Config
2424
t.children = data.Children
25+
t.path = data.Path
2526

2627
return nil
2728
}
@@ -31,6 +32,7 @@ func (t *Tree) GobEncode() ([]byte, error) {
3132
Config: t.config,
3233
Children: t.children,
3334
Name: t.name,
35+
Path: t.path,
3436
}
3537

3638
var buf bytes.Buffer
@@ -51,4 +53,5 @@ type treeGob struct {
5153
Config *config.Config
5254
Children map[string]*Tree
5355
Name string
56+
Path []string
5457
}

config/module/tree_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,35 @@ func TestTreeChild(t *testing.T) {
1818
t.Fatal("should not be nil")
1919
} else if c.Name() != "root" {
2020
t.Fatalf("bad: %#v", c.Name())
21+
} else if !reflect.DeepEqual(c.Path(), []string(nil)) {
22+
t.Fatalf("bad: %#v", c.Path())
2123
}
2224

2325
// Should be able to get the root child
2426
if c := tree.Child(nil); c == nil {
2527
t.Fatal("should not be nil")
2628
} else if c.Name() != "root" {
2729
t.Fatalf("bad: %#v", c.Name())
30+
} else if !reflect.DeepEqual(c.Path(), []string(nil)) {
31+
t.Fatalf("bad: %#v", c.Path())
2832
}
2933

3034
// Should be able to get the foo child
3135
if c := tree.Child([]string{"foo"}); c == nil {
3236
t.Fatal("should not be nil")
3337
} else if c.Name() != "foo" {
3438
t.Fatalf("bad: %#v", c.Name())
39+
} else if !reflect.DeepEqual(c.Path(), []string{"foo"}) {
40+
t.Fatalf("bad: %#v", c.Path())
3541
}
3642

3743
// Should be able to get the nested child
3844
if c := tree.Child([]string{"foo", "bar"}); c == nil {
3945
t.Fatal("should not be nil")
4046
} else if c.Name() != "bar" {
4147
t.Fatalf("bad: %#v", c.Name())
48+
} else if !reflect.DeepEqual(c.Path(), []string{"foo", "bar"}) {
49+
t.Fatalf("bad: %#v", c.Path())
4250
}
4351
}
4452

@@ -274,16 +282,16 @@ func TestTreeValidate_requiredChildVar(t *testing.T) {
274282

275283
const treeLoadStr = `
276284
root
277-
foo
285+
foo (path: foo)
278286
`
279287

280288
const treeLoadParentStr = `
281289
root
282-
a
283-
b
290+
a (path: a)
291+
b (path: a, b)
284292
`
285293
const treeLoadSubdirStr = `
286294
root
287-
foo
288-
bar
295+
foo (path: foo)
296+
bar (path: foo, bar)
289297
`

0 commit comments

Comments
 (0)