jsonnet icon indicating copy to clipboard operation
jsonnet copied to clipboard

Safe select operator

Open jam01 opened this issue 2 years ago • 4 comments

It would be useful to have a selector notation that was null safe. Perhaps ?. as Kotlin has https://kotlinlang.org/docs/null-safety.html#safe-calls

{x: 1}?.y

would return null instead of throwing an error

jam01 avatar Jul 08 '21 21:07 jam01

There are two jsonnet implementations which implement such feature:

Xtrasonnet - fork of sjsonnet, primarily focused to be embedded in Java apps: https://github.com/jam01/xtrasonnet/tree/0.5.4#what-kind-of-additions-to-the-jsonnet-language

Jrsonnet - general-purpose jsonnet implementation, has this feature as optional under the feature flag: https://github.com/CertainLach/jrsonnet/blob/master/docs/features.md#exp-null-coalescing

CertainLach avatar Jul 26 '23 21:07 CertainLach

Hey @CertainLach yeah I'm the author behind xtrasonnet. Glad our syntax ended up being the same!

jam01 avatar Jul 26 '23 21:07 jam01

Oh, I haven't noticed this :D No, I have taken an idea from xtrasonnet, because often write such code, and this is fairly annoying.

The only thing my implementation has over yours is computed index syntax a?.[field], borrowed from javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#optional_chaining_with_expressions

CertainLach avatar Jul 26 '23 21:07 CertainLach

Ah, great idea

jam01 avatar Jul 26 '23 21:07 jam01