From 90cfade62698c8c68fd82019b7955aa65bb001e8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 30 Apr 2015 17:02:39 -0700 Subject: [PATCH] terraform: naive flatten --- terraform/transform_flatten.go | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 terraform/transform_flatten.go diff --git a/terraform/transform_flatten.go b/terraform/transform_flatten.go new file mode 100644 index 000000000..93161ceab --- /dev/null +++ b/terraform/transform_flatten.go @@ -0,0 +1,43 @@ +package terraform + +// GraphNodeFlattenable must be implemented by nodes that can be flattened +// into the graph. +type GraphNodeFlattenable interface { + GraphNodeSubgraph + + // Flatten should return true if this should be flattened. + Flatten() bool +} + +// FlattenTransform is a transformer that goes through the graph, finds +// subgraphs that can be flattened, and flattens them into this graph, +// removing the prior subgraph node. +type FlattenTransform struct{} + +func (t *FlattenTransform) Transform(g *Graph) error { + for _, v := range g.Vertices() { + fn, ok := v.(GraphNodeFlattenable) + if !ok { + continue + } + + // If we don't want to be flattened, don't do it + if !fn.Flatten() { + continue + } + + // Get the subgraph and flatten it into this one + subgraph := fn.Subgraph() + for _, sv := range subgraph.Vertices() { + g.Add(sv) + } + for _, se := range subgraph.Edges() { + g.Connect(se) + } + + // Remove the old node + g.Remove(v) + } + + return nil +}