From 4e8e6cd66154fd385fead535c54cedf45ebdcc36 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 21 Sep 2016 11:33:47 -0700 Subject: [PATCH] dag: add EdgesFrom, EdgesTo, needs tests --- dag/graph.go | 26 +++++++++++++++++++++++++ terraform/transform_destroy_cbd.go | 3 ++- terraform/transform_destroy_cbd_test.go | 5 ++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/dag/graph.go b/dag/graph.go index 012118057..75bc5dbe5 100644 --- a/dag/graph.go +++ b/dag/graph.go @@ -48,6 +48,32 @@ func (g *Graph) Edges() []Edge { return result } +// EdgesFrom returns the list of edges from the given source. +func (g *Graph) EdgesFrom(v Vertex) []Edge { + var result []Edge + from := hashcode(v) + for _, e := range g.Edges() { + if hashcode(e.Source()) == from { + result = append(result, e) + } + } + + return result +} + +// EdgesTo returns the list of edges to the given target. +func (g *Graph) EdgesTo(v Vertex) []Edge { + var result []Edge + search := hashcode(v) + for _, e := range g.Edges() { + if hashcode(e.Target()) == search { + result = append(result, e) + } + } + + return result +} + // HasVertex checks if the given Vertex is present in the graph. func (g *Graph) HasVertex(v Vertex) bool { return g.vertices.Include(v) diff --git a/terraform/transform_destroy_cbd.go b/terraform/transform_destroy_cbd.go index b398920bc..d2abb7dc8 100644 --- a/terraform/transform_destroy_cbd.go +++ b/terraform/transform_destroy_cbd.go @@ -50,7 +50,8 @@ func (t *CBDEdgeTransformer) Transform(g *Graph) error { } // Find the destroy edge. There should only be one. - for _, e := range g.DownEdges(v).List() { + for _, e := range g.EdgesTo(v) { + log.Printf("WHAT: %#v", e) // Not a destroy edge, ignore it de, ok := e.(*DestroyEdge) if !ok { diff --git a/terraform/transform_destroy_cbd_test.go b/terraform/transform_destroy_cbd_test.go index 7df75dc3d..73314b60b 100644 --- a/terraform/transform_destroy_cbd_test.go +++ b/terraform/transform_destroy_cbd_test.go @@ -7,7 +7,8 @@ import ( func TestCBDEdgeTransformer(t *testing.T) { g := Graph{Path: RootModulePath} - g.Add(&graphNodeDestroyerTest{AddrString: "test.A"}) + g.Add(&graphNodeCreatorTest{AddrString: "test.A"}) + g.Add(&graphNodeDestroyerTest{AddrString: "test.A", CBD: true}) g.Add(&graphNodeDestroyerTest{AddrString: "test.B"}) { @@ -34,7 +35,9 @@ func TestCBDEdgeTransformer(t *testing.T) { } const testTransformCBDEdgeBasicStr = ` +test.A test.A (destroy) + test.A test.B (destroy) test.B (destroy) `