From 34cfd617dce2b83e92f42af2ce88893f6a92a04b Mon Sep 17 00:00:00 2001 From: Jesse Simpson Date: Sat, 24 Feb 2024 11:38:54 -0500 Subject: [PATCH] refactor: broke boolean expression into functions that protect against cast panics Signed-off-by: Jesse Simpson --- pkg/chart/v2/util/coalesce.go | 2 +- pkg/chart/v2/util/values.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/chart/v2/util/coalesce.go b/pkg/chart/v2/util/coalesce.go index d5d163c5e..307603bae 100644 --- a/pkg/chart/v2/util/coalesce.go +++ b/pkg/chart/v2/util/coalesce.go @@ -300,7 +300,7 @@ func coalesceTablesFullKey(printf printFn, dst, src map[string]interface{}, pref } else { printf("warning: cannot overwrite table with non table for %s (%v)", fullkey, val) } - } else if istable(dv) && val != nil && val != "" && len(dv.(map[string]interface{})) > 0 { + } else if istable(dv) && val != nil && isNonEmptyString(val) && isNonEmptyTable(dv) { printf("warning: destination for %s is a table. Ignoring non-table value (%v)", fullkey, val) } } diff --git a/pkg/chart/v2/util/values.go b/pkg/chart/v2/util/values.go index 6850e8b9b..5dbc2a3a3 100644 --- a/pkg/chart/v2/util/values.go +++ b/pkg/chart/v2/util/values.go @@ -179,6 +179,24 @@ func istable(v interface{}) bool { return ok } +func isNonEmptyTable(val interface{}) bool { + table, ok := val.(map[string]interface{}) + if !ok { + return false + } + + return len(table) > 0 +} + +func isNonEmptyString(val interface{}) bool { + stringContent, ok := val.(string) + if !ok { + return false + } + + return stringContent != "" +} + // PathValue takes a path that traverses a YAML structure and returns the value at the end of that path. // The path starts at the root of the YAML structure and is comprised of YAML keys separated by periods. // Given the following YAML data the value at path "chapter.one.title" is "Loomings".