Created
October 21, 2019 15:38
-
-
Save afrittoli/d0b01647492389c5627252fe7ded4f0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 0386243440fdbf81c5a6c8dcdf0e281237ef5415 | |
Author: Andrea Frittoli <[email protected]> | |
Date: Mon Oct 21 14:10:11 2019 +0100 | |
Support more GJSON syntax for the body | |
Extend the supported GJSON syntax to include queries on arrays, | |
which can be useful when dealing with JSON that describes | |
k8s resources. | |
diff --git a/docs/triggerbindings.md b/docs/triggerbindings.md | |
index 83d962f..fa6a738 100644 | |
--- a/docs/triggerbindings.md | |
+++ b/docs/triggerbindings.md | |
@@ -29,12 +29,12 @@ spec: | |
## Event Variable Interpolation | |
-In order to parse generic events as efficiently as possible, [GJSON](https://github.com/tidwall/gjson) | |
+In order to parse generic events as efficiently as possible, [GJSON](https://github.com/tidwall/gjson) | |
is used internally. As a result, the binding [path syntax](https://github.com/tidwall/gjson#path-syntax) | |
differs slightly from standard JSON. As of now, the following patterns are | |
supported within `TriggerBinding` parameter value interpolation: | |
-- `$(body(.[0-9A-Za-z_-]+)*)` | |
-- `$(header(.[0-9A-Za-z_-]+)?)` | |
+- `$(body([.|][[:alnum:]*?_-]+|[.|]#|[.|]#\([[:alnum:]=<>%!*?"_-]+\)#??)*)` | |
+- `$(header([.|][:alnum:]+)?)` | |
### Body | |
HTTP Post request body data can be referenced using variable interpolation. | |
diff --git a/pkg/template/event.go b/pkg/template/event.go | |
index 8a23af4..e0a0133 100644 | |
--- a/pkg/template/event.go | |
+++ b/pkg/template/event.go | |
@@ -27,8 +27,12 @@ import ( | |
) | |
// bodyPathVarRegex determines valid body path variables | |
-var bodyPathVarRegex = regexp.MustCompile(`\$\(body(.[0-9A-Za-z_-]+)*\)`) | |
-var headerVarRegex = regexp.MustCompile(`\$\(header(.[0-9A-Za-z_-]+)?\)`) | |
+// The body regular expression allows for most of GJSON syntax, except for nested | |
+// queries, modifiers and multipath. It does not guarantee a valid GJSON syntax. | |
+var bodyPathVarRegex = regexp.MustCompile(`\$\(body([.|][[:alnum:]*?_-]+|[.|]#|[.|]#\([[:alnum:]=<>%!*?"_-]+\)#??)*\)`) | |
+ | |
+// The headers regular expression allows for simple | |
+var headerVarRegex = regexp.MustCompile(`\$\(header([.|][[:alnum:]_-]+)?\)`) | |
// getBodyPathFromVar returns the body path given an body path variable | |
// $(body.my.path) -> my.path | |
diff --git a/pkg/template/event_test.go b/pkg/template/event_test.go | |
index 358bac8..981427c 100644 | |
--- a/pkg/template/event_test.go | |
+++ b/pkg/template/event_test.go | |
@@ -40,7 +40,13 @@ func Test_BodyPathVarRegex(t *testing.T) { | |
"$(body.a-b)", | |
"$(body.a1)", | |
"$(body.a.b)", | |
- "$(body.a.b.c)", | |
+ "$(body.a.b?.c)", | |
+ "$(body.1.b.c*)", | |
+ "$(body.#)", | |
+ "$(body.#(a==b))", | |
+ "$(body.#(a>1)#)", | |
+ "$(body|#(a%\"D*\")#.c)", | |
+ "$(body.#(a!%\"D*\")|c)", | |
} | |
for _, bodyPathVar := range tests { | |
t.Run(bodyPathVar, func(t *testing.T) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment