github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/test.md (about) 1 --- 2 title: test functions 3 menu: 4 main: 5 parent: functions 6 --- 7 8 The `test` namespace contains some simple functions to help validate 9 assumptions and can cause template generation to fail in specific cases. 10 11 ## `test.Assert` 12 13 **Alias:** `assert` 14 15 Asserts that the given expression or value is `true`. If it is not, causes 16 template generation to fail immediately with an optional message. 17 18 _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_ 19 ### Usage 20 21 ``` 22 test.Assert [message] value 23 ``` 24 ``` 25 value | test.Assert [message] 26 ``` 27 28 ### Arguments 29 30 | name | description | 31 |------|-------------| 32 | `message` | _(optional)_ The optional message to provide in the case of failure | 33 | `value` | _(required)_ The value to test | 34 35 ### Examples 36 37 ```console 38 $ gomplate -i '{{ assert (eq "foo" "bar") }}' 39 template: <arg>:1:3: executing "<arg>" at <assert (eq "foo" "ba...>: error calling assert: assertion failed 40 $ gomplate -i '{{ assert "something horrible happened" false }}' 41 template: <arg>:1:3: executing "<arg>" at <assert "something ho...>: error calling assert: assertion failed: something horrible happened 42 ``` 43 44 ## `test.Fail` 45 46 **Alias:** `fail` 47 48 Cause template generation to fail immediately, with an optional message. 49 50 _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_ 51 ### Usage 52 53 ``` 54 test.Fail [message] 55 ``` 56 ``` 57 message | test.Fail 58 ``` 59 60 ### Arguments 61 62 | name | description | 63 |------|-------------| 64 | `message` | _(optional)_ The optional message to provide | 65 66 ### Examples 67 68 ```console 69 $ gomplate -i '{{ fail }}' 70 template: <arg>:1:3: executing "<arg>" at <fail>: error calling fail: template generation failed 71 $ gomplate -i '{{ test.Fail "something is wrong!" }}' 72 template: <arg>:1:7: executing "<arg>" at <test.Fail>: error calling Fail: template generation failed: something is wrong! 73 ``` 74 75 ## `test.IsKind` 76 77 **Alias:** `isKind` 78 79 Report whether the argument is of the given Kind. Can be used to render 80 different templates depending on the kind of data. 81 82 See [the Go `reflect` source code](https://github.com/golang/go/blob/36fcde1676a0d3863cb5f295eed6938cd782fcbb/src/reflect/type.go#L595..L622) 83 for the complete list, but these are some common values: 84 85 - `string` 86 - `bool` 87 - `int`, `int64`, `uint64` 88 - `float64` 89 - `slice` 90 - `map` 91 - `invalid` (a catch-all, usually just `nil` values) 92 93 In addition, the special kind `number` is accepted by this function, to 94 represent _any_ numeric kind (whether `float32`, `uint8`, or whatever). 95 This is useful when the specific numeric type is unknown. 96 97 See also [`test.Kind`](test-kind). 98 99 _Added in gomplate [v3.8.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.8.0)_ 100 ### Usage 101 102 ``` 103 test.IsKind kind value 104 ``` 105 ``` 106 value | test.IsKind kind 107 ``` 108 109 ### Arguments 110 111 | name | description | 112 |------|-------------| 113 | `kind` | _(required)_ the kind to compare with (see desription for possible values) | 114 | `value` | _(required)_ the value to check | 115 116 ### Examples 117 118 ```console 119 $ gomplate -i '{{ $data := "hello world" }} 120 {{- if isKind "string" $data }}{{ $data }} is a string{{ end }}' 121 hello world is a string 122 ``` 123 ```console 124 $ gomplate -i '{{ $object := dict "key1" true "key2" "foobar" }} 125 {{- if test.IsKind "map" $object }} 126 Got a map: 127 {{ range $key, $value := $object -}} 128 - "{{ $key }}": {{ $value }} 129 {{ end }} 130 {{ else if test.IsKind "number" $object }} 131 Got a number: {{ $object }} 132 {{ end }}' 133 134 Got a map: 135 - "key1": true 136 - "key2": foobar 137 ``` 138 139 ## `test.Kind` 140 141 **Alias:** `kind` 142 143 Report the _kind_ of the given argument. This differs from the _type_ of 144 the argument in specificity; for example, while a slice of strings may 145 have a type of `[]string`, the _kind_ of that slice will simply be `slice`. 146 147 If you need to know the precise type of a value, use `printf "%T" $value`. 148 149 See also [`test.IsKind`](test-iskind). 150 151 _Added in gomplate [v3.8.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.8.0)_ 152 ### Usage 153 154 ``` 155 test.Kind value 156 ``` 157 ``` 158 value | test.Kind 159 ``` 160 161 ### Arguments 162 163 | name | description | 164 |------|-------------| 165 | `value` | _(required)_ the value to check | 166 167 ### Examples 168 169 ```console 170 $ gomplate -i '{{ kind "hello world" }}' 171 string 172 ``` 173 ```console 174 $ gomplate -i '{{ dict "key1" true "key2" "foobar" | test.Kind }}' 175 map 176 ``` 177 178 ## `test.Required` 179 180 **Alias:** `required` 181 182 Passes through the given value, if it's non-empty, and non-`nil`. Otherwise, 183 exits and prints a given error message so the user can adjust as necessary. 184 185 This is particularly useful for cases where templates require user-provided 186 data (such as datasources or environment variables), and rendering can not 187 continue correctly. 188 189 This was inspired by [Helm's `required` function](https://github.com/kubernetes/helm/blob/master/docs/charts_tips_and_tricks.md#know-your-template-functions), 190 but has slightly different behaviour. Notably, gomplate will always fail in 191 cases where a referenced _key_ is missing, and this function will have no 192 effect. 193 194 _Added in gomplate [v3.0.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.0.0)_ 195 ### Usage 196 197 ``` 198 test.Required [message] value 199 ``` 200 ``` 201 value | test.Required [message] 202 ``` 203 204 ### Arguments 205 206 | name | description | 207 |------|-------------| 208 | `message` | _(optional)_ The optional message to provide when the required value is not provided | 209 | `value` | _(required)_ The required value | 210 211 ### Examples 212 213 ```console 214 $ FOO=foobar gomplate -i '{{ getenv "FOO" | required "Missing FOO environment variable!" }}' 215 foobar 216 $ FOO= gomplate -i '{{ getenv "FOO" | required "Missing FOO environment variable!" }}' 217 error: Missing FOO environment variable! 218 ``` 219 ```console 220 $ cat <<EOF> config.yaml 221 defined: a value 222 empty: "" 223 EOF 224 $ gomplate -d config=config.yaml -i '{{ (ds "config").defined | required "The `config` datasource must have a value defined for `defined`" }}' 225 a value 226 $ gomplate -d config=config.yaml -i '{{ (ds "config").empty | required "The `config` datasource must have a value defined for `empty`" }}' 227 template: <arg>:1:25: executing "<arg>" at <required "The `confi...>: error calling required: The `config` datasource must have a value defined for `empty` 228 $ gomplate -d config=config.yaml -i '{{ (ds "config").bogus | required "The `config` datasource must have a value defined for `bogus`" }}' 229 template: <arg>:1:7: executing "<arg>" at <"config">: map has no entry for key "bogus" 230 ``` 231 232 ## `test.Ternary` 233 234 **Alias:** `ternary` 235 236 Returns one of two values depending on whether the third is true. Note that the third value does not have to be a boolean - it is converted first by the [`conv.ToBool`](../conv/#conv-tobool) function (values like `true`, `1`, `"true"`, `"Yes"`, etc... are considered true). 237 238 This is effectively a short-form of the following template: 239 240 ``` 241 {{ if conv.ToBool $condition }}{{ $truevalue }}{{ else }}{{ $falsevalue }}{{ end }} 242 ``` 243 244 Keep in mind that using an explicit `if`/`else` block is often easier to understand than ternary expressions! 245 246 _Added in gomplate [v3.1.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.1.0)_ 247 ### Usage 248 249 ``` 250 test.Ternary truevalue falsevalue condition 251 ``` 252 ``` 253 condition | test.Ternary truevalue falsevalue 254 ``` 255 256 ### Arguments 257 258 | name | description | 259 |------|-------------| 260 | `truevalue` | _(required)_ the value to return if `condition` is true | 261 | `falsevalue` | _(required)_ the value to return if `condition` is false | 262 | `condition` | _(required)_ the value to evaluate for truthiness | 263 264 ### Examples 265 266 ```console 267 $ gomplate -i '{{ ternary "FOO" "BAR" false }}' 268 BAR 269 $ gomplate -i '{{ ternary "FOO" "BAR" "yes" }}' 270 FOO 271 ```