From 5c9bda9d8b197cd5b55768fce4be1788312a0cc5 Mon Sep 17 00:00:00 2001 From: Sean Eagan Date: Wed, 18 Jul 2018 16:14:38 -0500 Subject: [PATCH] htk: merge list items with same "name" key This patchset changes the "helm-toolkit.utils.merge" function such that when merging lists it not only removes duplicates, but also optionally merges any items which have the same value for the "name" key, when passing a "merge_same_named" parameter as true. Change-Id: I5105e3649820b41b0dbd6fb36f776bc5ad38c84d --- helm-toolkit/templates/utils/_merge.tpl | 46 ++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/helm-toolkit/templates/utils/_merge.tpl b/helm-toolkit/templates/utils/_merge.tpl index b5ded2d804..d7ba11d3a9 100644 --- a/helm-toolkit/templates/utils/_merge.tpl +++ b/helm-toolkit/templates/utils/_merge.tpl @@ -25,21 +25,27 @@ When merging maps, for each key in the source, if the target does not define that key, the source value is assigned. If both define the key, then the key values are merged using this algorithm (recursively) and the result is assigned to the target key. Slices are merged by appending them and removing any -duplicates. Any other values are merged by simply keeping the source, and -throwing away the target. +duplicates, and when passing a map to this function and including a +"merge_same_named" key set to true, then map items from the slices with the same +value for the "name" key will be merged with each other. Any other values are +merged by simply keeping the source, and throwing away the target. */}} {{- define "helm-toolkit.utils.merge" -}} {{- $local := dict -}} + {{- $_ := set $local "merge_same_named" false -}} {{- if kindIs "map" $ -}} {{- $_ := set $local "values" $.values -}} + {{- if hasKey $ "merge_same_named" -}} + {{- $_ := set $local "merge_same_named" $.merge_same_named -}} + {{- end -}} {{- else -}} {{- $_ := set $local "values" $ -}} {{- end -}} {{- $target := first $local.values -}} {{- range $item := rest $local.values -}} - {{- $call := dict "target" $target "source" . -}} + {{- $call := dict "target" $target "source" . "merge_same_named" $local.merge_same_named -}} {{- $_ := include "helm-toolkit.utils._merge" $call -}} {{- $_ := set $local "result" $call.result -}} {{- end -}} @@ -71,7 +77,7 @@ throwing away the target. {{- end -}} {{- else -}} {{- $targetValue := index $.target $key -}} - {{- $call := dict "target" $targetValue "source" $sourceValue -}} + {{- $call := dict "target" $targetValue "source" $sourceValue "merge_same_named" $.merge_same_named -}} {{- $_ := include "helm-toolkit.utils._merge" $call -}} {{- $_ := set $local "newTargetValue" $call.result -}} {{- end -}} @@ -81,7 +87,37 @@ throwing away the target. {{- else if and (kindIs "slice" $.target) (kindIs "slice" $.source) -}} {{- $call := dict "target" $.target "source" $.source -}} {{- $_ := include "helm-toolkit.utils._merge.append_slice" $call -}} - {{- $_ := set $ "result" (uniq $call.result) -}} + {{- if $.merge_same_named -}} + {{- $_ := set $local "result" list -}} + {{- $_ := set $local "named_items" dict -}} + {{- range $item := $call.result -}} + {{- $_ := set $local "has_name_key" false -}} + {{- if kindIs "map" $item -}} + {{- if hasKey $item "name" -}} + {{- $_ := set $local "has_name_key" true -}} + {{- end -}} + {{- end -}} + + {{- if $local.has_name_key -}} + {{- if hasKey $local.named_items $item.name -}} + {{- $named_item := index $local.named_items $item.name -}} + {{- $call := dict "target" $named_item "source" $item "merge_same_named" $.merge_same_named -}} + {{- $_ := include "helm-toolkit.utils._merge" $call -}} + {{- else -}} + {{- $copy := dict -}} + {{- $copy_call := dict "target" $copy "source" $item -}} + {{- $_ := include "helm-toolkit.utils._merge.shallow" $copy_call -}} + {{- $_ := set $local.named_items $item.name $copy -}} + {{- $_ := set $local "result" (append $local.result $copy) -}} + {{- end -}} + {{- else -}} + {{- $_ := set $local "result" (append $local.result $item) -}} + {{- end -}} + {{- end -}} + {{- else -}} + {{- $_ := set $local "result" $call.result -}} + {{- end -}} + {{- $_ := set $ "result" (uniq $local.result) -}} {{- end -}} {{- end -}}