英文:
Get value from list if list-item has key==value
问题
我有以下的yaml文件:
items:
- itemName: a
awesome: true
- itemName: b
- itemName: c
awesome: false
我需要一个函数,以a
作为输出,因为项目a
具有属性awesome == true
。我该如何做到这一点?
不起作用的方法:
{{- $result := "nope" }}
{{- range $i := $items }}
{{- if and (and (hasKey $i. awesome) ($i.awesome)) !($result -eq "nope") }}
{{- $result := $i.itemName }}
{{- end }}
{{- end }}
我还没有尝试上述方法,但我相信这种方法不起作用(参见这篇帖子)。我需要的是一个适用于列表的默认函数。我在sprig中找不到任何函数。有什么想法吗?
英文:
I have the following yaml
items:
- itemName: a
awesome: true
- itemName: b
- itemName: c
awesome: false
I need a function that gets a
as an output, since item a
has the attribute awesome == true
. How do I do that?
Approach that does not work:
{{- $result := "nope" }}
{{- range $i := $items }}
{{- if and (and (hasKey $i. awesome) ($i.awesome)) !($result -eq "nope") }}
{{- $result := $i.itemName }}
{{- end }}
{{- end }}
Have not tried the above approach, but am confident that this does not work (see this post). What I'd need is a default function that works for lists. I could not find any in sprig. Any Idea?
答案1
得分: 1
这是要翻译的内容:
基于你声称你知道代码不起作用的帖子,在问题片段中并没有预先声明变量。
在你的代码中,你确实预先声明了变量,但你犯了一个不同的错误。错误在于你使用:=
将值赋给预先声明的变量,然而在模板中,:=
的工作方式与Go本身相同,它会初始化一个新的变量。你需要使用=
来赋值给现有的变量。
https://pkg.go.dev/text/template@go1.17.2#hdr-Variables
在操作中,管道可以初始化一个变量来捕获结果。初始化的语法如下:
$variable := pipeline
其中
$variable
是变量的名称。声明变量的操作不会产生输出。之前声明的变量也可以进行赋值,使用的语法如下:
$variable = pipeline
所以你只需要将{{- $result := $i.itemName }}
改为{{- $result = $i.itemName }}
。
{{- $result := "nope" }}
{{- range $i := $items }}
{{- if and (and (hasKey $i. awesome) ($i.awesome)) !($result -eq "nope") }}
{{- $result = $i.itemName }}
{{- end }}
{{- end }}
一个证明这个代码工作的例子:https://play.golang.org/p/A44yl7jo0v7
英文:
The linked post, based on which you claim you know that the code doesn't work, does not, in the question snippet, pre-declare the variable.
In your code you do pre-declare the variable but you are making a different error. The error is that you are using :=
to assign the value to the pre-declared variable, however :=
in templates works the same as in Go itself, it initializes a new variable. You need to use =
for assignment to an existing variable.
https://pkg.go.dev/text/template@go1.17.2#hdr-Variables
> A pipeline inside an action may initialize a variable to capture the
> result. The initialization has syntax
>
>
> $variable := pipeline
>
>
> where $variable
is the name of the variable. An
> action that declares a variable produces no output.
>
> Variables previously declared can also be assigned, using the syntax
>
>
> $variable = pipeline
>
So all you need to do is to change {{- $result := $i.itemName }}
to {{- $result = $i.itemName }}
.
{{- $result := "nope" }}
{{- range $i := $items }}
{{- if and (and (hasKey $i. awesome) ($i.awesome)) !($result -eq "nope") }}
{{- $result = $i.itemName }}
{{- end }}
{{- end }}
An example to prove this is working: https://play.golang.org/p/A44yl7jo0v7
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论