-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathexample_func_test.go
57 lines (48 loc) · 1.05 KB
/
example_func_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package hil_test
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/hil"
"github.com/hashicorp/hil/ast"
)
func Example_functions() {
input := "${lower(var.test)} - ${6 + 2}"
tree, err := hil.Parse(input)
if err != nil {
log.Fatal(err)
}
lowerCase := ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Variadic: false,
Callback: func(inputs []interface{}) (interface{}, error) {
input := inputs[0].(string)
return strings.ToLower(input), nil
},
}
config := &hil.EvalConfig{
GlobalScope: &ast.BasicScope{
VarMap: map[string]ast.Variable{
"var.test": ast.Variable{
Type: ast.TypeString,
Value: "TEST STRING",
},
},
FuncMap: map[string]ast.Function{
"lower": lowerCase,
},
},
}
result, err := hil.Eval(tree, config)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Type: %s\n", result.Type)
fmt.Printf("Value: %s\n", result.Value)
// Output:
// Type: TypeString
// Value: test string - 8
}