Skip to content

Commit

Permalink
groups: add graph.IsRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
ryane committed Oct 18, 2016
1 parent 941ad14 commit f4f2973
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions graph/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
13 changes: 13 additions & 0 deletions graph/ids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
}
2 changes: 1 addition & 1 deletion graph/merge_duplicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions load/dependencyresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion load/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion render/preprocessor/preprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion render/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f4f2973

Please sign in to comment.