问题
我该如何做这样的事情:
{{ $use_ssl := (ne $.Env.CERT_NAME "") }}
其中 $.Env.CERT_NAME
可能为零/未定义。如果它是零,它给出这个错误:
at <ne $.Env.CERT_NAME "">: error calling ne: invalid type for comparison
注意:我无法控制传递给 Go 模板的对象,因此必须完全在模板本身内解决这个问题。
我尝试过的
我试图通过首先检查它是否为非空来变通:
{{ $use_ssl := ( ($.Env.CERT_NAME) && (ne $.Env.CERT_NAME "") ) }}
但它给出了这个错误:
unexpected "&" in operand
所以我切换到这个,这在语法上是允许的:
{{ $use_ssl := (and ($.Env.CERT_NAME) (ne $.Env.CERT_NAME "") ) }}
但后来我丢失了 short-circuit evaluation我会得到 &&
运算符(operator),我又回到了这个错误:
at <ne $.Env.CERT_NAME ...>: error calling ne: invalid type for comparison
好吧,我想,如果我不能在一个漂亮的单行中做到这一点,并且 Go doesn't have a ternary operator ,没关系,我会做 idiomatic Go way ,这显然是 if/else
。
{{ if $.Env.CERT_NAME }}
{{ $use_ssl := (ne $.Env.CERT_NAME "") }}
{{ else }}
{{ $use_ssl := false }}
{{ end }}
但是当然我遇到了范围问题,因为 if
莫名其妙地(或者至少令人讨厌地)创建了一个新的变量范围(不像我更习惯的 Ruby/ERB 模板),所以显然我什至不能这样做:
{{ if $.Env.CERT_NAME }}
{{ $use_ssl := true }}
{{ else }}
{{ $use_ssl := false }}
{{ end }}
# $use_ssl: {{ $use_ssl }}
现在没有得到这个错误:
undefined variable "$use_ssl"
“没有汗水”,我想。我只是在外部作用域中声明变量,这样它将具有正确的作用域并且内部作用域仍然能够更改该变量(具有外部作用域的变量)。 (顺便说一句,这就是它在 Ruby 中的工作方式。)
不,显然所做的只是创建 2 个具有相同名称但不同范围的不同变量(这怎么会令人困惑!):
{{ $use_ssl := false }}
{{ if $.Env.CERT_NAME }}
{{ $use_ssl := true }}
# $use_ssl: {{ $use_ssl }}
{{ else }}
{{ $use_ssl := false }}
{{ end }}
# $use_ssl: {{ $use_ssl }}
# $use_ssl: true
# $use_ssl: false
我也试过像这样内联 if 语句:
{{ $use_ssl := if $.Env.CERT_NAME { true } }}
但是这给出了这个错误:
unexpected <if> in command
显然 if
语句不能像在 Ruby 中那样在 Go 中用作表达式?
如果我尝试使用 What is the idiomatic Go equivalent of C's ternary operator? 中建议的三元运算符的各种替代方法,我也会遇到语法错误,比如:
c := map[bool]int{true: 1, false: 0} [5 > 4]
当然,您可以使用 $.something
从外部作用域中读取变量,但是如何设置 $ .something
在内部范围内?
如果不是那样,那又如何呢??
所以我错过了什么?这在 Go 模板中甚至可能吗?
Go 模板(我是新手)似乎非常有限。几乎所有你在 Go 中能做的事情在模板中是不可能的。但希望有一个解决方法......
http://play.golang.org/p/SufZdsx-1v有一个巧妙的解决方法,涉及使用 {{$hasFemale := cell false}}
创建一个新对象,然后使用 $hasFemale.Set true
设置一个值,但我如何才能在无法访问评估模板的代码(他们在哪里调用 template.FuncMap
)的情况下做类似的事情?
其他尝试失败的人
https://groups.google.com/forum/#!topic/golang-nuts/MUzNZ9dbHrg
This is the biggest (and only) problem I have with Go. I simply do not understand why this functionality has not been implemented yet.
When the template package is used in a generic context (eg. Executing an arbitrary template file with an arbitrary json file), you might not have the luxury of doing precalculations.
https://github.com/golang/go/issues/10608
This is probably as designed, but it would be really nice if there was a way to get the changed $v outside of the conditional.
This is the #1 template question we get over at Hugo (https://github.com/spf13/hugo). We have added a hack [
$.Scratch.Set "v1" 123
] to work around it for now ...
最佳答案
当前有效的解决方案
如果您更新作用域而不是声明变量,无论是使用 set
还是使用 merge
,您都可以在 if 子句之外访问它。
{{- if eq .podKind "core" -}}
{{- $dummy := set . "matchNodePurpose" .Values.scheduling.corePods.nodeAffinity.matchNodePurpose }}
{{- else if eq .podKind "user" -}}
{{- $dummy := set . "matchNodePurpose" .Values.scheduling.userPods.nodeAffinity.matchNodePurpose }}
{{- end -}}
# Will now output what you decided within an if/else if clause
{{- .matchNodePurpose }}
future 的解决方案(Helm 2.10 可用)
我在 Helm 模板的上下文中遇到了同样的问题,这些模板是预装了一些附加功能的 go 模板,例如 Sprig .
28 March 2018, in this pr一个 Terenary 运算符被添加到 Sprig 中,Helm 也会及时使用它们来解决你我都遇到的问题。
三元
真 |三元 "b""c"
将返回 "b"
错误 |三元 "b""c"
将返回 "c"
关于go-templates - 如何根据表达式有条件地在 Go 模板中设置变量,如果不使用 if 语句包装可能会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36209677/