terraform/lang
Martin Atkins 140c613ae8 lang/funcs: "one" function
In the Terraform language we typically use lists of zero or one values in
some sense interchangably with single values that might be null, because
various Terraform language constructs are designed to work with
collections rather than with nullable values.

In Terraform v0.12 we made the splat operator [*] have a "special power"
of concisely converting from a possibly-null single value into a
zero-or-one list as a way to make that common operation more concise.

In a sense this "one" function is the opposite operation to that special
power: it goes from a zero-or-one collection (list, set, or tuple) to a
possibly-null single value.

This is a concise alternative to the following clunky conditional
expression, with the additional benefit that the following expression is
also not viable for set values, and it also properly handles the case
where there's unexpectedly more than one value:

    length(var.foo) != 0 ? var.foo[0] : null

Instead, we can write:

    one(var.foo)

As with the splat operator, this is a tricky tradeoff because it could be
argued that it's not something that'd be immediately intuitive to someone
unfamiliar with Terraform. However, I think that's justified given how
often zero-or-one collections arise in typical Terraform configurations.
Unlike the splat operator, it should at least be easier to search for its
name and find its documentation the first time you see it in a
configuration.

My expectation that this will become a common pattern is also my
justification for giving it a short, concise name. Arguably it could be
better named something like "oneornull", but that's a pretty clunky name
and I'm not convinced it really adds any clarity for someone who isn't
already familiar with it.
2021-04-12 15:32:03 -07:00
..
blocktoattr add benchmark used for DecoderSpec 2020-10-14 09:19:26 -04:00
funcs lang/funcs: "one" function 2021-04-12 15:32:03 -07:00
testdata/functions-test lang/funcs: Update fileset() function to include path as separate first argument, automatically trim the path argument from results, and ensure results are always canonical with forward slash path separators 2019-08-30 20:19:44 -04:00
data.go remove GetModuleInstanceOutput 2020-04-14 14:49:10 -04:00
data_test.go eval Data needs to operate on whole modules 2020-04-12 10:50:31 -04:00
doc.go lang: new package for the runtime parts of the config language 2018-10-16 18:44:26 -07:00
eval.go Fix diags non-assignment bugs 2020-11-20 13:37:23 -05:00
eval_test.go allow path and terraform in self-block eval 2020-11-02 14:00:58 -05:00
functions.go lang/funcs: "one" function 2021-04-12 15:32:03 -07:00
functions_test.go lang/funcs: "one" function 2021-04-12 15:32:03 -07:00
references.go vendor: switch to HCL 2.0 in the HCL repository 2019-10-02 15:10:21 -07:00
scope.go lang: allow functions to be subject to experiments 2020-11-13 17:25:16 -08:00