nil coalescing ? #1175
-
Can we do nil coalescing? For example I have the following template: {{ $ds := datasource "ds" }}
foo:
bar: {{ $ds.nonExisting | coalesce 1 }} With |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
(I've moved this to a discussion, as this isn't an issue so much as a question) @daniel-torok There is a way to accomplish this, though not in a way that's quite so compact. This can, however, be accomplished with an One issue is that if you try to reference a missing key, gomplate will error - this is by design as gomplate treats missing keys as errors (see #310 for some discussion on this). To avoid this, you'll want to use Here's an example: {{ $ds := datasource "ds" }}
foo:
bar: {{ if has $ds "nonExisting" }}{{ $ds.nonExisting }}{{ else }}1{{end}} Alternately, if the key does exist, but you want to test whether it's false, you could look at the |
Beta Was this translation helpful? Give feedback.
(I've moved this to a discussion, as this isn't an issue so much as a question)
@daniel-torok There is a way to accomplish this, though not in a way that's quite so compact. This can, however, be accomplished with an
if
condition.One issue is that if you try to reference a missing key, gomplate will error - this is by design as gomplate treats missing keys as errors (see #310 for some discussion on this). To avoid this, you'll want to use
has
.Here's an example:
Alternately, if the key does exist, but you want to test whether it's false, you could look at the
ternary
function. H…