合并操作示例在Helm中

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

Example of merge operation in helm

问题

在官方文档中,描述非常简陋:

合并两个或多个字典,以给定目标字典的优先级:

$newdict := merge $dest $source1 $source2 这是一个深度合并操作,而不是深度复制操作。被合并的嵌套对象在两个字典上是相同的实例。如果您希望进行深度复制并合并,那么请使用deepCopy函数与合并...

请提供具有输入和输出值的示例吗?

英文:

In offcial documentaion the description is very poor:

> Merge two or more dictionaries into one, giving precedence to the dest
> dictionary:
>
> $newdict := merge $dest $source1 $source2 This is a deep merge
> operation but not a deep copy operation. Nested objects that are
> merged are the same instance on both dicts. If you want a deep copy
> along with the merge, then use the deepCopy function along with
> merging...

Could you please provide example with input and output values ?

答案1

得分: 3

以下是您要翻译的内容:

在合并字典时,具有不同值的键将保留,对于相同键的情况,"dst" 字典具有优先权。例如,执行 merge $dst $src1 $src2 的结果如下:

合并后的字典为:

mergedDict = {
    "default": "default",
    "key": "true",
    "name": "value1",
    "overwrite": "me"
}

或者在 Helm 语法中:

{{ $mergedDict := merge $dst $src1 $src2 }}

将会是:

mergedDict: map[default:default key:true name:value1 overwrite:me]

在 mergeOverwrite 中,优先顺序是从右到左。因此我们有:

{{ $mergedDict := mergeOverwrite $dst $src1 $src2}}

将会是:

mergedDict: map[default:default key:false name:value2 overwrite:overwritten]

或者如下所示:

{
    "default": "default",
    "key": "false",
    "name": "value2",
    "overwrite": "overwritten"
}
英文:

Consider we have 3 maps (or Dict) as follows:

dst = {
    "default": "default",
    "overwrite": "me",
    "key": "true"
}
src1 = {
    "overwrite": "overwritten",
    "key": "false",
    "name": "value1"
}
src2 = {
    "key": "false",
    "name": "value2"
}

You can represent it in helm with:

{{ $dst := dict "default" "default" "overwrite" "me" "key" "true"}}
{{ $src1 := dict "overwrite" "overwritten" "key" "false" "name" "value1"}}
{{ $src2 := dict  "key" "false" "name" "value2"}}

Merge

In merging dicts, keys with different values will be kept, and in the same keys cases, dst dict takes precedence.
For example, result of merge $dst $src1 $src2 would be:

mergedDict = {
    "default": "default",
    "key": "true",
    "name": "value1",
    "overwrite": "me"
}

or in helm syntacs:

{{ $mergedDict := merge $dst $src1 $src2 }}

would be:

mergedDict: map[default:default key:true name:value1 overwrite:me]

mergeOverwrite

In mergeOverwrite, precedence would be from right to left. So we have:

{{ $mergedDict := mergeOverwrite  $dst $src1 $src2}}

would be:

mergedDict: map[default:default key:false name:value2 overwrite:overwritten]

or

{
    "default": "default",
    "key": "false",
    "name": "value2",
    "overwrite": "overwritten"
}

huangapple
  • 本文由 发表于 2023年3月21日 01:52:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793679-4.html
匿名

发表评论

匿名网友

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

确定