From cfcada535690dd4a2a91503887f55c0b699b4006 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Fri, 30 Dec 2022 10:03:39 -0500 Subject: New coll.Index function Signed-off-by: Dave Henderson --- docs/content/functions/coll.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'docs/content/functions') diff --git a/docs/content/functions/coll.md b/docs/content/functions/coll.md index 55f2b9cf..b78fe4ca 100644 --- a/docs/content/functions/coll.md +++ b/docs/content/functions/coll.md @@ -165,6 +165,48 @@ $ gomplate -i '{{ $o := data.JSON (getenv "DATA") -}} THERE IS NO FOO ``` +## `coll.Index` + +Returns the result of indexing the given map, slice, or array by the given +key or index. This is similar to the built-in `index` function, but the +arguments are ordered differently for pipeline compatibility. Also this +function is more strict, and will return an error when trying to access a +non-existent map key. + +Multiple indexes may be given, for nested indexing. + +### Usage + +```go +coll.Index indexes... in +``` +```go +in | coll.Index indexes... +``` + +### Arguments + +| name | description | +|------|-------------| +| `indexes...` | _(required)_ The key or index | +| `in` | _(required)_ The map, slice, or array to index | + +### Examples + +```console +$ gomplate -i '{{ coll.Index "foo" (dict "foo" "bar") }}' +bar +``` +```console +$ gomplate -i '{{ $m := json `{"foo": {"bar": "baz"}}` -}} + {{ coll.Index "foo" "bar" $m }}' +baz +``` +```console +$ gomplate -i '{{ coll.Slice "foo" "bar" "baz" | coll.Index 1 }}' +bar +``` + ## `coll.JSONPath` **Alias:** `jsonpath` -- cgit v1.2.3