default true false triggers the default value
Hi,
the doc to the default function states:
default: Give a default value. Used like this: trim " "| default "empty". Since trim produces an empty string, the default value is returned. For things with a length (strings, slices, maps), len(0) will trigger the default. For numbers, the value 0 will trigger the default. For booleans, false will trigger the default. For structs, the default is never returned (there is no clear empty condition). For everything else, nil value triggers a default.
A common scenario is to template config files, where you want to define a default value if nothing is given by the user.
default true "my value"
# "my value"
default true ""
# true
If your default option needs to be true and the user supplies false to deactivate the option. The default function will use the default instead of the user supplied value.
default true false
# true
In my opinion this behavior is a pitfall and needs to be changed, that boolean true/false are passed as they are if the default is also boolean or needs to be more clear in the documentation. You can circumvent the problem with the following template:
{{/*
defaulttrue: Give true as default value. Used like this: trim " "| defaulttrue.
Since trim produces an empty string, the default value true is returned. For
things with a length (strings, slices, maps), len(0) will trigger the default value true.
For numbers, the value 0 will trigger the default value true. For booleans, the value is
passed as is. For structs, the default is never returned (there is no clear
empty condition). For everything else, nil value triggers the default value true.
*/}}
{{- define "defaulttrue" -}}
{{- if typeIs "bool" . -}}
{{- . -}}
{{- else -}}
{{- default true . -}}
{{- end -}}
{{- end -}}
Yeah, that solution makes sense to me.
Should this be documented somewhere before closing this issue?
Is there any way to cast string "true/false" to boolean?
Return type of template block is string and I have to cast it into boolean or my if control structure will not work as it should. Every non empty string will be evaluated as true.
Thanks
We could look into adding one in the type conversion functions.
On Tue, Jun 18, 2019, 09:26 Nenad Strainovic [email protected] wrote:
Is there any way to cast string "true/false" to boolean?
Return type of template block is string and I have to cast it into boolean or my if control structure will not work as it should. Every non empty string will be evaluated as true.
Thanks
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Masterminds/sprig/issues/111?email_source=notifications&email_token=AAAVY2IUJ4RFGPELG32KPODP3D5DZA5CNFSM4FOBBA4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODX7AKCQ#issuecomment-503186698, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAVY2NT6NL54YI4U36B5VDP3D5DZANCNFSM4FOBBA4A .
@technosophos That would be great! I've managed to solve it for "if" control structure in a way that I return true (string) if boolean is true and return empty string if boolean is false. It works but it does not work for ternary operator as it expects boolean input.
Can we also specify return type in "define" block?
Thanks!
Any update on this? This is still an issue a year later...
Is it possible to create defaultKey or similar to be executed like this:
anotherKey: {{ hasKey .Values "someKey" | ternary .Values.someKey true }}
Any updates? This is still an issue that I believe is catching lots of people out