Open johakoch opened 1 year ago
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
.
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 norequest.json_body
):Invalid function argument; Invalid value for "str" parameter: string required.
Invalid operand; Unsuitable value for left operand: number required.
Invalid operand; Unsuitable value for left operand: number required.
Unsupported attribute; This object does not have an attribute named "a".
Invalid operand; Unsuitable value for unary operand: bool required.
Iteration over null value; A null value cannot be used as the collection in a 'for' expression.
Invalid index; The given key does not identify an element in this collection value., and 1 other diagnostic(s)
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 benull
in some cases, can't benull
in others) themselves. They will know better than the developers of Couper.See documentation of
try()
andcan()
(https://pkg.go.dev/github.com/hashicorp/hcl/v2/ext/tryfunc#section-readme):