testing map key existence

33 views
Skip to first unread message

Dan Kortschak

unread,
Jul 12, 2023, 8:46:24 PM7/12/23
to CEL Go Discussion Forum
There is has(e.f) when statically testing object e for existence of the
field f, but I cannot see an obvious way to do the equivalent when we
have a map, m and a key, k. How can I express what would be has(m[k]),
but isn't? At the moment I'm using our is_error (consumes a ref.Val
which may be an error and returns a boolean if it is) extension as
!is_error(m[k]).

Dan

Tristan Swadell

unread,
Jul 13, 2023, 3:34:21 PM7/13/23
to Dan Kortschak, CEL Go Discussion Forum
Hi Dan,

The best way to test for map key presence would be to use `in`: e.g. `k in m`. Also consider using OptionalTypes to improve ease of use.

Hope that helps,

-Tristan

--
You received this message because you are subscribed to the Google Groups "CEL Go Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/dc0ed13b50cf66bf51d0695cfced840da6235780.camel%40kortschak.io.

Dan Kortschak

unread,
Jul 13, 2023, 7:34:03 PM7/13/23
to cel-go-...@googlegroups.com
On Thu, 2023-07-13 at 12:34 -0700, 'Tristan Swadell' via CEL Go
Discussion Forum wrote:
> The best way to test for map key presence would be to use `in`: e.g.
> `k in m`. Also consider using OptionalTypes to improve ease of use.
>
> Hope that helps,

Thanks, Tristan. This is perfect.

Dan

Dan Kortschak

unread,
Nov 1, 2023, 8:32:34 PM11/1/23
to cel-go-...@googlegroups.com
On Thu, 2023-07-13 at 12:34 -0700, 'Tristan Swadell' via CEL Go
Discussion Forum wrote:
> Also consider using OptionalTypes to improve ease of use.

Just following this up. From the docs, adding OptionalTypes into an env
looks like it is backwards compatible, but I'd just like to check that
that is the case.

thanks
Dan

Tristan Swadell

unread,
Nov 2, 2023, 3:48:08 PM11/2/23
to Dan Kortschak, cel-go-...@googlegroups.com
Hi Dan,

Optional types are backwards compatible.

Cheers,

-Tristan

--
You received this message because you are subscribed to the Google Groups "CEL Go Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages