Skip to content

Commit

Permalink
schemahcl: add support for regexpescape hcl function
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Jun 11, 2024
1 parent 0a93842 commit e7af6a4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions schemahcl/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -141,6 +142,7 @@ func stdFuncs() map[string]function.Function {
"range": stdlib.RangeFunc,
"regex": stdlib.RegexFunc,
"regexall": stdlib.RegexAllFunc,
"regexpescape": regexpEscape,
"regexreplace": stdlib.RegexReplaceFunc,
"reverse": stdlib.ReverseListFunc,
"setintersection": stdlib.SetIntersectionFunc,
Expand Down Expand Up @@ -442,6 +444,20 @@ var (
return cty.False, nil
},
})

regexpEscape = function.New(&function.Spec{
Description: `Return a string that escapes all regular expression metacharacters in the provided text.`,
Params: []function.Parameter{
{
Name: "str",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {

Check warning on line 457 in schemahcl/stdlib.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'retType' seems to be unused, consider removing or renaming it as _ (revive)
return cty.StringVal(regexp.QuoteMeta(args[0].AsString())), nil
},
})
)

// MakeFileFunc returns a function that reads a file
Expand Down
39 changes: 39 additions & 0 deletions schemahcl/stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ func TestEndsWithFunc(t *testing.T) {
require.Equal(t, cty.True, got)
}

func TestRegexpEscapeFunc(t *testing.T) {
got, err := regexpEscape.Call([]cty.Value{cty.StringVal("a|b|c")})
require.NoError(t, err)
require.Equal(t, "a\\|b\\|c", got.AsString())
got, err = regexpEscape.Call([]cty.Value{cty.StringVal("abc")})
require.NoError(t, err)
require.Equal(t, "abc", got.AsString())
}

func TestMakeFileFunc(t *testing.T) {
fn := MakeFileFunc("testdata")
_, err := fn.Call([]cty.Value{cty.StringVal("foo")})
Expand All @@ -168,6 +177,36 @@ func TestMakeFileFunc(t *testing.T) {
require.Equal(t, "person \"rotemtam\" {\n hobby = var.hobby\n}", v.AsString())
}

func Example_RegexpEscapeFunc() {
for _, f := range []string{
`v = regexpescape("a|b|c")`,
`v = regexpescape("abc")`,
`v = regexpescape(<<TAB
id | name
---+-----
1 | foo
TAB
)`,
} {
var d struct {
V string `spec:"v"`
}
if err := New().EvalBytes([]byte(f), &d, nil); err != nil {
fmt.Println("failed to evaluate:", err)
return
}
fmt.Printf("%s\n\n", d.V)
}
// Output:
// a\|b\|c
//
// abc
//
// id \| name
// ---\+-----
// 1 \| foo
}

func Example_PrintFunc() {
for _, f := range []string{
`v = print("a")`,
Expand Down

0 comments on commit e7af6a4

Please sign in to comment.