Skip to content

Commit

Permalink
ext/userfunc: use bare identifiers for param names
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apparentlymart committed Feb 4, 2018
1 parent 2ddf8b4 commit 18a92d2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
10 changes: 8 additions & 2 deletions cmd/hcldec/spec-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ block type:

```
function "add_one" {
params = ["n"]
params = [n]
result = n + 1
}
```
Expand All @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion ext/userfunc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 24 additions & 5 deletions ext/userfunc/decode.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
}
Expand Down
18 changes: 9 additions & 9 deletions ext/userfunc/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -20,7 +20,7 @@ func TestDecodeUserFunctions(t *testing.T) {
{
`
function "greet" {
params = ["name"]
params = [name]
result = "Hello, ${name}."
}
`,
Expand All @@ -32,7 +32,7 @@ function "greet" {
{
`
function "greet" {
params = ["name"]
params = [name]
result = "Hello, ${name}."
}
`,
Expand All @@ -44,7 +44,7 @@ function "greet" {
{
`
function "greet" {
params = ["name"]
params = [name]
result = "Hello, ${name}."
}
`,
Expand All @@ -56,7 +56,7 @@ function "greet" {
{
`
function "add" {
params = ["a", "b"]
params = [a, b]
result = a + b
}
`,
Expand All @@ -69,7 +69,7 @@ function "add" {
`
function "argstuple" {
params = []
variadic_param = "args"
variadic_param = args
result = args
}
`,
Expand Down Expand Up @@ -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
}
`,
Expand All @@ -125,7 +125,7 @@ function "add" {
{
`
function "neg" {
parrams = ["val"]
parrams = [val]
result = -val
}
`,
Expand Down

0 comments on commit 18a92d2

Please sign in to comment.