From a8a64c66c0c7c74d08c033fa15650c509a775128 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 14 Apr 2017 19:39:35 -0700 Subject: [PATCH] config/module: helper to visit all modules in a tree --- config/module/tree.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/config/module/tree.go b/config/module/tree.go index b6f90fd93..4b0b153f7 100644 --- a/config/module/tree.go +++ b/config/module/tree.go @@ -92,6 +92,25 @@ func (t *Tree) Children() map[string]*Tree { return t.children } +// DeepEach calls the provided callback for the receiver and then all of +// its descendents in the tree, allowing an operation to be performed on +// all modules in the tree. +// +// Parents will be visited before their children but otherwise the order is +// not defined. +func (t *Tree) DeepEach(cb func(*Tree)) { + t.lock.RLock() + defer t.lock.RUnlock() + t.deepEach(cb) +} + +func (t *Tree) deepEach(cb func(*Tree)) { + cb(t) + for _, c := range t.children { + c.deepEach(cb) + } +} + // Loaded says whether or not this tree has been loaded or not yet. func (t *Tree) Loaded() bool { t.lock.RLock()