fffca3d205
This utility is intended to support the extension packages that are siblings of this package, along with third-party extensions, by providing a way to transform bodies in arbitrary ways. The "Deep" function then provides a means to apply a particular transform recursively to a nested block tree, allowing a particular extension to be supported at arbitrary nesting levels. This functionality is provided in terms of the standard zcl.Body interface, so that transform results can be used with any code that operates generically on bodies. This includes the zcldec and gozcl packages, so files with extensions can still be decoded in the usual way.
41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package transform
|
|
|
|
import (
|
|
"github.com/zclconf/go-zcl/zcl"
|
|
)
|
|
|
|
// 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 {
|
|
TransformBody(zcl.Body) zcl.Body
|
|
}
|
|
|
|
// TransformerFunc is a function type that implements Transformer.
|
|
type TransformerFunc func(zcl.Body) zcl.Body
|
|
|
|
// TransformBody is an implementation of Transformer.TransformBody.
|
|
func (f TransformerFunc) TransformBody(in zcl.Body) zcl.Body {
|
|
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)
|
|
}
|
|
|
|
func (c chain) TransformBody(body zcl.Body) zcl.Body {
|
|
for _, t := range c {
|
|
body = t.TransformBody(body)
|
|
}
|
|
return body
|
|
}
|