test functions
The test namespace contains some simple functions to help validate
assumptions and can cause template generation to fail in specific cases.
test.Assert
Alias: assert
Asserts that the given expression or value is true. If it is not, causes
template generation to fail immediately with an optional message.
Added in gomplate v2.7.0
Usage
test.Assert [message] value
value | test.Assert [message]
Arguments
| name | description |
|---|---|
message |
(optional) The optional message to provide in the case of failure |
value |
(required) The value to test |
Examples
test.Fail
Alias: fail
Cause template generation to fail immediately, with an optional message.
Added in gomplate v2.7.0
Usage
test.Fail [message]
message | test.Fail
Arguments
| name | description |
|---|---|
message |
(optional) The optional message to provide |
Examples
test.IsKind
Alias: isKind
Report whether the argument is of the given Kind. Can be used to render different templates depending on the kind of data.
See the Go reflect source code
for the complete list, but these are some common values:
stringboolint,int64,uint64float64slicemapinvalid(a catch-all, usually justnilvalues)
In addition, the special kind number is accepted by this function, to
represent any numeric kind (whether float32, uint8, or whatever).
This is useful when the specific numeric type is unknown.
See also test.Kind.
Added in gomplate v3.8.0
Usage
test.IsKind kind value
value | test.IsKind kind
Arguments
| name | description |
|---|---|
kind |
(required) the kind to compare with (see description for possible values) |
value |
(required) the value to check |
Examples
test.JSONSchema (experimental)
Experimental: This function is experimental and may be enabled with the --experimental flag.
Validates the given value against a JSON Schema, and returns the value unchanged. If the value does not match the schema, template generation fails with a list of every failing location.
The schema can be a JSON string, or already-parsed data such as a
datasource. Schemas without a $schema keyword are
treated as draft 2020-12; drafts 4, 6, 7 and 2019-09 are also supported.
References within the schema (e.g. #/$defs/name) are supported, but
$refs to other files or URLs are not loaded.
Added in gomplate v5.3.0
Usage
test.JSONSchema schema value
value | test.JSONSchema schema
Arguments
| name | description |
|---|---|
schema |
(required) the JSON Schema, as a JSON string or parsed data |
value |
(required) the value to validate |
Examples
test.Kind
Alias: kind
Report the kind of the given argument. This differs from the type of
the argument in specificity; for example, while a slice of strings may
have a type of []string, the kind of that slice will simply be slice.
If you need to know the precise type of a value, use printf "%T" $value.
See also test.IsKind.
Added in gomplate v3.8.0
Usage
test.Kind value
value | test.Kind
Arguments
| name | description |
|---|---|
value |
(required) the value to check |
Examples
test.Required
Alias: required
Passes through the given value, if it’s non-empty, and non-nil. Otherwise,
exits and prints a given error message so the user can adjust as necessary.
This is particularly useful for cases where templates require user-provided data (such as datasources or environment variables), and rendering can not continue correctly.
This was inspired by Helm’s required function,
but has slightly different behaviour. Notably, gomplate will always fail in
cases where a referenced key is missing, and this function will have no
effect.
Added in gomplate v3.0.0
Usage
test.Required [message] value
value | test.Required [message]
Arguments
| name | description |
|---|---|
message |
(optional) The optional message to provide when the required value is not provided |
value |
(required) The required value |
Examples
test.Ternary
Alias: ternary
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 function (values like true, 1, "true", "Yes", etc… are considered true).
This is effectively a short-form of the following template:
{{ if conv.ToBool $condition }}{{ $truevalue }}{{ else }}{{ $falsevalue }}{{ end }}
Keep in mind that using an explicit if/else block is often easier to understand than ternary expressions!
Added in gomplate v3.1.0
Usage
test.Ternary truevalue falsevalue condition
condition | test.Ternary truevalue falsevalue
Arguments
| name | description |
|---|---|
truevalue |
(required) the value to return if condition is true |
falsevalue |
(required) the value to return if condition is false |
condition |
(required) the value to evaluate for truthiness |