-
Notifications
You must be signed in to change notification settings - Fork 117
feat: Enable cel: template expressions #2363
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package cel | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
|
|
@@ -38,14 +39,29 @@ func evaluate(expr string, env *cel.Env, data map[string]any) (ref.Val, error) { | |
| // / pacParams, it will output a Cel value or an error if selectedjm. | ||
chmouel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| func Value(query string, body any, headers, pacParams map[string]string, changedFiles map[string]any) (ref.Val, error) { | ||
| // Marshal/Unmarshal the body to a map[string]any so we can access it from the CEL | ||
| nbody, err := json.Marshal(body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var jsonMap map[string]any | ||
| err = json.Unmarshal(nbody, &jsonMap) | ||
| if err != nil { | ||
| return nil, err | ||
| switch b := body.(type) { | ||
| case nil: | ||
| jsonMap = map[string]any{} | ||
| case map[string]any: | ||
| jsonMap = b | ||
| default: | ||
| nbody, err := json.Marshal(body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| trimmed := bytes.TrimSpace(nbody) | ||
| if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("null")) { | ||
| jsonMap = map[string]any{} | ||
| } else { | ||
| err = json.Unmarshal(nbody, &jsonMap) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if jsonMap == nil { | ||
| jsonMap = map[string]any{} | ||
| } | ||
|
Comment on lines
+61
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd rather be explicit than caught by surprise |
||
| } | ||
| } | ||
|
|
||
| mapStrDyn := types.NewMapType(types.StringType, types.DynType) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ternary operator
? \"true\" : \"false\"is redundant in this CEL expression. Boolean results from CEL expressions are automatically converted to the strings "true" or "false" during template rendering. Simplifying the expression makes the example cleaner and demonstrates a more idiomatic usage of the new feature.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am not so sure about that, i let this be explicit