opentofu/internal/terraform/transform_vertex.go
Martin Atkins 36d0a50427 Move terraform/ to internal/terraform/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

45 lines
1.1 KiB
Go

package terraform
import (
"fmt"
"github.com/hashicorp/terraform/internal/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
}