Add two new DSL helper functions

hmac_sha1 and concat_ws (with seperator) this are helpful in
signing API requests.
This commit is contained in:
James Turner 2022-06-06 15:49:03 -04:00 committed by forgedhallpass
parent f3514e9b92
commit 9d37bd6c0c
2 changed files with 26 additions and 0 deletions

View File

@ -241,6 +241,11 @@ func init() {
h.Write([]byte(args[0].(string)))
return hex.EncodeToString(h.Sum(nil)), nil
}),
"hmac_sha1": makeDslFunction(2, func(args ...interface{}) (interface{}, error) {
h := hmac.New(sha1.New, []byte(args[1].(string)))
h.Write([]byte(args[0].(string)))
return hex.EncodeToString(h.Sum(nil)), nil
}),
"html_escape": makeDslFunction(1, func(args ...interface{}) (interface{}, error) {
return html.EscapeString(types.ToString(args[0])), nil
}),
@ -283,6 +288,23 @@ func init() {
return builder.String(), nil
},
),
"concat_ws": makeDslWithOptionalArgsFunction(
"(args ...interface{}) string",
func(arguments ...interface{}) (interface{}, error) {
builder := &strings.Builder{}
separator := arguments[len(arguments)-1]
arguments = arguments[:len(arguments)-1]
for i, argument := range arguments {
builder.WriteString(types.ToString(argument))
if i != len(arguments)-1 {
builder.WriteString(types.ToString(separator))
}
}
return builder.String(), nil
},
),
"regex": makeDslFunction(2, func(args ...interface{}) (interface{}, error) {
compiled, err := regexp.Compile(types.ToString(args[0]))
if err != nil {

View File

@ -110,6 +110,7 @@ func TestGetPrintableDslFunctionSignatures(t *testing.T) {
base64_py(arg1 interface{}) interface{}
compare_versions(firstVersion, constraints ...string) bool
concat(args ...interface{}) string
concat_ws(args ...interface{}) string
contains(arg1, arg2 interface{}) interface{}
date(arg1 interface{}) interface{}
dec_to_hex(arg1 interface{}) interface{}
@ -118,6 +119,7 @@ func TestGetPrintableDslFunctionSignatures(t *testing.T) {
gzip_decode(arg1 interface{}) interface{}
hex_decode(arg1 interface{}) interface{}
hex_encode(arg1 interface{}) interface{}
hmac_sha1(arg1, arg2 interface{}) interface{}
hmac_sha256(arg1, arg2 interface{}) interface{}
html_escape(arg1 interface{}) interface{}
html_unescape(arg1 interface{}) interface{}
@ -190,6 +192,7 @@ func TestDslExpressions(t *testing.T) {
`remove_bad_chars("abcd", "bc")`: "ad",
`replace("Hello", "He", "Ha")`: "Hallo",
`concat("Hello", 123, "world")`: "Hello123world",
`concat_ws("Hello", 123, "world", "_")`: "Hello_123_world",
`repeat("a", 5)`: "aaaaa",
`repeat("a", "5")`: "aaaaa",
`repeat("../", "5")`: "../../../../../",
@ -228,6 +231,7 @@ func TestDslExpressions(t *testing.T) {
`compare_versions('v1.1.1', '>v1.1.0')`: true,
`compare_versions('v1.0.0', '>v0.0.1,<v1.0.1')`: true,
`compare_versions('v1.0.0', '>v0.0.1', '<v1.0.1')`: true,
`hmac_sha1('test','scrt')`: "8856b111056d946d5c6c92a21b43c233596623c6",
`hmac_sha256('test','scrt')`: "1f1bff5574f18426eb376d6dd5368a754e67a798aa2074644d5e3fd4c90c7a92",
`time_format("02-01-2006 15:04")`: now.Format("02-01-2006 15:04"),
}