Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement std.match as standard function #715

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"io"
"math"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -259,6 +260,29 @@ func builtinTrace(i *interpreter, x value, y value) (value, error) {
return y, nil
}

func builtinMatch(i *interpreter, strv value, patv value) (value, error) {
str, err := i.getString(strv)
if err != nil {
return nil, err
}
pat, err := i.getString(patv)
if err != nil {
return nil, err
}

r, err := regexp.Compile(pat.getGoString())
if err != nil {
return nil, i.Error(fmt.Sprintf("Pattern %s is not valid", pat.getGoString()))
}

matches := []*cachedThunk{} // to return empty array
for _, a := range r.FindAllString(str.getGoString(), -1) {
matches = append(matches, readyThunk(makeValueString(a)))
}

return makeValueArray(matches), nil
}

// astMakeArrayElement wraps the function argument of std.makeArray so that
// it can be embedded in cachedThunk without needing to execute it ahead of
// time. It is equivalent to `local i = 42; func(i)`. It therefore has no
Expand Down Expand Up @@ -2097,12 +2121,12 @@ func builtinAvg(i *interpreter, arrv value) (value, error) {
if err != nil {
return nil, err
}

len := float64(arr.length())
if len == 0 {
return nil, i.Error("Cannot calculate average of an empty array.")
}

sumValue, err := builtinSum(i, arrv)
if err != nil {
return nil, err
Expand All @@ -2112,7 +2136,7 @@ func builtinAvg(i *interpreter, arrv value) (value, error) {
return nil, err
}

avg := sum.value/len
avg := sum.value / len
return makeValueNumber(avg), nil
}

Expand Down Expand Up @@ -2515,6 +2539,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&unaryBuiltin{name: "isEmpty", function: builtinIsEmpty, params: ast.Identifiers{"str"}},
&binaryBuiltin{name: "equalsIgnoreCase", function: builtinEqualsIgnoreCase, params: ast.Identifiers{"str1", "str2"}},
&unaryBuiltin{name: "trim", function: builtinTrim, params: ast.Identifiers{"str"}},
&binaryBuiltin{name: "match", function: builtinMatch, params: ast.Identifiers{"str", "pat"}},
&unaryBuiltin{name: "base64Decode", function: builtinBase64Decode, params: ast.Identifiers{"str"}},
&unaryBuiltin{name: "base64DecodeBytes", function: builtinBase64DecodeBytes, params: ast.Identifiers{"str"}},
&unaryBuiltin{name: "parseInt", function: builtinParseInt, params: ast.Identifiers{"str"}},
Expand Down
3 changes: 2 additions & 1 deletion linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func prepareStdlib(g *typeGraph) {
"isEmpty": g.newSimpleFuncType(boolType, "str"),
"equalsIgnoreCase": g.newSimpleFuncType(boolType, "str1", "str2"),
"trim": g.newSimpleFuncType(stringType, "str"),
"match": g.newSimpleFuncType(stringType, "str", "pat"),
// TODO(sbarzowski) Fix when they match the documentation
"escapeStringBash": g.newSimpleFuncType(stringType, "str_"),
"escapeStringDollars": g.newSimpleFuncType(stringType, "str_"),
Expand Down Expand Up @@ -152,7 +153,7 @@ func prepareStdlib(g *typeGraph) {
"minArray": g.newFuncType(anyArrayType, []ast.Parameter{required("arr"), optional("keyF")}),
"maxArray": g.newFuncType(anyArrayType, []ast.Parameter{required("arr"), optional("keyF")}),
"contains": g.newSimpleFuncType(boolType, "arr", "elem"),
"avg": g.newSimpleFuncType(numberType, "arr"),
"avg": g.newSimpleFuncType(numberType, "arr"),
"all": g.newSimpleFuncType(boolArrayType, "arr"),
"any": g.newSimpleFuncType(boolArrayType, "arr"),
"remove": g.newSimpleFuncType(anyArrayType, "arr", "elem"),
Expand Down
5 changes: 5 additions & 0 deletions testdata/builtinMatch.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"peach",
"punch",
"pinch"
]
1 change: 1 addition & 0 deletions testdata/builtinMatch.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.match("peach punch pinch", "p([a-z]+)ch")
Empty file.
10 changes: 10 additions & 0 deletions testdata/builtinMatch2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RUNTIME ERROR: Pattern (a] is not valid
-------------------------------------------------
testdata/builtinMatch2:1:1-24 $

std.match("foo", "(a]")

-------------------------------------------------
During evaluation


1 change: 1 addition & 0 deletions testdata/builtinMatch2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.match("foo", "(a]")
Empty file.