google / cel-go

Fast, portable, non-Turing complete expression evaluation with gradual typing (Go)
https://cel.dev
Apache License 2.0
2.27k stars 220 forks source link

Has macro returns no such key error #912

Closed parsnips closed 7 months ago

parsnips commented 7 months ago

Describe the bug When executing the has macro on nested paths, will throw a no such key error instead of false.

Consider the following json in context as doc

{
  "fizz": {
     "buzz": {
         "baz": "biz"
      }
   }
 }

if you execute the following has macros:

macro result
has(doc.fizz.buzz.baz) true
has(doc.fizz.baz) false
has(doc.fizz.baz.buzz) input: evaluate [...]: has(document.fizz.baz.buzz), no such key: baz

Expected behavior

Expect has macro to return false instead of an error.

TristonianJones commented 7 months ago

The has macro only operates on the last field in the field selection. If you're looking for a more robust check, consider using optional types: doc.?fizz.baz.buzz.hasValue()