-
Notifications
You must be signed in to change notification settings - Fork 43
Description
Hey,
Is there a reason why GVal defaults to returning nil if the expression references an undefined data key, but JsonPath errors out?
https://github.com/PaesslerAG/gval/blob/cd026a3dee26df39acddc2292229512c4fa68756/evaluable.go#L133
vs
Line 92 in 3484786
if r, ok := o[k]; ok { |
As an example,
g := gval.NewLanguage(
gval.Full(),
jsonpath.Language())
value, err := g.Evaluate(`$.data`, map[string]any{"data": "test"})
if err != nil {
fmt.Println(err)
}
fmt.Println(value)
Results in test
; however, if the expression is $.invalidData
then we error out:
can not evaluate $.invalidData: unknown key invalidData
.
BUT if we just use standard Gval expressions, i.e:
value, err := g.Evaluate(`invalidData`, map[string]any{"data": "test"})
we get nil as a return value.
The only reason I can think of is to have a distinction between null values and undefined values in JSON (null -> nil, undefined -> error), but we don't always want to error out if the key is undefined, it'd be nice if we could configure this default behavior. I might be missing something here so please let me know if this is the case!