github.com/hashicorp/hcl/v2@v2.20.0/ext/transform/transformer.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package transform 5 6 import ( 7 "github.com/hashicorp/hcl/v2" 8 ) 9 10 // A Transformer takes a given body, applies some (possibly no-op) 11 // transform to it, and returns the new body. 12 // 13 // It must _not_ mutate the given body in-place. 14 // 15 // The transform call cannot fail, but it _can_ return a body that immediately 16 // returns diagnostics when its methods are called. NewErrorBody is a utility 17 // to help with this. 18 type Transformer interface { 19 TransformBody(hcl.Body) hcl.Body 20 } 21 22 // TransformerFunc is a function type that implements Transformer. 23 type TransformerFunc func(hcl.Body) hcl.Body 24 25 // TransformBody is an implementation of Transformer.TransformBody. 26 func (f TransformerFunc) TransformBody(in hcl.Body) hcl.Body { 27 return f(in) 28 } 29 30 type chain []Transformer 31 32 // Chain takes a slice of transformers and returns a single new 33 // Transformer that applies each of the given transformers in sequence. 34 func Chain(c []Transformer) Transformer { 35 return chain(c) 36 } 37 38 func (c chain) TransformBody(body hcl.Body) hcl.Body { 39 for _, t := range c { 40 body = t.TransformBody(body) 41 } 42 return body 43 }