1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
ns: coll
title: collection functions
preamble: |
These functions help manipulate and query collections of data, like lists (slices, or arrays) and maps (dictionaries).
#### Implementation Note
For the functions that return an array, a Go `[]interface{}` is returned, regardless of whether or not the
input was a different type.
funcs:
- name: coll.Dict
alias: dict
description: |
Dict is a convenience function that creates a map with string keys.
Provide arguments as key/value pairs. If an odd number of arguments
is provided, the last is used as the key, and an empty string is
set as the value.
All keys are converted to strings.
This function is equivalent to [Sprig's `dict`](http://masterminds.github.io/sprig/dicts.html#dict)
function, as used in [Helm templates](https://docs.helm.sh/chart_template_guide#template-functions-and-pipelines).
For creating more complex maps, see [`data.JSON`](../data/#data-json) or [`data.YAML`](../data/#data-yaml).
For creating arrays, see [`coll.Slice`](#coll-slice).
arguments:
- name: in...
required: true
description: The key/value pairs
examples:
- |
$ gomplate -i '{{ coll.Dict "name" "Frank" "age" 42 | data.ToYAML }}'
age: 42
name: Frank
$ gomplate -i '{{ dict 1 2 3 | toJSON }}'
{"1":2,"3":""}
- |
$ cat <<EOF| gomplate
{{ define "T1" }}Hello {{ .thing }}!{{ end -}}
{{ template "T1" (dict "thing" "world")}}
{{ template "T1" (dict "thing" "everybody")}}
EOF
Hello world!
Hello everybody!
- name: coll.Slice
deprecated: The `slice` alias is deprecated, use the full name `coll.Slice` instead.
alias: slice
description: |
Creates a slice (like an array or list). Useful when needing to `range` over a bunch of variables.
pipeline: false
arguments:
- name: in...
required: true
description: the elements of the slice
examples:
- |
$ gomplate -i '{{ range coll.Slice "Bart" "Lisa" "Maggie" }}Hello, {{ . }}{{ end }}'
Hello, Bart
Hello, Lisa
Hello, Maggie
- name: coll.GoSlice
description: |
This exposes the `slice` function from Go's [`text/template`](https://golang.org/pkg/text/template/#hdr-Functions)
package. Note that using `slice` will use the `coll.Slice` function instead,
which may not be desired.
For some background on this, see [this issue](https://github.com/hairyhenderson/gomplate/issues/1461).
Here is the upstream documentation:
```
slice returns the result of slicing its first argument by the
remaining arguments. Thus "slice x 1 2" is, in Go syntax, x[1:2],
while "slice x" is x[:], "slice x 1" is x[1:], and "slice x 1 2 3"
is x[1:2:3]. The first argument must be a string, slice, or array.
```
See the [Go language spec](https://go.dev/ref/spec#Slice_expressions) for
more details.
pipeline: false
arguments:
- name: item
required: true
description: the string, slice, or array to slice
- name: indexes...
required: false
description: the indexes to slice the item by (0 to 3 arguments)
examples:
- |
$ gomplate -i '{{ coll.GoSlice "hello world" 3 8 }}'
lo wo
- name: coll.Has
alias: has
description: |
Reports whether a given object has a property with the given key, or whether a given array/slice contains the given value. Can be used with `if` to prevent the template from trying to access a non-existent property in an object.
pipeline: false
arguments:
- name: in
required: true
description: The object or list to search
- name: item
required: true
description: The item to search for
examples:
- |
$ gomplate -i '{{ $l := coll.Slice "foo" "bar" "baz" }}there is {{ if has $l "bar" }}a{{else}}no{{end}} bar'
there is a bar
- |
$ export DATA='{"foo": "bar"}'
$ gomplate -i '{{ $o := data.JSON (getenv "DATA") -}}
{{ if (has $o "foo") }}{{ $o.foo }}{{ else }}THERE IS NO FOO{{ end }}'
bar
- |
$ export DATA='{"baz": "qux"}'
$ gomplate -i '{{ $o := data.JSON (getenv "DATA") -}}
{{ if (has $o "foo") }}{{ $o.foo }}{{ else }}THERE IS NO FOO{{ end }}'
THERE IS NO FOO
- name: coll.JSONPath
alias: jsonpath
description: |
Extracts portions of an input object or list using a [JSONPath][] expression.
Any object or list may be used as input. The output depends somewhat on the expression; if multiple items are matched, an array is returned.
JSONPath expressions can be validated at https://jsonpath.com
[JSONPath]: https://goessner.net/articles/JsonPath
pipeline: true
arguments:
- name: expression
required: true
description: The JSONPath expression
- name: in
required: true
description: The object or list to query
examples:
- |
$ gomplate -i '{{ .books | jsonpath `$..works[?( @.edition_count > 400 )].title` }}' -c books=https://openlibrary.org/subjects/fantasy.json
[Alice's Adventures in Wonderland Gulliver's Travels]
- name: coll.JQ
alias: jq
description: |
Filters an input object or list using the [jq](https://stedolan.github.io/jq/) language, as implemented by [gojq](https://github.com/itchyny/gojq).
Any JSON datatype may be used as input (NOTE: strings are not JSON-parsed but passed in as is).
If the expression results in multiple items (no matter if streamed or as an array) they are wrapped in an array.
Otherwise a single item is returned (even if resulting in an array with a single contained element).
JQ filter expressions can be tested at https://jqplay.org/
See also:
- [jq manual](https://stedolan.github.io/jq/manual/)
- [gojq differences to jq](https://github.com/itchyny/gojq#difference-to-jq)
pipeline: true
arguments:
- name: expression
required: true
description: The JQ expression
- name: in
required: true
description: The object or list to query
examples:
- |
$ gomplate \
-i '{{ .books | jq `[.works[]|{"title":.title,"authors":[.authors[].name],"published":.first_publish_year}][0]` }}' \
-c books=https://openlibrary.org/subjects/fantasy.json
map[authors:[Lewis Carroll] published:1865 title:Alice's Adventures in Wonderland]
- name: coll.Keys
alias: keys
description: |
Return a list of keys in one or more maps.
The keys will be ordered first by map position (if multiple maps are given),
then alphabetically.
See also [`coll.Values`](#coll-values).
pipeline: true
arguments:
- name: in...
required: true
description: the maps
examples:
- |
$ gomplate -i '{{ coll.Keys (dict "foo" 1 "bar" 2) }}'
[bar foo]
$ gomplate -i '{{ $map1 := dict "foo" 1 "bar" 2 -}}{{ $map2 := dict "baz" 3 "qux" 4 -}}{{ coll.Keys $map1 $map2 }}'
[bar foo baz qux]
- name: coll.Values
alias: values
description: |
Return a list of values in one or more maps.
The values will be ordered first by map position (if multiple maps are given),
then alphabetically by key.
See also [`coll.Keys`](#coll-keys).
pipeline: true
arguments:
- name: in...
required: true
description: the maps
examples:
- |
$ gomplate -i '{{ coll.Values (dict "foo" 1 "bar" 2) }}'
[2 1]
$ gomplate -i '{{ $map1 := dict "foo" 1 "bar" 2 -}}{{ $map2 := dict "baz" 3 "qux" 4 -}}{{ coll.Values $map1 $map2 }}'
[2 1 3 4]
- name: coll.Append
alias: append
description: |
Append a value to the end of a list.
_Note that this function does not change the given list; it always produces a new one._
See also [`coll.Prepend`](#coll-prepend).
pipeline: true
arguments:
- name: value
required: true
description: the value to add
- name: list...
required: true
description: the slice or array to append to
examples:
- |
$ gomplate -i '{{ coll.Slice 1 1 2 3 | append 5 }}'
[1 1 2 3 5]
- name: coll.Prepend
alias: prepend
description: |
Prepend a value to the beginning of a list.
_Note that this function does not change the given list; it always produces a new one._
See also [`coll.Append`](#coll-append).
pipeline: true
arguments:
- name: value
required: true
description: the value to add
- name: list...
required: true
description: the slice or array to prepend to
examples:
- |
$ gomplate -i '{{ coll.Slice 4 3 2 1 | prepend 5 }}'
[5 4 3 2 1]
- name: coll.Uniq
alias: uniq
description: |
Remove any duplicate values from the list, without changing order.
_Note that this function does not change the given list; it always produces a new one._
pipeline: true
arguments:
- name: list
required: true
description: the input list
examples:
- |
$ gomplate -i '{{ coll.Slice 1 2 3 2 3 4 1 5 | uniq }}'
[1 2 3 4 5]
- name: coll.Flatten
alias: flatten
description: |
Flatten a nested list. Defaults to completely flattening all nested lists,
but can be limited with `depth`.
_Note that this function does not change the given list; it always produces a new one._
pipeline: true
arguments:
- name: depth
required: false
description: maximum depth of nested lists to flatten. Omit or set to `-1` for infinite depth.
- name: list
required: true
description: the input list
examples:
- |
$ gomplate -i '{{ "[[1,2],[],[[3,4],[[[5],6],7]]]" | jsonArray | flatten }}'
[1 2 3 4 5 6 7]
- |
$ gomplate -i '{{ coll.Flatten 2 ("[[1,2],[],[[3,4],[[[5],6],7]]]" | jsonArray) }}'
[1 2 3 4 [[5] 6] 7]
- name: coll.Reverse
alias: reverse
description: |
Reverse a list.
_Note that this function does not change the given list; it always produces a new one._
pipeline: true
arguments:
- name: list
required: true
description: the list to reverse
examples:
- |
$ gomplate -i '{{ coll.Slice 4 3 2 1 | reverse }}'
[1 2 3 4]
- name: coll.Sort
alias: sort
description: |
Sort a given list. Uses the natural sort order if possible. For inputs
that are not sortable (either because the elements are of different types,
or of an un-sortable type), the input will simply be returned, unmodified.
Maps and structs can be sorted by a named key.
_Note that this function does not modify the input._
pipeline: true
arguments:
- name: key
required: false
description: the key to sort by, for lists of maps or structs
- name: list
required: true
description: the slice or array to sort
examples:
- |
$ gomplate -i '{{ coll.Slice "foo" "bar" "baz" | coll.Sort }}'
[bar baz foo]
- |
$ gomplate -i '{{ sort (coll.Slice 3 4 1 2 5) }}'
[1 2 3 4 5]
- |
$ cat <<EOF > in.json
[{"a": "foo", "b": 1}, {"a": "bar", "b": 8}, {"a": "baz", "b": 3}]
EOF
$ gomplate -d in.json -i '{{ range (include "in" | jsonArray | coll.Sort "b") }}{{ print .a "\n" }}{{ end }}'
foo
baz
bar
- name: coll.Merge
alias: merge
description: |
Merge maps together by overriding src with dst.
In other words, the src map can be configured the "default" map, whereas the dst
map can be configured the "overrides".
Many source maps can be provided. Precedence is in left-to-right order.
_Note that this function does not modify the input._
pipeline: true
arguments:
- name: dst
required: true
description: the map to merge _into_
- name: srcs...
required: true
description: the map (or maps) to merge _from_
examples:
- |
$ gomplate -i '{{ $default := dict "foo" 1 "bar" 2}}
{{ $config := dict "foo" 8 }}
{{ merge $config $default }}'
map[bar:2 foo:8]
- |
$ gomplate -i '{{ $dst := dict "foo" 1 "bar" 2 }}
{{ $src1 := dict "foo" 8 "baz" 4 }}
{{ $src2 := dict "foo" 3 "bar" 5 }}
{{ coll.Merge $dst $src1 $src2 }}'
map[foo:1 bar:5 baz:4]
- name: coll.Pick
description: |
Given a map, returns a new map with any entries that have the given keys.
All keys are converted to strings.
This is the inverse of [`coll.Omit`](#coll-omit).
_Note that this function does not modify the input._
pipeline: true
arguments:
- name: keys...
required: true
description: the keys to match
- name: map
required: true
description: the map to pick from
examples:
- |
$ gomplate -i '{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }}
{{ coll.Pick "foo" "baz" $data }}'
map[baz:3 foo:1]
- name: coll.Omit
description: |
Given a map, returns a new map without any entries that have the given keys.
All keys are converted to strings.
This is the inverse of [`coll.Pic`](#coll-pick).
_Note that this function does not modify the input._
pipeline: true
arguments:
- name: keys...
required: true
description: the keys to match
- name: map
required: true
description: the map to omit from
examples:
- |
$ gomplate -i '{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }}
{{ coll.Omit "foo" "baz" $data }}'
map[bar:2]
|