From 1d5095cf6789fc26c7eface8b68d747d85dbf02e Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 18 Jul 2019 17:44:47 +0000 Subject: plugin/file: Rename do to walk and cleanup and document (#2987) * plugin/file: Rename do to walk, cleanup and document * This renames Do to Walk to be more inline with Go standards. Also make it return an error instead of a bool. Also give give walk access to rrs. Alternatively e.m could be exported, but just access the map of rrs should work as well. Another alternative would be adding a whole bunch of helper functions, but those need grab and return the data. Just having access to the rrs should be easiest for most Walks. * It adds Type and TypeForWildcard to show the different functions * *Removes* the identical RR check when inserting; this was only done for A, AAAA and MX and not finished; removed under the mantra garbage in garbage out. * Reuses Types to return all the types in an *tree.Elem Signed-off-by: Miek Gieben * better comments Signed-off-by: Miek Gieben --- plugin/file/tree/walk.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 plugin/file/tree/walk.go (limited to 'plugin/file/tree/walk.go') diff --git a/plugin/file/tree/walk.go b/plugin/file/tree/walk.go new file mode 100644 index 000000000..00accbafa --- /dev/null +++ b/plugin/file/tree/walk.go @@ -0,0 +1,30 @@ +package tree + +import "github.com/miekg/dns" + +// Walk performs fn on all values stored in the tree. If a non-nil error is returned the +// Walk was interrupted by an fn returning that error. If fn alters stored values' sort +// relationships, future tree operation behaviors are undefined. +func (t *Tree) Walk(fn func(e *Elem, rrs map[uint16][]dns.RR) error) error { + if t.Root == nil { + return nil + } + return t.Root.walk(fn) +} + +func (n *Node) walk(fn func(e *Elem, rrs map[uint16][]dns.RR) error) error { + if n.Left != nil { + if err := n.Left.walk(fn); err != nil { + return err + } + } + if err := fn(n.Elem, n.Elem.m); err != nil { + return err + } + if n.Right != nil { + if err := n.Right.walk(fn); err != nil { + return err + } + } + return nil +} -- cgit v1.2.3