将整个 YAML 文件从 values 到 templates 传递,而不要用 null 填充空字段。

huangapple go评论85阅读模式
英文:

Pass an entire yaml from values to templates without filling empty fields with nulls

问题

我正在尝试将给定的values.yaml的一部分传递到helm模板中:

  receivers:
    test1:
      test2:
        test3:
    test4: 

使用以下函数:

{{ .Values.receivers | toYaml | nindent 2}}

代码格式正确,但是空字段会被填充为'null':

receivers:
  test1:
    test2:
      test3: null
  test4: null

有没有办法防止这种情况发生?

我期望模板正确地生成,而不会插入null字段。

英文:

I am trying to pass given part of values.yaml into helm template:

  receivers:
    test1:
      test2:
        test3:
    test4: 

using function:

{{ .Values.receivers | toYaml | nindent 2}}

Code is placed in correct format, however empty fields get filled with 'null':

receivers:
  test1:
    test2:
      test3: null
  test4: null

Is there any way to prevent this?

I am expecting correct templating without insterted null fields.

答案1

得分: 2

没有插入字段。处理器只会用具有相同语义的不同序列化替换已经存在的值。

在没有值的情况下,YAML中的test3:被解析为具有空标量值。YAML核心模式对空值定义如下:

正则表达式 解析为标签
`null Null
/* 空 */ tag:yaml.org,2002:null

由于空值被解析为具有标签!!null(这是上面显示的完整形式的简写),它被加载为**nil**到Go中。

toYaml接收到您的数据时,它不知道**nil**值是由空标量产生的。它需要选择可能的序列化之一,并选择null。这符合YAML规范,因此是正确的行为。

任何支持核心模式的下游处理器都应该以与处理没有值的test3:相同的方式处理test3: null。因此不应该有问题。

如果您希望test3:具体包含空字符串而不是null,请写成:

test3: ""

如果您希望它包含一个空映射,请写成:

test3: {}
英文:

There are no fields inserted. The processor only replaces values that already exist with a different serialization that has the same semantics.

test3: in YAML without a value is parsed as having an empty scalar value. The YAML Core Schema defines the following for empty values:

Regular expression Resolved to tag
`null Null
/* Empty */ tag:yaml.org,2002:null

Since the empty value is resolved to have the tag !!null (which is a shorthand for the full form shown above), it is loaded as nil into Go.

When toYaml receives your data, it doesn't know that the nil values originated from empty scalars. It needs to choose one of the possible serializations and chooses null. This adheres to the YAML spec and is therefore correct behavior.

Any downstream processor that supports the Core Schema should process test3: null in the same way it processes test3: without value. Therefore there should be no problem.

If you want test3: to specifically have the empty string as value instead of null, write

test3: ""

If you want it to contain an empty mapping, write

test3: {}

huangapple
  • 本文由 发表于 2023年2月9日 23:29:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75400822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定