From 1740e7f89b59b44fed8c678b586f3a64083f30a3 Mon Sep 17 00:00:00 2001 From: Bernd Gutjahr Date: Thu, 24 Sep 2020 12:29:51 +0200 Subject: [PATCH 1/2] enhanced coelesce_test with some more test cases Signed-off-by: Bernd Gutjahr --- pkg/chartutil/coalesce_test.go | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/pkg/chartutil/coalesce_test.go b/pkg/chartutil/coalesce_test.go index fc55e0a25..6f7c37483 100644 --- a/pkg/chartutil/coalesce_test.go +++ b/pkg/chartutil/coalesce_test.go @@ -78,17 +78,27 @@ func TestCoalesceValues(t *testing.T) { "right": "exists", "scope": "moby", "top": "nope", + "global": map[string]interface{}{ + "nested2": map[string]interface{}{"l0": "moby"}, + }, }, }, withDeps(&chart.Chart{ Metadata: &chart.Metadata{Name: "pequod"}, - Values: map[string]interface{}{"name": "pequod", "scope": "pequod"}, + Values: map[string]interface{}{ + "name": "pequod", + "scope": "pequod", + "global": map[string]interface{}{ + "nested2": map[string]interface{}{"l1": "pequod"}, + }, + }, }, &chart.Chart{ Metadata: &chart.Metadata{Name: "ahab"}, Values: map[string]interface{}{ "global": map[string]interface{}{ - "nested": map[string]interface{}{"foo": "bar"}, + "nested": map[string]interface{}{"foo": "bar"}, + "nested2": map[string]interface{}{"l2": "ahab"}, }, "scope": "ahab", "name": "ahab", @@ -99,7 +109,12 @@ func TestCoalesceValues(t *testing.T) { ), &chart.Chart{ Metadata: &chart.Metadata{Name: "spouter"}, - Values: map[string]interface{}{"scope": "spouter"}, + Values: map[string]interface{}{ + "scope": "spouter", + "global": map[string]interface{}{ + "nested2": map[string]interface{}{"l1": "spouter"}, + }, + }, }, ) @@ -152,6 +167,19 @@ func TestCoalesceValues(t *testing.T) { {"{{.spouter.global.nested.boat}}", "true"}, {"{{.pequod.global.nested.sail}}", "true"}, {"{{.spouter.global.nested.sail}}", ""}, + + {"{{.global.nested2.l0}}", "moby"}, + {"{{.global.nested2.l1}}", ""}, + {"{{.global.nested2.l2}}", ""}, + {"{{.pequod.global.nested2.l0}}", "moby"}, + {"{{.pequod.global.nested2.l1}}", "pequod"}, + {"{{.pequod.global.nested2.l2}}", ""}, + {"{{.pequod.ahab.global.nested2.l0}}", "moby"}, + {"{{.pequod.ahab.global.nested2.l1}}", "pequod"}, + {"{{.pequod.ahab.global.nested2.l2}}", "ahab"}, + {"{{.spouter.global.nested2.l0}}", "moby"}, + {"{{.spouter.global.nested2.l1}}", "spouter"}, + {"{{.spouter.global.nested2.l2}}", ""}, } for _, tt := range tests { From 5cf9735a9dec42ceed7db04b551b5c2f8762f3db Mon Sep 17 00:00:00 2001 From: Bernd Gutjahr Date: Thu, 24 Sep 2020 12:53:24 +0200 Subject: [PATCH 2/2] improved coalesce readability by getting rid of continue statements Signed-off-by: Bernd Gutjahr --- pkg/chartutil/coalesce.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pkg/chartutil/coalesce.go b/pkg/chartutil/coalesce.go index 7b1ee1d80..b49a31b01 100644 --- a/pkg/chartutil/coalesce.go +++ b/pkg/chartutil/coalesce.go @@ -120,13 +120,11 @@ func coalesceGlobals(dest, src map[string]interface{}) { // top-down. CoalesceTables(vv, destvmap) dg[key] = vv - continue } } } else if dv, ok := dg[key]; ok && istable(dv) { // It's not clear if this condition can actually ever trigger. log.Printf("key %s is table. Skipping", key) - continue } else { // TODO: Do we need to do any additional checking on the value? dg[key] = val @@ -159,15 +157,15 @@ func coalesceValues(c *chart.Chart, v map[string]interface{}) { src, ok := val.(map[string]interface{}) if !ok { // If the original value is nil, there is nothing to coalesce, so we don't print - // the warning but simply continue + // the warning if val != nil { log.Printf("warning: skipped value for %s: Not a table.", key) } - continue + } else { + // Because v has higher precedence than nv, dest values override src + // values. + CoalesceTables(dest, src) } - // Because v has higher precedence than nv, dest values override src - // values. - CoalesceTables(dest, src) } } else { // If the key is not in v, copy it from nv.