forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform_vertex.go
44 lines (36 loc) · 1.04 KB
/
transform_vertex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package terraform
import (
"fmt"
"github.com/hashicorp/terraform/dag"
)
// VertexTransformer is a GraphTransformer that transforms vertices
// using the GraphVertexTransformers. The Transforms are run in sequential
// order. If a transform replaces a vertex then the next transform will see
// the new vertex.
type VertexTransformer struct {
Transforms []GraphVertexTransformer
}
func (t *VertexTransformer) Transform(g *Graph) error {
for _, v := range g.Vertices() {
for _, vt := range t.Transforms {
newV, err := vt.Transform(v)
if err != nil {
return err
}
// If the vertex didn't change, then don't do anything more
if newV == v {
continue
}
// Vertex changed, replace it within the graph
if ok := g.Replace(v, newV); !ok {
// This should never happen, big problem
return fmt.Errorf(
"Failed to replace %s with %s!\n\nSource: %#v\n\nTarget: %#v",
dag.VertexName(v), dag.VertexName(newV), v, newV)
}
// Replace v so that future transforms use the proper vertex
v = newV
}
}
return nil
}