Skip to content

Commit 71da38c

Browse files
authored
Merge pull request #50 from Esonhugh/main
helperfunctions to common Slice operations
2 parents 6f7f739 + c8ee1b1 commit 71da38c

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func main() {
8383
| hmac(algorithm, data, secret) string | hmac function that accepts a hashing function type with data and secret | `hmac("sha1", "test", "scrt")` | `8856b111056d946d5c6c92a21b43c233596623c6` |
8484
| html_escape(input interface{}) string | HTML escapes the given input | `html_escape("<body>test</body>")` | `&lt;body&gt;test&lt;/body&gt;` |
8585
| html_unescape(input interface{}) string | HTML un-escapes the given input | `html_unescape("&lt;body&gt;test&lt;/body&gt;")` | `<body>test</body>` |
86+
| index(slice, index) interface{} | Select item at index from slice or string (zero based) | `index("test",0)` | `t` |
8687
| join(separator string, elements ...interface{}) string | Joins the given elements using the specified separator | `join("_", 123, "hello", "world")` | `123_hello_world` |
8788
| json_minify(json) string | Minifies a JSON string by removing unnecessary whitespace | `json_minify("{ \"name\": \"John Doe\", \"foo\": \"bar\" }")` | `{"foo":"bar","name":"John Doe"}` |
8889
| json_prettify(json) string | Prettifies a JSON string by adding indentation | `json_prettify("{\"foo\":\"bar\",\"name\":\"John Doe\"}")` | `{\n \"foo\": \"bar\",\n \"name\": \"John Doe\"\n}` |

dsl.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io"
2222
"math"
2323
"net/url"
24+
"reflect"
2425
"regexp"
2526
"sort"
2627
"strconv"
@@ -76,8 +77,42 @@ func MustAddFunction(function dslFunction) {
7677
}
7778

7879
func init() {
80+
// note: index helper is zero based
81+
MustAddFunction(NewWithPositionalArgs("index", 2, func(args ...interface{}) (interface{}, error) {
82+
index, err := strconv.ParseInt(toString(args[1]), 10, 64)
83+
if err != nil {
84+
return nil, err
85+
}
86+
// If the first argument is a slice, we index into it
87+
switch v := args[0].(type) {
88+
case []string:
89+
l := int64(len(v))
90+
if index < 0 || index >= l {
91+
return nil, fmt.Errorf("index out of range for %v: %d", v, index)
92+
}
93+
return v[index], nil
94+
default:
95+
// Otherwise, we index into the string
96+
str := toString(v)
97+
l := int64(len(str))
98+
if index < 0 || index >= l {
99+
return nil, fmt.Errorf("index out of range for %v: %d", v, index)
100+
}
101+
return string(str[index]), nil
102+
}
103+
}))
104+
79105
MustAddFunction(NewWithPositionalArgs("len", 1, func(args ...interface{}) (interface{}, error) {
80-
length := len(toString(args[0]))
106+
var length int
107+
value := reflect.ValueOf(args[0])
108+
switch value.Kind() {
109+
case reflect.Slice:
110+
length = value.Len()
111+
case reflect.Map:
112+
length = value.Len()
113+
default:
114+
length = len(toString(args[0]))
115+
}
81116
return float64(length), nil
82117
}))
83118

dsl_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ import (
1111
"github.com/stretchr/testify/require"
1212
)
1313

14+
func TestIndex(t *testing.T) {
15+
index, err := govaluate.NewEvaluableExpressionWithFunctions("index(split(url, '.', -1), 1) == 'example'", DefaultHelperFunctions)
16+
require.Nil(t, err, "could not compile index")
17+
18+
result, err := index.Evaluate(map[string]interface{}{"url": "https://www.example.com"})
19+
require.Nil(t, err, "could not evaluate index")
20+
require.Equal(t, true, result, "could not get index data")
21+
}
22+
1423
func TestDSLURLEncodeDecode(t *testing.T) {
1524
encoded, err := DefaultHelperFunctions["url_encode"]("&test\"")
1625
require.Nil(t, err, "could not url encode")
@@ -113,6 +122,7 @@ func TestGetPrintableDslFunctionSignatures(t *testing.T) {
113122
hmac(arg1, arg2, arg3 interface{}) interface{}
114123
html_escape(arg1 interface{}) interface{}
115124
html_unescape(arg1 interface{}) interface{}
125+
index(arg1, arg2 interface{}) interface{}
116126
ip_format(arg1, arg2 interface{}) interface{}
117127
join(separator string, elements ...interface{}) string
118128
join(separator string, elements []interface{}) string
@@ -227,6 +237,7 @@ func TestDslExpressions(t *testing.T) {
227237
`hex_decode("6161")`: "aa",
228238
`len("Hello")`: float64(5),
229239
`len(1234)`: float64(4),
240+
`len(split("1.2.3.4",'.',-1))`: float64(4),
230241
`contains("Hello", "lo")`: true,
231242
`starts_with("Hello", "He")`: true,
232243
`ends_with("Hello", "lo")`: true,

0 commit comments

Comments
 (0)