“Example of merge operation in helm” can be translated to “Helm中合并操作的示例.”

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

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 的结果将是:

或者在helm语法中:

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

将会是:

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

mergeOverwrite

在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-2.html
匿名

发表评论

匿名网友

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

确定