github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/terraform/transform.go (about)

     1  package terraform
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/dag"
     5  )
     6  
     7  // GraphTransformer is the interface that transformers implement. This
     8  // interface is only for transforms that need entire graph visibility.
     9  type GraphTransformer interface {
    10  	Transform(*Graph) error
    11  }
    12  
    13  // GraphVertexTransformer is an interface that transforms a single
    14  // Vertex within with graph. This is a specialization of GraphTransformer
    15  // that makes it easy to do vertex replacement.
    16  //
    17  // The GraphTransformer that runs through the GraphVertexTransformers is
    18  // VertexTransformer.
    19  type GraphVertexTransformer interface {
    20  	Transform(dag.Vertex) (dag.Vertex, error)
    21  }
    22  
    23  // GraphTransformIf is a helper function that conditionally returns a
    24  // GraphTransformer given. This is useful for calling inline a sequence
    25  // of transforms without having to split it up into multiple append() calls.
    26  func GraphTransformIf(f func() bool, then GraphTransformer) GraphTransformer {
    27  	if f() {
    28  		return then
    29  	}
    30  
    31  	return nil
    32  }
    33  
    34  type graphTransformerMulti struct {
    35  	Transforms []GraphTransformer
    36  }
    37  
    38  func (t *graphTransformerMulti) Transform(g *Graph) error {
    39  	for _, t := range t.Transforms {
    40  		if err := t.Transform(g); err != nil {
    41  			return err
    42  		}
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  // GraphTransformMulti combines multiple graph transformers into a single
    49  // GraphTransformer that runs all the individual graph transformers.
    50  func GraphTransformMulti(ts ...GraphTransformer) GraphTransformer {
    51  	return &graphTransformerMulti{Transforms: ts}
    52  }