github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/coll.md (about) 1 --- 2 title: collection functions 3 menu: 4 main: 5 parent: functions 6 --- 7 8 These functions help manipulate and query collections of data, like lists (slices, or arrays) and maps (dictionaries). 9 10 #### Implementation Note 11 For the functions that return an array, a Go `[]interface{}` is returned, regardless of whether or not the 12 input was a different type. 13 14 ## `coll.Dict` 15 16 **Alias:** `dict` 17 18 Dict is a convenience function that creates a map with string keys. 19 Provide arguments as key/value pairs. If an odd number of arguments 20 is provided, the last is used as the key, and an empty string is 21 set as the value. 22 23 All keys are converted to strings. 24 25 This function is equivalent to [Sprig's `dict`](http://masterminds.github.io/sprig/dicts.html#dict) 26 function, as used in [Helm templates](https://docs.helm.sh/chart_template_guide#template-functions-and-pipelines). 27 28 For creating more complex maps, see [`data.JSON`](../data/#data-json) or [`data.YAML`](../data/#data-yaml). 29 30 For creating arrays, see [`coll.Slice`](#coll-slice). 31 32 _Added in gomplate [v3.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.2.0)_ 33 ### Usage 34 35 ``` 36 coll.Dict in... 37 ``` 38 39 ### Arguments 40 41 | name | description | 42 |------|-------------| 43 | `in...` | _(required)_ The key/value pairs | 44 45 ### Examples 46 47 ```console 48 $ gomplate -i '{{ coll.Dict "name" "Frank" "age" 42 | data.ToYAML }}' 49 age: 42 50 name: Frank 51 $ gomplate -i '{{ dict 1 2 3 | toJSON }}' 52 {"1":2,"3":""} 53 ``` 54 ```console 55 $ cat <<EOF| gomplate 56 {{ define "T1" }}Hello {{ .thing }}!{{ end -}} 57 {{ template "T1" (dict "thing" "world")}} 58 {{ template "T1" (dict "thing" "everybody")}} 59 EOF 60 Hello world! 61 Hello everybody! 62 ``` 63 64 ## `coll.Slice` _(deprecated)_ 65 **Deprecation Notice:** The `slice` alias is deprecated, use the full name `coll.Slice` instead. 66 67 **Alias:** `slice` 68 69 Creates a slice (like an array or list). Useful when needing to `range` over a bunch of variables. 70 71 _Added in gomplate [v3.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.2.0)_ 72 ### Usage 73 74 ``` 75 coll.Slice in... 76 ``` 77 78 ### Arguments 79 80 | name | description | 81 |------|-------------| 82 | `in...` | _(required)_ the elements of the slice | 83 84 ### Examples 85 86 ```console 87 $ gomplate -i '{{ range coll.Slice "Bart" "Lisa" "Maggie" }}Hello, {{ . }}{{ end }}' 88 Hello, Bart 89 Hello, Lisa 90 Hello, Maggie 91 ``` 92 93 ## `coll.GoSlice`_(unreleased)_ 94 **Unreleased:** _This function is in development, and not yet available in released builds of gomplate._ 95 96 This exposes the `slice` function from Go's [`text/template`](https://golang.org/pkg/text/template/#hdr-Functions) 97 package. Note that using `slice` will use the `coll.Slice` function instead, 98 which may not be desired. 99 For some background on this, see [this issue](https://github.com/hairyhenderson/gomplate/issues/1461). 100 101 Here is the upstream documentation: 102 103 ``` 104 slice returns the result of slicing its first argument by the 105 remaining arguments. Thus "slice x 1 2" is, in Go syntax, x[1:2], 106 while "slice x" is x[:], "slice x 1" is x[1:], and "slice x 1 2 3" 107 is x[1:2:3]. The first argument must be a string, slice, or array. 108 ``` 109 110 See the [Go language spec](https://go.dev/ref/spec#Slice_expressions) for 111 more details. 112 113 ### Usage 114 115 ``` 116 coll.GoSlice item [indexes...] 117 ``` 118 119 ### Arguments 120 121 | name | description | 122 |------|-------------| 123 | `item` | _(required)_ the string, slice, or array to slice | 124 | `indexes...` | _(optional)_ the indexes to slice the item by (0 to 3 arguments) | 125 126 ### Examples 127 128 ```console 129 $ gomplate -i '{{ coll.GoSlice "hello world" 3 8 }}' 130 lo wo 131 ``` 132 133 ## `coll.Has` 134 135 **Alias:** `has` 136 137 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. 138 139 _Added in gomplate [v3.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.2.0)_ 140 ### Usage 141 142 ``` 143 coll.Has in item 144 ``` 145 146 ### Arguments 147 148 | name | description | 149 |------|-------------| 150 | `in` | _(required)_ The object or list to search | 151 | `item` | _(required)_ The item to search for | 152 153 ### Examples 154 155 ```console 156 $ gomplate -i '{{ $l := coll.Slice "foo" "bar" "baz" }}there is {{ if has $l "bar" }}a{{else}}no{{end}} bar' 157 there is a bar 158 ``` 159 ```console 160 $ export DATA='{"foo": "bar"}' 161 $ gomplate -i '{{ $o := data.JSON (getenv "DATA") -}} 162 {{ if (has $o "foo") }}{{ $o.foo }}{{ else }}THERE IS NO FOO{{ end }}' 163 bar 164 ``` 165 ```console 166 $ export DATA='{"baz": "qux"}' 167 $ gomplate -i '{{ $o := data.JSON (getenv "DATA") -}} 168 {{ if (has $o "foo") }}{{ $o.foo }}{{ else }}THERE IS NO FOO{{ end }}' 169 THERE IS NO FOO 170 ``` 171 172 ## `coll.Index`_(unreleased)_ 173 **Unreleased:** _This function is in development, and not yet available in released builds of gomplate._ 174 175 Returns the result of indexing the given map, slice, or array by the given 176 key or index. This is similar to the built-in `index` function, but the 177 arguments are ordered differently for pipeline compatibility. Also this 178 function is more strict, and will return an error when trying to access a 179 non-existent map key. 180 181 Multiple indexes may be given, for nested indexing. 182 183 ### Usage 184 185 ``` 186 coll.Index indexes... in 187 ``` 188 ``` 189 in | coll.Index indexes... 190 ``` 191 192 ### Arguments 193 194 | name | description | 195 |------|-------------| 196 | `indexes...` | _(required)_ The key or index | 197 | `in` | _(required)_ The map, slice, or array to index | 198 199 ### Examples 200 201 ```console 202 $ gomplate -i '{{ coll.Index "foo" (dict "foo" "bar") }}' 203 bar 204 ``` 205 ```console 206 $ gomplate -i '{{ $m := json `{"foo": {"bar": "baz"}}` -}} 207 {{ coll.Index "foo" "bar" $m }}' 208 baz 209 ``` 210 ```console 211 $ gomplate -i '{{ coll.Slice "foo" "bar" "baz" | coll.Index 1 }}' 212 bar 213 ``` 214 215 ## `coll.JSONPath` 216 217 **Alias:** `jsonpath` 218 219 Extracts portions of an input object or list using a [JSONPath][] expression. 220 221 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. 222 223 JSONPath expressions can be validated at https://jsonpath.com 224 225 [JSONPath]: https://goessner.net/articles/JsonPath 226 227 _Added in gomplate [v3.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.4.0)_ 228 ### Usage 229 230 ``` 231 coll.JSONPath expression in 232 ``` 233 ``` 234 in | coll.JSONPath expression 235 ``` 236 237 ### Arguments 238 239 | name | description | 240 |------|-------------| 241 | `expression` | _(required)_ The JSONPath expression | 242 | `in` | _(required)_ The object or list to query | 243 244 ### Examples 245 246 ```console 247 $ gomplate -i '{{ .books | jsonpath `$..works[?( @.edition_count > 400 )].title` }}' -c books=https://openlibrary.org/subjects/fantasy.json 248 [Alice's Adventures in Wonderland Gulliver's Travels] 249 ``` 250 251 ## `coll.JQ`_(unreleased)_ 252 **Unreleased:** _This function is in development, and not yet available in released builds of gomplate._ 253 254 **Alias:** `jq` 255 256 Filters an input object or list using the [jq](https://stedolan.github.io/jq/) language, as implemented by [gojq](https://github.com/itchyny/gojq). 257 258 Any JSON datatype may be used as input (NOTE: strings are not JSON-parsed but passed in as is). 259 If the expression results in multiple items (no matter if streamed or as an array) they are wrapped in an array. 260 Otherwise a single item is returned (even if resulting in an array with a single contained element). 261 262 JQ filter expressions can be tested at https://jqplay.org/ 263 264 See also: 265 266 - [jq manual](https://stedolan.github.io/jq/manual/) 267 - [gojq differences to jq](https://github.com/itchyny/gojq#difference-to-jq) 268 269 ### Usage 270 271 ``` 272 coll.JQ expression in 273 ``` 274 ``` 275 in | coll.JQ expression 276 ``` 277 278 ### Arguments 279 280 | name | description | 281 |------|-------------| 282 | `expression` | _(required)_ The JQ expression | 283 | `in` | _(required)_ The object or list to query | 284 285 ### Examples 286 287 ```console 288 $ gomplate \ 289 -i '{{ .books | jq `[.works[]|{"title":.title,"authors":[.authors[].name],"published":.first_publish_year}][0]` }}' \ 290 -c books=https://openlibrary.org/subjects/fantasy.json 291 map[authors:[Lewis Carroll] published:1865 title:Alice's Adventures in Wonderland] 292 ``` 293 294 ## `coll.Keys` 295 296 **Alias:** `keys` 297 298 Return a list of keys in one or more maps. 299 300 The keys will be ordered first by map position (if multiple maps are given), 301 then alphabetically. 302 303 See also [`coll.Values`](#coll-values). 304 305 _Added in gomplate [v3.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.2.0)_ 306 ### Usage 307 308 ``` 309 coll.Keys in... 310 ``` 311 ``` 312 in... | coll.Keys 313 ``` 314 315 ### Arguments 316 317 | name | description | 318 |------|-------------| 319 | `in...` | _(required)_ the maps | 320 321 ### Examples 322 323 ```console 324 $ gomplate -i '{{ coll.Keys (dict "foo" 1 "bar" 2) }}' 325 [bar foo] 326 $ gomplate -i '{{ $map1 := dict "foo" 1 "bar" 2 -}}{{ $map2 := dict "baz" 3 "qux" 4 -}}{{ coll.Keys $map1 $map2 }}' 327 [bar foo baz qux] 328 ``` 329 330 ## `coll.Values` 331 332 **Alias:** `values` 333 334 Return a list of values in one or more maps. 335 336 The values will be ordered first by map position (if multiple maps are given), 337 then alphabetically by key. 338 339 See also [`coll.Keys`](#coll-keys). 340 341 _Added in gomplate [v3.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.2.0)_ 342 ### Usage 343 344 ``` 345 coll.Values in... 346 ``` 347 ``` 348 in... | coll.Values 349 ``` 350 351 ### Arguments 352 353 | name | description | 354 |------|-------------| 355 | `in...` | _(required)_ the maps | 356 357 ### Examples 358 359 ```console 360 $ gomplate -i '{{ coll.Values (dict "foo" 1 "bar" 2) }}' 361 [2 1] 362 $ gomplate -i '{{ $map1 := dict "foo" 1 "bar" 2 -}}{{ $map2 := dict "baz" 3 "qux" 4 -}}{{ coll.Values $map1 $map2 }}' 363 [2 1 3 4] 364 ``` 365 366 ## `coll.Append` 367 368 **Alias:** `append` 369 370 Append a value to the end of a list. 371 372 _Note that this function does not change the given list; it always produces a new one._ 373 374 See also [`coll.Prepend`](#coll-prepend). 375 376 _Added in gomplate [v3.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.2.0)_ 377 ### Usage 378 379 ``` 380 coll.Append value list... 381 ``` 382 ``` 383 list... | coll.Append value 384 ``` 385 386 ### Arguments 387 388 | name | description | 389 |------|-------------| 390 | `value` | _(required)_ the value to add | 391 | `list...` | _(required)_ the slice or array to append to | 392 393 ### Examples 394 395 ```console 396 $ gomplate -i '{{ coll.Slice 1 1 2 3 | append 5 }}' 397 [1 1 2 3 5] 398 ``` 399 400 ## `coll.Prepend` 401 402 **Alias:** `prepend` 403 404 Prepend a value to the beginning of a list. 405 406 _Note that this function does not change the given list; it always produces a new one._ 407 408 See also [`coll.Append`](#coll-append). 409 410 _Added in gomplate [v3.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.2.0)_ 411 ### Usage 412 413 ``` 414 coll.Prepend value list... 415 ``` 416 ``` 417 list... | coll.Prepend value 418 ``` 419 420 ### Arguments 421 422 | name | description | 423 |------|-------------| 424 | `value` | _(required)_ the value to add | 425 | `list...` | _(required)_ the slice or array to prepend to | 426 427 ### Examples 428 429 ```console 430 $ gomplate -i '{{ coll.Slice 4 3 2 1 | prepend 5 }}' 431 [5 4 3 2 1] 432 ``` 433 434 ## `coll.Uniq` 435 436 **Alias:** `uniq` 437 438 Remove any duplicate values from the list, without changing order. 439 440 _Note that this function does not change the given list; it always produces a new one._ 441 442 _Added in gomplate [v3.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.2.0)_ 443 ### Usage 444 445 ``` 446 coll.Uniq list 447 ``` 448 ``` 449 list | coll.Uniq 450 ``` 451 452 ### Arguments 453 454 | name | description | 455 |------|-------------| 456 | `list` | _(required)_ the input list | 457 458 ### Examples 459 460 ```console 461 $ gomplate -i '{{ coll.Slice 1 2 3 2 3 4 1 5 | uniq }}' 462 [1 2 3 4 5] 463 ``` 464 465 ## `coll.Flatten` 466 467 **Alias:** `flatten` 468 469 Flatten a nested list. Defaults to completely flattening all nested lists, 470 but can be limited with `depth`. 471 472 _Note that this function does not change the given list; it always produces a new one._ 473 474 _Added in gomplate [v3.6.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.6.0)_ 475 ### Usage 476 477 ``` 478 coll.Flatten [depth] list 479 ``` 480 ``` 481 list | coll.Flatten [depth] 482 ``` 483 484 ### Arguments 485 486 | name | description | 487 |------|-------------| 488 | `depth` | _(optional)_ maximum depth of nested lists to flatten. Omit or set to `-1` for infinite depth. | 489 | `list` | _(required)_ the input list | 490 491 ### Examples 492 493 ```console 494 $ gomplate -i '{{ "[[1,2],[],[[3,4],[[[5],6],7]]]" | jsonArray | flatten }}' 495 [1 2 3 4 5 6 7] 496 ``` 497 ```console 498 $ gomplate -i '{{ coll.Flatten 2 ("[[1,2],[],[[3,4],[[[5],6],7]]]" | jsonArray) }}' 499 [1 2 3 4 [[5] 6] 7] 500 ``` 501 502 ## `coll.Reverse` 503 504 **Alias:** `reverse` 505 506 Reverse a list. 507 508 _Note that this function does not change the given list; it always produces a new one._ 509 510 _Added in gomplate [v3.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.2.0)_ 511 ### Usage 512 513 ``` 514 coll.Reverse list 515 ``` 516 ``` 517 list | coll.Reverse 518 ``` 519 520 ### Arguments 521 522 | name | description | 523 |------|-------------| 524 | `list` | _(required)_ the list to reverse | 525 526 ### Examples 527 528 ```console 529 $ gomplate -i '{{ coll.Slice 4 3 2 1 | reverse }}' 530 [1 2 3 4] 531 ``` 532 533 ## `coll.Sort` 534 535 **Alias:** `sort` 536 537 Sort a given list. Uses the natural sort order if possible. For inputs 538 that are not sortable (either because the elements are of different types, 539 or of an un-sortable type), the input will simply be returned, unmodified. 540 541 Maps and structs can be sorted by a named key. 542 543 _Note that this function does not modify the input._ 544 545 _Added in gomplate [v3.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.2.0)_ 546 ### Usage 547 548 ``` 549 coll.Sort [key] list 550 ``` 551 ``` 552 list | coll.Sort [key] 553 ``` 554 555 ### Arguments 556 557 | name | description | 558 |------|-------------| 559 | `key` | _(optional)_ the key to sort by, for lists of maps or structs | 560 | `list` | _(required)_ the slice or array to sort | 561 562 ### Examples 563 564 ```console 565 $ gomplate -i '{{ coll.Slice "foo" "bar" "baz" | coll.Sort }}' 566 [bar baz foo] 567 ``` 568 ```console 569 $ gomplate -i '{{ sort (coll.Slice 3 4 1 2 5) }}' 570 [1 2 3 4 5] 571 ``` 572 ```console 573 $ cat <<EOF > in.json 574 [{"a": "foo", "b": 1}, {"a": "bar", "b": 8}, {"a": "baz", "b": 3}] 575 EOF 576 $ gomplate -d in.json -i '{{ range (include "in" | jsonArray | coll.Sort "b") }}{{ print .a "\n" }}{{ end }}' 577 foo 578 baz 579 bar 580 ``` 581 582 ## `coll.Merge` 583 584 **Alias:** `merge` 585 586 Merge maps together by overriding src with dst. 587 588 In other words, the src map can be configured the "default" map, whereas the dst 589 map can be configured the "overrides". 590 591 Many source maps can be provided. Precedence is in left-to-right order. 592 593 _Note that this function does not modify the input._ 594 595 _Added in gomplate [v3.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.2.0)_ 596 ### Usage 597 598 ``` 599 coll.Merge dst srcs... 600 ``` 601 ``` 602 srcs... | coll.Merge dst 603 ``` 604 605 ### Arguments 606 607 | name | description | 608 |------|-------------| 609 | `dst` | _(required)_ the map to merge _into_ | 610 | `srcs...` | _(required)_ the map (or maps) to merge _from_ | 611 612 ### Examples 613 614 ```console 615 $ gomplate -i '{{ $default := dict "foo" 1 "bar" 2}} 616 {{ $config := dict "foo" 8 }} 617 {{ merge $config $default }}' 618 map[bar:2 foo:8] 619 ``` 620 ```console 621 $ gomplate -i '{{ $dst := dict "foo" 1 "bar" 2 }} 622 {{ $src1 := dict "foo" 8 "baz" 4 }} 623 {{ $src2 := dict "foo" 3 "bar" 5 }} 624 {{ coll.Merge $dst $src1 $src2 }}' 625 map[foo:1 bar:5 baz:4] 626 ``` 627 628 ## `coll.Pick` 629 630 Given a map, returns a new map with any entries that have the given keys. 631 632 The keys can either be separate arguments, or a slice (since v4.0.0). 633 634 This is the inverse of [`coll.Omit`](#coll-omit). 635 636 _Note that this function does not modify the input._ 637 638 _Added in gomplate [v3.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.7.0)_ 639 ### Usage 640 641 ``` 642 coll.Pick keys... map 643 ``` 644 ``` 645 map | coll.Pick keys... 646 ``` 647 648 ### Arguments 649 650 | name | description | 651 |------|-------------| 652 | `keys...` | _(required)_ the keys (strings) to match | 653 | `map` | _(required)_ the map to pick from | 654 655 ### Examples 656 657 ```console 658 $ gomplate -i '{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }} 659 {{ coll.Pick "foo" "baz" $data }}' 660 map[baz:3 foo:1] 661 ``` 662 ```console 663 $ gomplate -i '{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }} 664 {{ $keys := coll.Slice "foo" "baz" }} 665 {{ coll.Pick $keys $data }}' 666 map[baz:3 foo:1] 667 ``` 668 669 ## `coll.Omit` 670 671 Given a map, returns a new map without any entries that have the given keys. 672 673 The keys can either be separate arguments, or a slice (since v4.0.0). 674 675 This is the inverse of [`coll.Pick`](#coll-pick). 676 677 _Note that this function does not modify the input._ 678 679 _Added in gomplate [v3.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.7.0)_ 680 ### Usage 681 682 ``` 683 coll.Omit keys... map 684 ``` 685 ``` 686 map | coll.Omit keys... 687 ``` 688 689 ### Arguments 690 691 | name | description | 692 |------|-------------| 693 | `keys...` | _(required)_ the keys (strings) to match | 694 | `map` | _(required)_ the map to omit from | 695 696 ### Examples 697 698 ```console 699 $ gomplate -i '{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }} 700 {{ coll.Omit "foo" "baz" $data }}' 701 map[bar:2] 702 ``` 703 ```console 704 $ gomplate -i '{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }} 705 {{ $keys := coll.Slice "foo" "baz" }} 706 {{ coll.Omit $keys $data }}' 707 map[bar:2] 708 ```