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

Add HCL function try(), remove nilval handling #702

Open
johakoch opened this issue Feb 2, 2023 · 1 comment
Open

Add HCL function try(), remove nilval handling #702

johakoch opened this issue Feb 2, 2023 · 1 comment

Comments

@johakoch
Copy link
Collaborator

johakoch commented Feb 2, 2023

For Couper 2?

Couper's internal eval error by null value replacement (in eval.value.go) is quite complicated, and still does not work in several use cases. E.g. (in case there is no request.json_body):

to_upper(request.json_body.a)

Invalid function argument; Invalid value for "str" parameter: string required.


request.json_body.a + 2

Invalid operand; Unsuitable value for left operand: number required.


request.json_body.a < 2

Invalid operand; Unsuitable value for left operand: number required.


request.json_body.a ? "ok" : "nok"

Unsupported attribute; This object does not have an attribute named "a".


!request.json_body.a ? "ok" : "nok"

Invalid operand; Unsuitable value for unary operand: bool required.


[ for v in request.json_body.a : v ]

Iteration over null value; A null value cannot be used as the collection in a 'for' expression.


[ for k in ["a", "b"] : request.json_body[k] ]

Invalid index; The given key does not identify an element in this collection value., and 1 other diagnostic(s)


{a = 1, b = 2}[request.json_body.a]

Invalid index; Can't use a null value as an indexing key.


I would rather provide the HCL try() function and expect config writers to use it and set an appropriate default value (may be null in some cases, can't be null in others) themselves. They will know better than the developers of Couper.

See documentation of try() and can() (https://pkg.go.dev/github.com/hashicorp/hcl/v2/ext/tryfunc#section-readme):

Both of these are primarily intended for working with deep data structures which might not have a dependable shape. For example, we can use try to attempt to fetch a value from deep inside a data structure but produce a default value if any step of the traversal fails:

result = try(foo.deep[0].lots.of["traversals"], null)

The final result to try should generally be some sort of constant value that will always evaluate successfully.

@johakoch
Copy link
Collaborator Author

johakoch commented Feb 3, 2023

From the examples above, only one could currently be "healed" with the (to be provided) try function:

[ for k in ["a", "b"] : try(request.json_body[k], null ) ] # returns [null,null]

The other examples would still throw an error, because request.json_body.a is replaced with null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant