ext/userfunc: use bare identifiers for param names

Now that we have the necessary functions to deal with this in the
low-level HCL API, it's more intuitive to use bare identifiers for these
parameter names. This reinforces the idea that they are symbols being
defined rather than arbitrary string expressions.
This commit is contained in:
Martin Atkins 2018-02-04 11:20:42 -08:00
parent 2ddf8b4b8c
commit 18a92d222b
4 changed files with 48 additions and 17 deletions

View File

@ -364,7 +364,7 @@ block type:
```
function "add_one" {
params = ["n"]
params = [n]
result = n + 1
}
```
@ -379,9 +379,15 @@ input file:
```
function "upper" {
params = ["str"]
params = [str]
result = upper(str)
}
function "min" {
params = []
variadic_param = nums
result = min(nums...)
}
```
Custom functions defined in the spec cannot be called from the spec itself.

View File

@ -7,9 +7,15 @@ Functions are defined via a specific block type, like this:
```hcl
function "add" {
params = ["a", "b"]
params = [a, b]
result = a + b
}
function "list" {
params = []
variadic_param = items
result = items
}
```
The extension is implemented as a pre-processor for `cty.Body` objects. Given

View File

@ -1,7 +1,6 @@
package userfunc
import (
"github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl2/hcl"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
@ -54,6 +53,7 @@ func decodeUserFunctions(body hcl.Body, blockType string, contextFunc ContextFun
}
funcs = make(map[string]function.Function)
Blocks:
for _, block := range content.Blocks {
name := block.Labels[0]
funcContent, funcDiags := block.Body.Content(funcBodySchema)
@ -72,15 +72,34 @@ func decodeUserFunctions(body hcl.Body, blockType string, contextFunc ContextFun
var params []string
var varParam string
paramsDiags := gohcl.DecodeExpression(paramsExpr, nil, &params)
paramExprs, paramsDiags := hcl.ExprList(paramsExpr)
diags = append(diags, paramsDiags...)
if paramsDiags.HasErrors() {
continue
}
for _, paramExpr := range paramExprs {
param := hcl.ExprAsKeyword(paramExpr)
if param == "" {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid param element",
Detail: "Each parameter name must be an identifier.",
Subject: paramExpr.Range().Ptr(),
})
continue Blocks
}
params = append(params, param)
}
if varParamExpr != nil {
paramsDiags := gohcl.DecodeExpression(varParamExpr, nil, &varParam)
diags = append(diags, paramsDiags...)
if paramsDiags.HasErrors() {
varParam = hcl.ExprAsKeyword(varParamExpr)
if varParam == "" {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid variadic_param",
Detail: "The variadic parameter name must be an identifier.",
Subject: varParamExpr.Range().Ptr(),
})
continue
}
}

View File

@ -4,8 +4,8 @@ import (
"fmt"
"testing"
"github.com/hashicorp/hcl2/hcl/hclsyntax"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcl/hclsyntax"
"github.com/zclconf/go-cty/cty"
)
@ -20,7 +20,7 @@ func TestDecodeUserFunctions(t *testing.T) {
{
`
function "greet" {
params = ["name"]
params = [name]
result = "Hello, ${name}."
}
`,
@ -32,7 +32,7 @@ function "greet" {
{
`
function "greet" {
params = ["name"]
params = [name]
result = "Hello, ${name}."
}
`,
@ -44,7 +44,7 @@ function "greet" {
{
`
function "greet" {
params = ["name"]
params = [name]
result = "Hello, ${name}."
}
`,
@ -56,7 +56,7 @@ function "greet" {
{
`
function "add" {
params = ["a", "b"]
params = [a, b]
result = a + b
}
`,
@ -69,7 +69,7 @@ function "add" {
`
function "argstuple" {
params = []
variadic_param = "args"
variadic_param = args
result = args
}
`,
@ -109,11 +109,11 @@ function "closure" {
{
`
function "neg" {
params = ["val"]
params = [val]
result = -val
}
function "add" {
params = ["a", "b"]
params = [a, b]
result = a + b
}
`,
@ -125,7 +125,7 @@ function "add" {
{
`
function "neg" {
parrams = ["val"]
parrams = [val]
result = -val
}
`,