Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function toBool to convert string "true" or "false" to real boolean type #10382

Closed
davinkevin opened this issue Nov 22, 2021 · 4 comments
Closed

Comments

@davinkevin
Copy link

Hello everyone,

I'm currently trying to use _helpers.tpl to host logic about a common behaviour:

{{- define "foo.isSSLEnabled" -}}
    {{- $isFooAEnabled := (eq ((.Values.foo).ssl).enabled true ) -}}
    {{- $isIngressSSLEnabled := (and (eq (.Values.ingress).enabled true) (eq ((.Values.ingress).ssl).enabled true))  -}}
    {{- $isSSLEnabled := or $isIngressSSLEnabled $isProxySSLEnabled  -}}
    {{ $isSSLEnabled }}
{{- end -}}

This returns, obviously, a string, so I have to write something like this:

  {{- $isSSLEnabled := eq (include "foo.isSSLEnabled" .) "true" }}
  enable.ssl: "{{ $isSSLEnabled | ternary "on" "off" }}"
  scheme.hostname: "{{ $isSSLEnabled |  ternary "https" "http" }}://{{ .Values.global.hostname }}"

I would prefer, and think, something like this could be more simpler to read:

  {{- $isSSLEnabled := (include "foo.isSSLEnabled" .) | toBool }}
  enable.ssl: "{{ $isSSLEnabled | ternary "on" "off" }}"
  scheme.hostname: "{{ $isSSLEnabled |  ternary "https" "http" }}://{{ .Values.global.hostname }}"

BTW, if you think a better, simpler, more idiomatic solution is possible, feel free to share it with me. I've seen a lot of circumvention (especially by returning anything instead of false in template) but it was still more complex to read / maintain from my point of view.

NOTE: This code example is WAY more simpler than my usecase, I have a lot of condition and usages in our template.

Thank for your help and support

@bacongobbler
Copy link
Member

bacongobbler commented Nov 22, 2021

I think the simplest method would be to define a new function that evaluates the value against whatever "truthy" string values you want to define. If the input string is truthy ("true", "yes", "ON", etc.), return a non-empty string. If it's a falsy value ("false", "no", "off", etc.), return an empty string. That way if, and, and or all evaluate it as expected.

e.g. (spitballing, may not actually compile)

{{- define "foo.strToBool" -}}
    {{- $output := "" -}}
    {{- if or (eq . "true") (eq . "yes") (eq . "on") -}}
        {{- $output = "1" -}}
    {{- end -}}
    {{ $output }}
{{- end -}}

Then you can use it like so:

{{- $isSSLEnabled := include "foo.isSSLEnabled" . | include "foo.strToBool" }}
enable.ssl: "{{ ternary "on" "off" $isSSLEnabled }}"
scheme.hostname: "{{ ternary "https" "http" $isSSLEnabled }}://{{ .Values.global.hostname }}"

In any case you could propose this as a new helper function to sprig, which is the library used by helm to provide helper functions like this one. But everyone may have opinions on what they'd consider to be truthy or non-truthy values which is why I think a local define function may be a better fit.

Another alternative is to just return a non-empty or empty value as the output of isSSLEnabled instead of returning "true" or "false", bypassing the need for yet another function.

@bacongobbler
Copy link
Member

Another alternative is to just return a non-empty or empty value as the output of isSSLEnabled

quick solution: change your last line in isSSLEnabled to

{{ ternary "true" "" $isSSLEnabled }}

@davinkevin
Copy link
Author

Thank you for all this information, you confirm this doesn't exist (yet) and we should rely on "missing value" instead of real boolean, which makes me sad 😅 and harder to debug 🤷‍♂️

I'm not the only one with this need I think: Masterminds/sprig#258

Thank you again, I'll try as soon as possible your solution(s).

@github-actions
Copy link

This issue has been marked as stale because it has been open for 90 days with no activity. This thread will be automatically closed in 30 days if no further activity occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants