From f4f297347f5667fb64fc49f3da8a5464f1021816 Mon Sep 17 00:00:00 2001 From: ryane Date: Tue, 18 Oct 2016 12:14:56 -0400 Subject: [PATCH] groups: add graph.IsRoot --- graph/ids.go | 5 +++++ graph/ids_test.go | 13 +++++++++++++ graph/merge_duplicates.go | 2 +- load/dependencyresolver.go | 4 ++-- load/resource.go | 2 +- render/preprocessor/preprocessor.go | 2 +- render/render.go | 2 +- render/renderer.go | 2 +- 8 files changed, 25 insertions(+), 7 deletions(-) diff --git a/graph/ids.go b/graph/ids.go index 7e37680fa..cb59d2e6c 100644 --- a/graph/ids.go +++ b/graph/ids.go @@ -48,3 +48,8 @@ func BaseID(id string) string { func IsDescendentID(parent, child string) bool { return parent != child && strings.HasPrefix(child, parent) } + +// IsRoot checks if the ID identifies the root +func IsRoot(id string) bool { + return id == "root" +} diff --git a/graph/ids_test.go b/graph/ids_test.go index d296a57f0..0a1075d7b 100644 --- a/graph/ids_test.go +++ b/graph/ids_test.go @@ -68,3 +68,16 @@ func TestIsDescendentIDNot(t *testing.T) { assert.False(t, graph.IsDescendentID("a/b", "a/c")) } + +// TestIsRoot tests the IsRoot function +func TestIsRoot(t *testing.T) { + t.Parallel() + + t.Run("is root", func(t *testing.T) { + assert.True(t, graph.IsRoot("root")) + }) + + t.Run("is not root", func(t *testing.T) { + assert.False(t, graph.IsRoot("root/module.test")) + }) +} diff --git a/graph/merge_duplicates.go b/graph/merge_duplicates.go index a23d537a8..55c288450 100644 --- a/graph/merge_duplicates.go +++ b/graph/merge_duplicates.go @@ -35,7 +35,7 @@ func MergeDuplicates(ctx context.Context, g *Graph, skip SkipMergeFunc) (*Graph, logger := logging.GetLogger(ctx).WithField("function", "MergeDuplicates") return g.RootFirstTransform(ctx, func(meta *node.Node, out *Graph) error { - if meta.ID == "root" { + if IsRoot(meta.ID) { return nil } diff --git a/load/dependencyresolver.go b/load/dependencyresolver.go index 8f7a0dadf..21725fce7 100644 --- a/load/dependencyresolver.go +++ b/load/dependencyresolver.go @@ -143,7 +143,7 @@ func getXrefs(g *graph.Graph, id string, node *parse.Node) (out []string, err er } func getPeerVertex(src, dst string) (string, bool) { - if dst == "." || dst == "root" { + if dst == "." || graph.IsRoot(dst) { return "", false } if graph.AreSiblingIDs(src, dst) { @@ -153,7 +153,7 @@ func getPeerVertex(src, dst string) (string, bool) { } func getNearestAncestor(g *graph.Graph, id, node string) (string, bool) { - if id == "root" || id == "" || id == "." { + if graph.IsRoot(id) || id == "" || id == "." { return "", false } diff --git a/load/resource.go b/load/resource.go index 35882139a..86916b33d 100644 --- a/load/resource.go +++ b/load/resource.go @@ -49,7 +49,7 @@ func SetResources(ctx context.Context, g *graph.Graph) (*graph.Graph, error) { logger.Debug("loading resources") return g.Transform(ctx, func(meta *node.Node, out *graph.Graph) error { - if meta.ID == "root" { + if graph.IsRoot(meta.ID) { return nil } diff --git a/render/preprocessor/preprocessor.go b/render/preprocessor/preprocessor.go index 608594f7f..995c09999 100644 --- a/render/preprocessor/preprocessor.go +++ b/render/preprocessor/preprocessor.go @@ -181,7 +181,7 @@ func VertexSplitTraverse(g *graph.Graph, toFind string, startingNode string, sto // VertexSplitTraverse and will cause vertex splitting to propogate upwards // until it encounters a module func TraverseUntilModule(g *graph.Graph, id string) bool { - if id == "root" { + if graph.IsRoot(id) { return true } elemMeta, ok := g.Get(id) diff --git a/render/render.go b/render/render.go index 7cee37c98..aa083da81 100644 --- a/render/render.go +++ b/render/render.go @@ -76,7 +76,7 @@ func Pipeline(g *graph.Graph, id string, factory *Factory, top Values) executor. // the node is a valide resource.Resource return it. If it's not root and not a // resource.Resource return an error. func (p pipelineGen) maybeTransformRoot(idi interface{}) (interface{}, error) { - if p.ID == "root" { + if graph.IsRoot(p.ID) { return module.NewPreparer(p.Top), nil } if res, ok := idi.(resource.Resource); ok { diff --git a/render/renderer.go b/render/renderer.go index 3adf5903a..b178368fd 100644 --- a/render/renderer.go +++ b/render/renderer.go @@ -74,7 +74,7 @@ func (r *Renderer) Render(name, src string) (string, error) { } func getNearestAncestor(g *graph.Graph, id, node string) (string, bool) { - if node == "root" || node == "" { + if graph.IsRoot(node) || node == "" { return "", false } siblingID := graph.SiblingID(id, node)