package userfunc
import "github.com/docker/buildx/util/userfunc"
Package userfunc implements a HCL extension that allows user-defined functions in HCL configuration.
Using this extension requires some integration effort on the part of the calling application, to pass any declared functions into a HCL evaluation context after processing.
The function declaration syntax looks like this:
function "foo" { params = ["name"] result = "Hello, ${name}!" }
When a user-defined function is called, the expression given for the "result" attribute is evaluated in an isolated evaluation context that defines variables named after the given parameter names.
The block name "function" may be overridden by the calling application, if that default name conflicts with an existing block or attribute name in the application.
Index ¶
- func NewFunction(paramsExpr, varParamExpr, resultExpr hcl.Expression, getBaseCtx func() *hcl.EvalContext) (function.Function, hcl.Diagnostics)
- type ContextFunc
Functions ¶
func NewFunction ¶
func NewFunction(paramsExpr, varParamExpr, resultExpr hcl.Expression, getBaseCtx func() *hcl.EvalContext) (function.Function, hcl.Diagnostics)
NewFunction creates a new function instance from preparsed HCL expressions.
Types ¶
type ContextFunc ¶
type ContextFunc func() *hcl.EvalContext
A ContextFunc is a callback used to produce the base EvalContext for running a particular set of functions.
This is a function rather than an EvalContext directly to allow functions to be decoded before their context is complete. This will be true, for example, for applications that wish to allow functions to refer to themselves.
The simplest use of a ContextFunc is to give user functions access to the same global variables and functions available elsewhere in an application's configuration language, but more complex applications may use different contexts to support lexical scoping depending on where in a configuration structure a function declaration is found, etc.
Source Files ¶
- Version
- v0.23.0 (latest)
- Published
- Apr 15, 2025
- Platform
- js/wasm
- Imports
- 3 packages
- Last checked
- 13 hours ago –
Tools for package owners.