github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/conv.md (about)

     1  ---
     2  title: conversion functions
     3  menu:
     4    main:
     5      parent: functions
     6  ---
     7  
     8  These are a collection of functions that mostly help converting from one type
     9  to another - generally from a `string` to something else, and vice-versa.
    10  
    11  ## `conv.Bool`
    12  
    13  **Alias:** `bool`
    14  
    15  **Note:** See also [`conv.ToBool`](#conv-tobool) for a more flexible variant.
    16  
    17  Converts a true-ish string to a boolean. Can be used to simplify conditional statements based on environment variables or other text input.
    18  
    19  _Added in gomplate [v0.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v0.2.0)_
    20  ### Usage
    21  
    22  ```
    23  conv.Bool in
    24  ```
    25  ```
    26  in | conv.Bool
    27  ```
    28  
    29  ### Arguments
    30  
    31  | name | description |
    32  |------|-------------|
    33  | `in` | _(required)_ the input string |
    34  
    35  ### Examples
    36  
    37  _`input.tmpl`:_
    38  ```
    39  {{if bool (getenv "FOO")}}foo{{else}}bar{{end}}
    40  ```
    41  
    42  ```console
    43  $ gomplate < input.tmpl
    44  bar
    45  $ FOO=true gomplate < input.tmpl
    46  foo
    47  ```
    48  
    49  ## `conv.Default`
    50  
    51  **Alias:** `default`
    52  
    53  Provides a default value given an empty input. Empty inputs are `0` for numeric
    54  types, `""` for strings, `false` for booleans, empty arrays/maps, and `nil`.
    55  
    56  Note that this will not provide a default for the case where the input is undefined
    57  (i.e. referencing things like `.foo` where there is no `foo` field of `.`), but
    58  [`conv.Has`](#conv-has) can be used for that.
    59  
    60  _Added in gomplate [v2.5.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.5.0)_
    61  ### Usage
    62  
    63  ```
    64  conv.Default default in
    65  ```
    66  ```
    67  in | conv.Default default
    68  ```
    69  
    70  ### Arguments
    71  
    72  | name | description |
    73  |------|-------------|
    74  | `default` | _(required)_ the default value |
    75  | `in` | _(required)_ the input |
    76  
    77  ### Examples
    78  
    79  ```console
    80  $ gomplate -i '{{ "" | default "foo" }} {{ "bar" | default "baz" }}'
    81  foo bar
    82  ```
    83  
    84  ## `conv.Dict` _(deprecated)_
    85  **Deprecation Notice:** Renamed to [`coll.Dict`](#coll-dict)
    86  
    87  **Alias:** `dict`
    88  
    89  Dict is a convenience function that creates a map with string keys.
    90  Provide arguments as key/value pairs. If an odd number of arguments
    91  is provided, the last is used as the key, and an empty string is
    92  set as the value.
    93  
    94  All keys are converted to strings.
    95  
    96  This function is equivalent to [Sprig's `dict`](http://masterminds.github.io/sprig/dicts.html#dict)
    97  function, as used in [Helm templates](https://docs.helm.sh/chart_template_guide#template-functions-and-pipelines).
    98  
    99  For creating more complex maps, see [`data.JSON`](../data/#data-json) or [`data.YAML`](../data/#data-yaml).
   100  
   101  For creating arrays, see [`coll.Slice`](#coll-slice).
   102  
   103  _Added in gomplate [v3.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.0.0)_
   104  ### Usage
   105  
   106  ```
   107  conv.Dict in...
   108  ```
   109  
   110  ### Arguments
   111  
   112  | name | description |
   113  |------|-------------|
   114  | `in...` | _(required)_ The key/value pairs |
   115  
   116  ### Examples
   117  
   118  ```console
   119  $ gomplate -i '{{ conv.Dict "name" "Frank" "age" 42 | data.ToYAML }}'
   120  age: 42
   121  name: Frank
   122  $ gomplate -i '{{ dict 1 2 3 | toJSON }}'
   123  {"1":2,"3":""}
   124  ```
   125  ```console
   126  $ cat <<EOF| gomplate
   127  {{ define "T1" }}Hello {{ .thing }}!{{ end -}}
   128  {{ template "T1" (dict "thing" "world")}}
   129  {{ template "T1" (dict "thing" "everybody")}}
   130  EOF
   131  Hello world!
   132  Hello everybody!
   133  ```
   134  
   135  ## `conv.Slice` _(deprecated)_
   136  **Deprecation Notice:** Renamed to [`coll.Slice`](#coll-slice)
   137  
   138  **Alias:** `slice`
   139  
   140  Creates a slice (like an array or list). Useful when needing to `range` over a bunch of variables.
   141  
   142  _Added in gomplate [v0.3.0](https://github.com/hairyhenderson/gomplate/releases/tag/v0.3.0)_
   143  ### Usage
   144  
   145  ```
   146  conv.Slice in...
   147  ```
   148  
   149  ### Arguments
   150  
   151  | name | description |
   152  |------|-------------|
   153  | `in...` | _(required)_ the elements of the slice |
   154  
   155  ### Examples
   156  
   157  ```console
   158  $ gomplate -i '{{ range coll.Slice "Bart" "Lisa" "Maggie" }}Hello, {{ . }}{{ end }}'
   159  Hello, Bart
   160  Hello, Lisa
   161  Hello, Maggie
   162  ```
   163  
   164  ## `conv.Has` _(deprecated)_
   165  **Deprecation Notice:** Renamed to [`coll.Has`](#coll-has)
   166  
   167  **Alias:** `has`
   168  
   169  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.
   170  
   171  _Added in gomplate [v1.5.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.5.0)_
   172  ### Usage
   173  
   174  ```
   175  conv.Has in item
   176  ```
   177  
   178  ### Arguments
   179  
   180  | name | description |
   181  |------|-------------|
   182  | `in` | _(required)_ The object or list to search |
   183  | `item` | _(required)_ The item to search for |
   184  
   185  ### Examples
   186  
   187  ```console
   188  $ gomplate -i '{{ $l := coll.Slice "foo" "bar" "baz" }}there is {{ if has $l "bar" }}a{{else}}no{{end}} bar'
   189  there is a bar
   190  ```
   191  ```console
   192  $ export DATA='{"foo": "bar"}'
   193  $ gomplate -i '{{ $o := data.JSON (getenv "DATA") -}}
   194  {{ if (has $o "foo") }}{{ $o.foo }}{{ else }}THERE IS NO FOO{{ end }}'
   195  bar
   196  ```
   197  ```console
   198  $ export DATA='{"baz": "qux"}'
   199  $ gomplate -i '{{ $o := data.JSON (getenv "DATA") -}}
   200  {{ if (has $o "foo") }}{{ $o.foo }}{{ else }}THERE IS NO FOO{{ end }}'
   201  THERE IS NO FOO
   202  ```
   203  
   204  ## `conv.Join`
   205  
   206  **Alias:** `join`
   207  
   208  Concatenates the elements of an array to create a string. The separator string `sep` is placed between elements in the resulting string.
   209  
   210  _Added in gomplate [v0.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v0.4.0)_
   211  ### Usage
   212  
   213  ```
   214  conv.Join in sep
   215  ```
   216  
   217  ### Arguments
   218  
   219  | name | description |
   220  |------|-------------|
   221  | `in` | _(required)_ the array or slice |
   222  | `sep` | _(required)_ the separator |
   223  
   224  ### Examples
   225  
   226  ```console
   227  $ gomplate -i '{{ $a := coll.Slice 1 2 3 }}{{ join $a "-" }}'
   228  1-2-3
   229  ```
   230  
   231  ## `conv.URL`
   232  
   233  **Alias:** `urlParse`
   234  
   235  Parses a string as a URL for later use. Equivalent to [url.Parse](https://golang.org/pkg/net/url/#Parse)
   236  
   237  Any of `url.URL`'s methods can be called on the result.
   238  
   239  _Added in gomplate [v2.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.0.0)_
   240  ### Usage
   241  
   242  ```
   243  conv.URL in
   244  ```
   245  
   246  ### Arguments
   247  
   248  | name | description |
   249  |------|-------------|
   250  | `in` | _(required)_ the URL string to parse |
   251  
   252  ### Examples
   253  
   254  _`input.tmpl`:_
   255  ```
   256  {{ $u := conv.URL "https://example.com:443/foo/bar" }}
   257  The scheme is {{ $u.Scheme }}
   258  The host is {{ $u.Host }}
   259  The path is {{ $u.Path }}
   260  ```
   261  
   262  ```console
   263  $ gomplate < input.tmpl
   264  The scheme is https
   265  The host is example.com:443
   266  The path is /foo/bar
   267  ```
   268  _Call `Redacted` to hide the password in the output:_
   269  ```
   270  $ gomplate -i '{{ (conv.URL "https://user:supersecret@example.com").Redacted }}'
   271  https://user:xxxxx@example.com
   272  ```
   273  
   274  ## `conv.ParseInt`
   275  
   276  _**Note:**_ See [`conv.ToInt64`](#conv-toint64) instead for a simpler and more flexible variant of this function.
   277  
   278  Parses a string as an int64. Equivalent to [strconv.ParseInt](https://golang.org/pkg/strconv/#ParseInt)
   279  
   280  _Added in gomplate [v1.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.4.0)_
   281  ### Usage
   282  
   283  ```
   284  conv.ParseInt
   285  ```
   286  
   287  
   288  ### Examples
   289  
   290  _`input.tmpl`:_
   291  ```
   292  {{ $val := conv.ParseInt (getenv "HEXVAL") 16 32 }}
   293  The value in decimal is {{ $val }}
   294  ```
   295  
   296  ```console
   297  $ HEXVAL=7C0 gomplate < input.tmpl
   298  
   299  The value in decimal is 1984
   300  ```
   301  
   302  ## `conv.ParseFloat`
   303  
   304  _**Note:**_ See [`conv.ToFloat`](#conv-tofloat) instead for a simpler and more flexible variant of this function.
   305  
   306  Parses a string as an float64 for later use. Equivalent to [strconv.ParseFloat](https://golang.org/pkg/strconv/#ParseFloat)
   307  
   308  _Added in gomplate [v1.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.4.0)_
   309  ### Usage
   310  
   311  ```
   312  conv.ParseFloat
   313  ```
   314  
   315  
   316  ### Examples
   317  
   318  _`input.tmpl`:_
   319  ```
   320  {{ $pi := conv.ParseFloat (getenv "PI") 64 }}
   321  {{- if (gt $pi 3.0) -}}
   322  pi is greater than 3
   323  {{- end }}
   324  ```
   325  
   326  ```console
   327  $ PI=3.14159265359 gomplate < input.tmpl
   328  pi is greater than 3
   329  ```
   330  
   331  ## `conv.ParseUint`
   332  
   333  Parses a string as an uint64 for later use. Equivalent to [strconv.ParseUint](https://golang.org/pkg/strconv/#ParseUint)
   334  
   335  _Added in gomplate [v1.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.4.0)_
   336  ### Usage
   337  
   338  ```
   339  conv.ParseUint
   340  ```
   341  
   342  
   343  ### Examples
   344  
   345  _`input.tmpl`:_
   346  ```
   347  {{ conv.ParseInt (getenv "BIG") 16 64 }} is max int64
   348  {{ conv.ParseUint (getenv "BIG") 16 64 }} is max uint64
   349  ```
   350  
   351  ```console
   352  $ BIG=FFFFFFFFFFFFFFFF gomplate < input.tmpl
   353  9223372036854775807 is max int64
   354  18446744073709551615 is max uint64
   355  ```
   356  
   357  ## `conv.Atoi`
   358  
   359  _**Note:**_ See [`conv.ToInt`](#conv-toint) and [`conv.ToInt64`](#conv-toint64) instead for simpler and more flexible variants of this function.
   360  
   361  Parses a string as an int for later use. Equivalent to [strconv.Atoi](https://golang.org/pkg/strconv/#Atoi)
   362  
   363  _Added in gomplate [v1.4.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.4.0)_
   364  ### Usage
   365  
   366  ```
   367  conv.Atoi
   368  ```
   369  
   370  
   371  ### Examples
   372  
   373  _`input.tmpl`:_
   374  ```
   375  {{ $number := conv.Atoi (getenv "NUMBER") }}
   376  {{- if (gt $number 5) -}}
   377  The number is greater than 5
   378  {{- else -}}
   379  The number is less than 5
   380  {{- end }}
   381  ```
   382  
   383  ```console
   384  $ NUMBER=21 gomplate < input.tmpl
   385  The number is greater than 5
   386  ```
   387  
   388  ## `conv.ToBool`
   389  
   390  Converts the input to a boolean value.
   391  Possible `true` values are: `1` or the strings `"t"`, `"true"`, or `"yes"`
   392  (any capitalizations). All other values are considered `false`.
   393  
   394  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
   395  ### Usage
   396  
   397  ```
   398  conv.ToBool input
   399  ```
   400  ```
   401  input | conv.ToBool
   402  ```
   403  
   404  ### Arguments
   405  
   406  | name | description |
   407  |------|-------------|
   408  | `input` | _(required)_ The input to convert |
   409  
   410  ### Examples
   411  
   412  ```console
   413  $ gomplate -i '{{ conv.ToBool "yes" }} {{ conv.ToBool true }} {{ conv.ToBool "0x01" }}'
   414  true true true
   415  $ gomplate -i '{{ conv.ToBool false }} {{ conv.ToBool "blah" }} {{ conv.ToBool 0 }}'
   416  false false false
   417  ```
   418  
   419  ## `conv.ToBools`
   420  
   421  Converts a list of inputs to an array of boolean values.
   422  Possible `true` values are: `1` or the strings `"t"`, `"true"`, or `"yes"`
   423  (any capitalizations). All other values are considered `false`.
   424  
   425  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
   426  ### Usage
   427  
   428  ```
   429  conv.ToBools input
   430  ```
   431  ```
   432  input | conv.ToBools
   433  ```
   434  
   435  ### Arguments
   436  
   437  | name | description |
   438  |------|-------------|
   439  | `input` | _(required)_ The input array to convert |
   440  
   441  ### Examples
   442  
   443  ```console
   444  $ gomplate -i '{{ conv.ToBools "yes" true "0x01" }}'
   445  [true true true]
   446  $ gomplate -i '{{ conv.ToBools false "blah" 0 }}'
   447  [false false false]
   448  ```
   449  
   450  ## `conv.ToInt64`
   451  
   452  Converts the input to an `int64` (64-bit signed integer).
   453  
   454  This function attempts to convert most types of input (strings, numbers,
   455  and booleans), but behaviour when the input can not be converted is
   456  undefined and subject to change. Unconvertable inputs may result in
   457  errors, or `0` or `-1`.
   458  
   459  Floating-point numbers (with decimal points) are truncated.
   460  
   461  _Added in gomplate [v2.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.2.0)_
   462  ### Usage
   463  
   464  ```
   465  conv.ToInt64 in
   466  ```
   467  
   468  ### Arguments
   469  
   470  | name | description |
   471  |------|-------------|
   472  | `in` | _(required)_ the value to convert |
   473  
   474  ### Examples
   475  
   476  ```console
   477  $ gomplate -i '{{conv.ToInt64 "9223372036854775807"}}'
   478  9223372036854775807
   479  ```
   480  ```console
   481  $ gomplate -i '{{conv.ToInt64 "0x42"}}'
   482  66
   483  ```
   484  ```console
   485  $ gomplate -i '{{conv.ToInt64 true }}'
   486  1
   487  ```
   488  
   489  ## `conv.ToInt`
   490  
   491  Converts the input to an `int` (signed integer, 32- or 64-bit depending
   492  on platform). This is similar to [`conv.ToInt64`](#conv-toint64) on 64-bit
   493  platforms, but is useful when input to another function must be provided
   494  as an `int`.
   495  
   496  On 32-bit systems, given a number that is too large to fit in an `int`,
   497  the result is `-1`. This is done to protect against
   498  [CWE-190](https://cwe.mitre.org/data/definitions/190.html) and
   499  [CWE-681](https://cwe.mitre.org/data/definitions/681.html).
   500  
   501  See also [`conv.ToInt64`](#conv-toint64).
   502  
   503  _Added in gomplate [v2.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.2.0)_
   504  ### Usage
   505  
   506  ```
   507  conv.ToInt in
   508  ```
   509  
   510  ### Arguments
   511  
   512  | name | description |
   513  |------|-------------|
   514  | `in` | _(required)_ the value to convert |
   515  
   516  ### Examples
   517  
   518  ```console
   519  $ gomplate -i '{{conv.ToInt "9223372036854775807"}}'
   520  9223372036854775807
   521  ```
   522  ```console
   523  $ gomplate -i '{{conv.ToInt "0x42"}}'
   524  66
   525  ```
   526  ```console
   527  $ gomplate -i '{{conv.ToInt true }}'
   528  1
   529  ```
   530  
   531  ## `conv.ToInt64s`
   532  
   533  Converts the inputs to an array of `int64`s.
   534  
   535  This delegates to [`conv.ToInt64`](#conv-toint64) for each input argument.
   536  
   537  _Added in gomplate [v2.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.2.0)_
   538  ### Usage
   539  
   540  ```
   541  conv.ToInt64s in...
   542  ```
   543  
   544  ### Arguments
   545  
   546  | name | description |
   547  |------|-------------|
   548  | `in...` | _(required)_ the inputs to be converted |
   549  
   550  ### Examples
   551  
   552  ```console
   553  gomplate -i '{{ conv.ToInt64s true 0x42 "123,456.99" "1.2345e+3"}}'
   554  [1 66 123456 1234]
   555  ```
   556  
   557  ## `conv.ToInts`
   558  
   559  Converts the inputs to an array of `int`s.
   560  
   561  This delegates to [`conv.ToInt`](#conv-toint) for each input argument.
   562  
   563  _Added in gomplate [v2.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.2.0)_
   564  ### Usage
   565  
   566  ```
   567  conv.ToInts in...
   568  ```
   569  
   570  ### Arguments
   571  
   572  | name | description |
   573  |------|-------------|
   574  | `in...` | _(required)_ the inputs to be converted |
   575  
   576  ### Examples
   577  
   578  ```console
   579  gomplate -i '{{ conv.ToInts true 0x42 "123,456.99" "1.2345e+3"}}'
   580  [1 66 123456 1234]
   581  ```
   582  
   583  ## `conv.ToFloat64`
   584  
   585  Converts the input to a `float64`.
   586  
   587  This function attempts to convert most types of input (strings, numbers,
   588  and booleans), but behaviour when the input can not be converted is
   589  undefined and subject to change. Unconvertable inputs may result in
   590  errors, or `0` or `-1`.
   591  
   592  _Added in gomplate [v2.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.2.0)_
   593  ### Usage
   594  
   595  ```
   596  conv.ToFloat64 in
   597  ```
   598  
   599  ### Arguments
   600  
   601  | name | description |
   602  |------|-------------|
   603  | `in` | _(required)_ the value to convert |
   604  
   605  ### Examples
   606  
   607  ```console
   608  $ gomplate -i '{{ conv.ToFloat64 "8.233e-1"}}'
   609  0.8233
   610  $ gomplate -i '{{ conv.ToFloat64 "9,000.09"}}'
   611  9000.09
   612  ```
   613  
   614  ## `conv.ToFloat64s`
   615  
   616  Converts the inputs to an array of `float64`s.
   617  
   618  This delegates to [`conv.ToFloat64`](#conv-tofloat64) for each input argument.
   619  
   620  _Added in gomplate [v2.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.2.0)_
   621  ### Usage
   622  
   623  ```
   624  conv.ToFloat64s in...
   625  ```
   626  
   627  ### Arguments
   628  
   629  | name | description |
   630  |------|-------------|
   631  | `in...` | _(required)_ the inputs to be converted |
   632  
   633  ### Examples
   634  
   635  ```console
   636  $ gomplate -i '{{ conv.ToFloat64s true 0x42 "123,456.99" "1.2345e+3"}}'
   637  [1 66 123456.99 1234.5]
   638  ```
   639  
   640  ## `conv.ToString`
   641  
   642  Converts the input (of any type) to a `string`.
   643  
   644  The input will always be represented in _some_ way.
   645  
   646  _Added in gomplate [v2.5.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.5.0)_
   647  ### Usage
   648  
   649  ```
   650  conv.ToString in
   651  ```
   652  
   653  ### Arguments
   654  
   655  | name | description |
   656  |------|-------------|
   657  | `in` | _(required)_ the value to convert |
   658  
   659  ### Examples
   660  
   661  ```console
   662  $ gomplate -i '{{ conv.ToString 0xFF }}'
   663  255
   664  $ gomplate -i '{{ dict "foo" "bar" | conv.ToString}}'
   665  map[foo:bar]
   666  $ gomplate -i '{{ conv.ToString nil }}'
   667  nil
   668  ```
   669  
   670  ## `conv.ToStrings`
   671  
   672  Converts the inputs (of any type) to an array of `string`s
   673  
   674  This delegates to [`conv.ToString`](#conv-tostring) for each input argument.
   675  
   676  _Added in gomplate [v2.5.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.5.0)_
   677  ### Usage
   678  
   679  ```
   680  conv.ToStrings in...
   681  ```
   682  
   683  ### Arguments
   684  
   685  | name | description |
   686  |------|-------------|
   687  | `in...` | _(required)_ the inputs to be converted |
   688  
   689  ### Examples
   690  
   691  ```console
   692  $ gomplate -i '{{ conv.ToStrings nil 42 true 0xF (coll.Slice 1 2 3) }}'
   693  [nil 42 true 15 [1 2 3]]
   694  ```