我该如何做这样的事情:
{{ $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 }}
我在 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/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin