github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/data.md (about) 1 --- 2 title: data functions 3 menu: 4 main: 5 parent: functions 6 --- 7 8 A collection of functions that retrieve, parse, and convert structured data. 9 10 ## `datasource` 11 12 **Alias:** `ds` 13 14 Parses a given datasource (provided by the [`--datasource/-d`](../../usage/#datasource-d) argument or [`defineDatasource`](#definedatasource)). 15 16 If the `alias` is undefined, but is a valid URL, `datasource` will dynamically read from that URL. 17 18 See [Datasources](../../datasources) for (much!) more information. 19 20 _Added in gomplate [v0.5.0](https://github.com/hairyhenderson/gomplate/releases/tag/v0.5.0)_ 21 ### Usage 22 23 ``` 24 datasource alias [subpath] 25 ``` 26 27 ### Arguments 28 29 | name | description | 30 |------|-------------| 31 | `alias` | _(required)_ the datasource alias (or a URL for dynamic use) | 32 | `subpath` | _(optional)_ the subpath to use, if supported by the datasource | 33 34 ### Examples 35 36 _`person.json`:_ 37 ```json 38 { "name": "Dave" } 39 ``` 40 41 ```console 42 $ gomplate -d person.json -i 'Hello {{ (datasource "person").name }}' 43 Hello Dave 44 ``` 45 46 ## `datasourceExists` 47 48 Tests whether or not a given datasource was defined on the commandline (with the 49 [`--datasource/-d`](../../usage/#datasource-d) argument). This is intended mainly to allow 50 a template to be rendered differently whether or not a given datasource was 51 defined. 52 53 Note: this does _not_ verify if the datasource is reachable. 54 55 Useful when used in an `if`/`else` block. 56 57 _Added in gomplate [v1.3.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.3.0)_ 58 ### Usage 59 60 ``` 61 datasourceExists alias 62 ``` 63 64 ### Arguments 65 66 | name | description | 67 |------|-------------| 68 | `alias` | _(required)_ the datasource alias | 69 70 ### Examples 71 72 ```console 73 $ echo '{{if (datasourceExists "test")}}{{datasource "test"}}{{else}}no worries{{end}}' | gomplate 74 no worries 75 ``` 76 77 ## `datasourceReachable` 78 79 Tests whether or not a given datasource is defined and reachable, where the definition of "reachable" differs by datasource, but generally means the data is able to be read successfully. 80 81 Useful when used in an `if`/`else` block. 82 83 _Added in gomplate [v2.5.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.5.0)_ 84 ### Usage 85 86 ``` 87 datasourceReachable alias 88 ``` 89 90 ### Arguments 91 92 | name | description | 93 |------|-------------| 94 | `alias` | _(required)_ the datasource alias | 95 96 ### Examples 97 98 ```console 99 $ gomplate -i '{{if (datasourceReachable "test")}}{{datasource "test"}}{{else}}no worries{{end}}' -d test=https://bogus.example.com/wontwork.json 100 no worries 101 ``` 102 103 ## `listDatasources` 104 105 Lists all the datasources defined, list returned will be sorted in ascending order. 106 107 _Added in gomplate [v3.11.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.11.0)_ 108 ### Usage 109 110 ``` 111 listDatasources 112 ``` 113 114 115 ### Examples 116 117 ```console 118 $ gomplate -d person=env:///FOO -d bar=env:///BAR -i '{{range (listDatasources)}} Datasource-{{.}} {{end}}' 119 Datasource-bar 120 Datasource-person 121 ``` 122 123 ## `defineDatasource` 124 125 Define a datasource alias with target URL inside the template. Overridden by the [`--datasource/-d`](../../usage/#datasource-d) flag. 126 127 Note: once a datasource is defined, it can not be redefined (i.e. if this function is called twice with the same alias, only the first applies). 128 129 This function can provide a good way to set a default datasource when sharing templates. 130 131 See [Datasources](../../datasources) for (much!) more information. 132 133 _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_ 134 ### Usage 135 136 ``` 137 defineDatasource alias url 138 ``` 139 140 ### Arguments 141 142 | name | description | 143 |------|-------------| 144 | `alias` | _(required)_ the datasource alias | 145 | `url` | _(required)_ the datasource's URL | 146 147 ### Examples 148 149 _`person.json`:_ 150 ```json 151 { "name": "Dave" } 152 ``` 153 154 ```console 155 $ gomplate -i '{{ defineDatasource "person" "person.json" }}Hello {{ (ds "person").name }}' 156 Hello Dave 157 $ FOO='{"name": "Daisy"}' gomplate -d person=env:///FOO -i '{{ defineDatasource "person" "person.json" }}Hello {{ (ds "person").name }}' 158 Hello Daisy 159 ``` 160 161 ## `include` 162 163 Includes the content of a given datasource (provided by the [`--datasource/-d`](../../usage/#datasource-d) argument). 164 165 This is similar to [`datasource`](#datasource), except that the data is not parsed. There is no restriction on the type of data included, except that it should be textual. 166 167 _Added in gomplate [v1.8.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.8.0)_ 168 ### Usage 169 170 ``` 171 include alias [subpath] 172 ``` 173 174 ### Arguments 175 176 | name | description | 177 |------|-------------| 178 | `alias` | _(required)_ the datasource alias, as provided by [`--datasource/-d`](../../usage/#datasource-d) | 179 | `subpath` | _(optional)_ the subpath to use, if supported by the datasource | 180 181 ### Examples 182 183 _`person.json`:_ 184 ```json 185 { "name": "Dave" } 186 ``` 187 188 _`input.tmpl`:_ 189 ```go 190 { 191 "people": [ 192 {{ include "person" }} 193 ] 194 } 195 ``` 196 197 ```console 198 $ gomplate -d person.json -f input.tmpl 199 { 200 "people": [ 201 { "name": "Dave" } 202 ] 203 } 204 ``` 205 206 ## `data.JSON` 207 208 **Alias:** `json` 209 210 Converts a JSON string into an object. Works for JSON Objects, but will 211 also parse JSON Arrays. Will not parse other valid JSON types. 212 213 For more explict JSON Array support, see [`data.JSONArray`](#data-jsonarray). 214 215 #### Encrypted JSON support (EJSON) 216 217 If the input is in the [EJSON](https://github.com/Shopify/ejson) format (i.e. has a `_public_key` field), this function will attempt to decrypt the document first. A private key must be provided by one of these methods: 218 219 - set the `EJSON_KEY` environment variable to the private key's value 220 - set the `EJSON_KEY_FILE` environment variable to the path to a file containing the private key 221 - set the `EJSON_KEYDIR` environment variable to the path to a directory containing private keys (filename must be the public key), just like [`ejson decrypt`'s `--keydir`](https://github.com/Shopify/ejson/blob/master/man/man1/ejson.1.ronn) flag. Defaults to `/opt/ejson/keys`. 222 223 _Added in gomplate [v1.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.4.0)_ 224 ### Usage 225 226 ``` 227 data.JSON in 228 ``` 229 ``` 230 in | data.JSON 231 ``` 232 233 ### Arguments 234 235 | name | description | 236 |------|-------------| 237 | `in` | _(required)_ the input string | 238 239 ### Examples 240 241 _`input.tmpl`:_ 242 ``` 243 Hello {{ (getenv "FOO" | json).hello }} 244 ``` 245 246 ```console 247 $ export FOO='{"hello":"world"}' 248 $ gomplate < input.tmpl 249 Hello world 250 ``` 251 252 ## `data.JSONArray` 253 254 **Alias:** `jsonArray` 255 256 Converts a JSON string into a slice. Only works for JSON Arrays. 257 258 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 259 ### Usage 260 261 ``` 262 data.JSONArray in 263 ``` 264 ``` 265 in | data.JSONArray 266 ``` 267 268 ### Arguments 269 270 | name | description | 271 |------|-------------| 272 | `in` | _(required)_ the input string | 273 274 ### Examples 275 276 _`input.tmpl`:_ 277 ``` 278 Hello {{ index (getenv "FOO" | jsonArray) 1 }} 279 ``` 280 281 ```console 282 $ export FOO='[ "you", "world" ]' 283 $ gomplate < input.tmpl 284 Hello world 285 ``` 286 287 ## `data.YAML` 288 289 **Alias:** `yaml` 290 291 Converts a YAML string into an object. Works for YAML Objects but will 292 also parse YAML Arrays. This can be used to access properties of YAML objects. 293 294 For more explict YAML Array support, see [`data.JSONArray`](#data-yamlarray). 295 296 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 297 ### Usage 298 299 ``` 300 data.YAML in 301 ``` 302 ``` 303 in | data.YAML 304 ``` 305 306 ### Arguments 307 308 | name | description | 309 |------|-------------| 310 | `in` | _(required)_ the input string | 311 312 ### Examples 313 314 _`input.tmpl`:_ 315 ``` 316 Hello {{ (getenv "FOO" | yaml).hello }} 317 ``` 318 319 ```console 320 $ export FOO='hello: world' 321 $ gomplate < input.tmpl 322 Hello world 323 ``` 324 325 ## `data.YAMLArray` 326 327 **Alias:** `yamlArray` 328 329 Converts a YAML string into a slice. Only works for YAML Arrays. 330 331 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 332 ### Usage 333 334 ``` 335 data.YAMLArray in 336 ``` 337 ``` 338 in | data.YAMLArray 339 ``` 340 341 ### Arguments 342 343 | name | description | 344 |------|-------------| 345 | `in` | _(required)_ the input string | 346 347 ### Examples 348 349 _`input.tmpl`:_ 350 ``` 351 Hello {{ index (getenv "FOO" | yamlArray) 1 }} 352 ``` 353 354 ```console 355 $ export FOO='[ "you", "world" ]' 356 $ gomplate < input.tmpl 357 Hello world 358 ``` 359 360 ## `data.TOML` 361 362 **Alias:** `toml` 363 364 Converts a [TOML](https://github.com/toml-lang/toml) document into an object. 365 This can be used to access properties of TOML documents. 366 367 Compatible with [TOML v0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md). 368 369 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 370 ### Usage 371 372 ``` 373 data.TOML input 374 ``` 375 ``` 376 input | data.TOML 377 ``` 378 379 ### Arguments 380 381 | name | description | 382 |------|-------------| 383 | `input` | _(required)_ the TOML document to parse | 384 385 ### Examples 386 387 _`input.tmpl`:_ 388 ``` 389 {{ $t := `[data] 390 hello = "world"` -}} 391 Hello {{ (toml $t).hello }} 392 ``` 393 394 ```console 395 $ gomplate -f input.tmpl 396 Hello world 397 ``` 398 399 ## `data.CSV` 400 401 **Alias:** `csv` 402 403 Converts a CSV-format string into a 2-dimensional string array. 404 405 By default, the [RFC 4180](https://tools.ietf.org/html/rfc4180) format is 406 supported, but any single-character delimiter can be specified. 407 408 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 409 ### Usage 410 411 ``` 412 data.CSV [delim] input 413 ``` 414 ``` 415 input | data.CSV [delim] 416 ``` 417 418 ### Arguments 419 420 | name | description | 421 |------|-------------| 422 | `delim` | _(optional)_ the (single-character!) field delimiter, defaults to `","` | 423 | `input` | _(required)_ the CSV-format string to parse | 424 425 ### Examples 426 427 _`input.tmpl`:_ 428 ``` 429 {{ $c := `C,32 430 Go,25 431 COBOL,357` -}} 432 {{ range ($c | csv) -}} 433 {{ index . 0 }} has {{ index . 1 }} keywords. 434 {{ end }} 435 ``` 436 437 ```console 438 $ gomplate < input.tmpl 439 C has 32 keywords. 440 Go has 25 keywords. 441 COBOL has 357 keywords. 442 ``` 443 444 ## `data.CSVByRow` 445 446 **Alias:** `csvByRow` 447 448 Converts a CSV-format string into a slice of maps. 449 450 By default, the [RFC 4180](https://tools.ietf.org/html/rfc4180) format is 451 supported, but any single-character delimiter can be specified. 452 453 Also by default, the first line of the string will be assumed to be the header, 454 but this can be overridden by providing an explicit header, or auto-indexing 455 can be used. 456 457 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 458 ### Usage 459 460 ``` 461 data.CSVByRow [delim] [header] input 462 ``` 463 ``` 464 input | data.CSVByRow [delim] [header] 465 ``` 466 467 ### Arguments 468 469 | name | description | 470 |------|-------------| 471 | `delim` | _(optional)_ the (single-character!) field delimiter, defaults to `","` | 472 | `header` | _(optional)_ list of column names separated by `delim`, set to `""` to get auto-named columns (A-Z), defaults to using the first line of `input` | 473 | `input` | _(required)_ the CSV-format string to parse | 474 475 ### Examples 476 477 _`input.tmpl`:_ 478 ``` 479 {{ $c := `lang,keywords 480 C,32 481 Go,25 482 COBOL,357` -}} 483 {{ range ($c | csvByRow) -}} 484 {{ .lang }} has {{ .keywords }} keywords. 485 {{ end }} 486 ``` 487 488 ```console 489 $ gomplate < input.tmpl 490 C has 32 keywords. 491 Go has 25 keywords. 492 COBOL has 357 keywords. 493 ``` 494 495 ## `data.CSVByColumn` 496 497 **Alias:** `csvByColumn` 498 499 Like [`csvByRow`](#csvByRow), except that the data is presented as a columnar 500 (column-oriented) map. 501 502 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 503 ### Usage 504 505 ``` 506 data.CSVByColumn [delim] [header] input 507 ``` 508 ``` 509 input | data.CSVByColumn [delim] [header] 510 ``` 511 512 ### Arguments 513 514 | name | description | 515 |------|-------------| 516 | `delim` | _(optional)_ the (single-character!) field delimiter, defaults to `","` | 517 | `header` | _(optional)_ list of column names separated by `delim`, set to `""` to get auto-named columns (A-Z), defaults to using the first line of `input` | 518 | `input` | _(required)_ the CSV-format string to parse | 519 520 ### Examples 521 522 _`input.tmpl`:_ 523 ``` 524 {{ $c := `C;32 525 Go;25 526 COBOL;357` -}} 527 {{ $langs := ($c | csvByColumn ";" "lang,keywords").lang -}} 528 {{ range $langs }}{{ . }} 529 {{ end -}} 530 ``` 531 532 ```console 533 $ gomplate < input.tmpl 534 C 535 Go 536 COBOL 537 ``` 538 539 ## `data.CUE`_(unreleased)_ 540 **Unreleased:** _This function is in development, and not yet available in released builds of gomplate._ 541 542 **Alias:** `cue` 543 544 Converts a [CUE](https://cuelang.org/) document into an object. Any type 545 of CUE document is supported. This can be used to access properties of CUE 546 documents. 547 548 Note that the `import` statement is not yet supported, and will result in 549 an error (except for importing builtin packages). 550 551 ### Usage 552 553 ``` 554 data.CUE input 555 ``` 556 ``` 557 input | data.CUE 558 ``` 559 560 ### Arguments 561 562 | name | description | 563 |------|-------------| 564 | `input` | _(required)_ the CUE document to parse | 565 566 ### Examples 567 568 ```console 569 $ gomplate -i '{{ $t := `data: { 570 hello: "world" 571 }` -}} 572 Hello {{ (cue $t).data.hello }}' 573 Hello world 574 ``` 575 576 ## `data.ToJSON` 577 578 **Alias:** `toJSON` 579 580 Converts an object to a JSON document. Input objects may be the result of `json`, `yaml`, `jsonArray`, or `yamlArray` functions, or they could be provided by a `datasource`. 581 582 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 583 ### Usage 584 585 ``` 586 data.ToJSON obj 587 ``` 588 ``` 589 obj | data.ToJSON 590 ``` 591 592 ### Arguments 593 594 | name | description | 595 |------|-------------| 596 | `obj` | _(required)_ the object to marshal | 597 598 ### Examples 599 600 _This is obviously contrived - `json` is used to create an object._ 601 602 _`input.tmpl`:_ 603 ``` 604 {{ (`{"foo":{"hello":"world"}}` | json).foo | toJSON }} 605 ``` 606 607 ```console 608 $ gomplate < input.tmpl 609 {"hello":"world"} 610 ``` 611 612 ## `data.ToJSONPretty` 613 614 **Alias:** `toJSONPretty` 615 616 Converts an object to a pretty-printed (or _indented_) JSON document. 617 Input objects may be the result of functions like `data.JSON`, `data.YAML`, 618 `data.JSONArray`, or `data.YAMLArray` functions, or they could be provided 619 by a [`datasource`](../general/datasource). 620 621 The indent string must be provided as an argument. 622 623 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 624 ### Usage 625 626 ``` 627 data.ToJSONPretty indent obj 628 ``` 629 ``` 630 obj | data.ToJSONPretty indent 631 ``` 632 633 ### Arguments 634 635 | name | description | 636 |------|-------------| 637 | `indent` | _(required)_ the string to use for indentation | 638 | `obj` | _(required)_ the object to marshal | 639 640 ### Examples 641 642 _`input.tmpl`:_ 643 ``` 644 {{ `{"hello":"world"}` | data.JSON | data.ToJSONPretty " " }} 645 ``` 646 647 ```console 648 $ gomplate < input.tmpl 649 { 650 "hello": "world" 651 } 652 ``` 653 654 ## `data.ToYAML` 655 656 **Alias:** `toYAML` 657 658 Converts an object to a YAML document. Input objects may be the result of 659 `data.JSON`, `data.YAML`, `data.JSONArray`, or `data.YAMLArray` functions, 660 or they could be provided by a [`datasource`](../general/datasource). 661 662 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 663 ### Usage 664 665 ``` 666 data.ToYAML obj 667 ``` 668 ``` 669 obj | data.ToYAML 670 ``` 671 672 ### Arguments 673 674 | name | description | 675 |------|-------------| 676 | `obj` | _(required)_ the object to marshal | 677 678 ### Examples 679 680 _This is obviously contrived - `data.JSON` is used to create an object._ 681 682 _`input.tmpl`:_ 683 ``` 684 {{ (`{"foo":{"hello":"world"}}` | data.JSON).foo | data.ToYAML }} 685 ``` 686 687 ```console 688 $ gomplate < input.tmpl 689 hello: world 690 ``` 691 692 ## `data.ToTOML` 693 694 **Alias:** `toTOML` 695 696 Converts an object to a [TOML](https://github.com/toml-lang/toml) document. 697 698 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 699 ### Usage 700 701 ``` 702 data.ToTOML obj 703 ``` 704 ``` 705 obj | data.ToTOML 706 ``` 707 708 ### Arguments 709 710 | name | description | 711 |------|-------------| 712 | `obj` | _(required)_ the object to marshal as a TOML document | 713 714 ### Examples 715 716 ```console 717 $ gomplate -i '{{ `{"foo":"bar"}` | data.JSON | data.ToTOML }}' 718 foo = "bar" 719 ``` 720 721 ## `data.ToCSV` 722 723 **Alias:** `toCSV` 724 725 Converts an object to a CSV document. The input object must be a 2-dimensional 726 array of strings (a `[][]string`). Objects produced by [`data.CSVByRow`](#conv-csvbyrow) 727 and [`data.CSVByColumn`](#conv-csvbycolumn) cannot yet be converted back to CSV documents. 728 729 **Note:** With the exception that a custom delimiter can be used, `data.ToCSV` 730 outputs according to the [RFC 4180](https://tools.ietf.org/html/rfc4180) format, 731 which means that line terminators are `CRLF` (Windows format, or `\r\n`). If 732 you require `LF` (UNIX format, or `\n`), the output can be piped through 733 [`strings.ReplaceAll`](../strings/#strings-replaceall) to replace `"\r\n"` with `"\n"`. 734 735 _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_ 736 ### Usage 737 738 ``` 739 data.ToCSV [delim] input 740 ``` 741 ``` 742 input | data.ToCSV [delim] 743 ``` 744 745 ### Arguments 746 747 | name | description | 748 |------|-------------| 749 | `delim` | _(optional)_ the (single-character!) field delimiter, defaults to `","` | 750 | `input` | _(required)_ the object to convert to a CSV | 751 752 ### Examples 753 754 _`input.tmpl`:_ 755 ```go 756 {{ $rows := (jsonArray `[["first","second"],["1","2"],["3","4"]]`) -}} 757 {{ data.ToCSV ";" $rows }} 758 ``` 759 760 ```console 761 $ gomplate -f input.tmpl 762 first,second 763 1,2 764 3,4 765 ``` 766 767 ## `data.ToCUE`_(unreleased)_ 768 **Unreleased:** _This function is in development, and not yet available in released builds of gomplate._ 769 770 **Alias:** `toCUE` 771 772 Converts an object to a [CUE](https://cuelang.org/) document in canonical 773 format. The input object can be of any type. 774 775 This is roughly equivalent to using the `cue export --out=cue <file>` 776 command to convert from other formats to CUE. 777 778 ### Usage 779 780 ``` 781 data.ToCUE input 782 ``` 783 ``` 784 input | data.ToCUE 785 ``` 786 787 ### Arguments 788 789 | name | description | 790 |------|-------------| 791 | `input` | _(required)_ the object to marshal as a CUE document | 792 793 ### Examples 794 795 ```console 796 $ gomplate -i '{{ `{"foo":"bar"}` | data.JSON | data.ToCUE }}' 797 { 798 foo: "bar" 799 } 800 ``` 801 ```console 802 $ gomplate -i '{{ toCUE "hello world" }}' 803 "hello world" 804 ``` 805 ```console 806 $ gomplate -i '{{ coll.Slice 1 "two" true | data.ToCUE }}' 807 [1, "two", true] 808 ```