2018-01-27 10:35:12 -08:00
|
|
|
# HCL User Functions Extension
|
2017-07-25 18:34:56 -07:00
|
|
|
|
2018-01-27 10:35:12 -08:00
|
|
|
This HCL extension allows a calling application to support user-defined
|
2017-07-25 18:34:56 -07:00
|
|
|
functions.
|
|
|
|
|
|
|
|
Functions are defined via a specific block type, like this:
|
|
|
|
|
2018-01-27 10:35:12 -08:00
|
|
|
```hcl
|
2017-07-25 18:34:56 -07:00
|
|
|
function "add" {
|
2018-02-04 11:20:42 -08:00
|
|
|
params = [a, b]
|
2017-07-25 18:34:56 -07:00
|
|
|
result = a + b
|
|
|
|
}
|
2018-02-04 11:20:42 -08:00
|
|
|
|
|
|
|
function "list" {
|
|
|
|
params = []
|
|
|
|
variadic_param = items
|
|
|
|
result = items
|
|
|
|
}
|
2017-07-25 18:34:56 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
The extension is implemented as a pre-processor for `cty.Body` objects. Given
|
|
|
|
a body that may contain functions, the `DecodeUserFunctions` function searches
|
|
|
|
for blocks that define functions and returns a functions map suitable for
|
2017-09-11 16:40:37 -07:00
|
|
|
inclusion in a `hcl.EvalContext`. It also returns a new `cty.Body` that
|
2017-07-25 18:34:56 -07:00
|
|
|
contains the remainder of the content from the given body, allowing for
|
|
|
|
further processing of remaining content.
|
|
|
|
|
2020-08-21 15:03:22 -07:00
|
|
|
For more information, see [the godoc reference](https://pkg.go.dev/github.com/hashicorp/hcl/v2/ext/userfunc?tab=doc).
|