2017-07-27 23:23:20 +00:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2017-07-27 23:23:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Transformer takes a given body, applies some (possibly no-op)
|
|
|
|
// transform to it, and returns the new body.
|
|
|
|
//
|
|
|
|
// It must _not_ mutate the given body in-place.
|
|
|
|
//
|
|
|
|
// The transform call cannot fail, but it _can_ return a body that immediately
|
|
|
|
// returns diagnostics when its methods are called. NewErrorBody is a utility
|
|
|
|
// to help with this.
|
|
|
|
type Transformer interface {
|
2017-09-11 23:40:37 +00:00
|
|
|
TransformBody(hcl.Body) hcl.Body
|
2017-07-27 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TransformerFunc is a function type that implements Transformer.
|
2017-09-11 23:40:37 +00:00
|
|
|
type TransformerFunc func(hcl.Body) hcl.Body
|
2017-07-27 23:23:20 +00:00
|
|
|
|
|
|
|
// TransformBody is an implementation of Transformer.TransformBody.
|
2017-09-11 23:40:37 +00:00
|
|
|
func (f TransformerFunc) TransformBody(in hcl.Body) hcl.Body {
|
2017-07-27 23:23:20 +00:00
|
|
|
return f(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
type chain []Transformer
|
|
|
|
|
|
|
|
// Chain takes a slice of transformers and returns a single new
|
|
|
|
// Transformer that applies each of the given transformers in sequence.
|
|
|
|
func Chain(c []Transformer) Transformer {
|
|
|
|
return chain(c)
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
func (c chain) TransformBody(body hcl.Body) hcl.Body {
|
2017-07-27 23:23:20 +00:00
|
|
|
for _, t := range c {
|
|
|
|
body = t.TransformBody(body)
|
|
|
|
}
|
|
|
|
return body
|
|
|
|
}
|