You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Helm中如何利用动态值列表调用dig函数?

解决Helm中用空格分隔字符串作为多层键查询字典的问题

问题根源

你遇到的问题是dig函数需要接收多个独立的键参数(比如dig "A" "B" "C" "not present" $mydict),但你传入的是单一字符串或列表,不符合它的参数格式,所以无法正确解析。

可行解决方案:自定义递归查询模板

直接用递归逻辑模拟dig的逐层查询行为,同时处理你的空格分隔字符串:

{{- /* 定义递归查询模板,实现dig的核心逻辑 */ -}}
{{- define "dict.lookup" -}}
{{- $dict := index . 0 -}}
{{- $keys := index . 1 -}}
{{- $default := index . 2 -}}
{{- if empty $keys -}}
{{- $dict -}}
{{- else -}}
{{- $currentKey := index $keys 0 -}}
{{- if hasKey $dict $currentKey -}}
{{- /* 递归进入下一层字典 */ -}}
{{- template "dict.lookup" (list (index $dict $currentKey) (slice $keys 1) $default) -}}
{{- else -}}
{{- /* 键不存在时返回默认值 */ -}}
{{- $default -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{- /* 原始数据处理 */ -}}
{{- $mydict := (dict "A" (dict "B" ( dict "C" "Present"))) -}}
{{- $string := "\"A\" \"B\" \"C\"" -}}

{{- /* 清理字符串:去掉引号,分割为键列表 */ -}}
{{- $cleanKeyStr := replace "\"" "" $string -}}
{{- $keys := split " " $cleanKeyStr -}}

{{- /* 调用递归模板查询 */ -}}
{{- $vals := include "dict.lookup" (list $mydict $keys "not present") | trim -}}

{{- /* 结果校验与报错 */ -}}
{{- if eq $vals "not present" }}
{{- fail (printf "Failed to find value") }}
{{- end }}

{{- /* 可选:输出查询结果 */ -}}
查询结果:{{ $vals }}

为什么转列表没用?

dig函数的参数格式是dig [KEY1] [KEY2]... [DEFAULT] [DICT],它要求每个键都是独立参数。如果你直接把列表传给dig,相当于把整个列表当作第一个键去查询,自然找不到对应的值。

内容的提问来源于stack exchange,提问作者user3116846

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 03:44:57