From 2155fce1212e595caacf1b19769355114a01b250 Mon Sep 17 00:00:00 2001 From: Nick Van Dyck Date: Tue, 12 Mar 2024 03:04:04 +0100 Subject: [PATCH] Allow for conditions to have a deeper nested structure. (#1360) * allow conditions to have a deeper nested structure Signed-off-by: Nick Van Dyck --- pkg/state/state.go | 39 ++++++++++++++++++++++++-------------- pkg/state/state_test.go | 42 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/pkg/state/state.go b/pkg/state/state.go index a2caaff9..8f7006a5 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -2274,28 +2274,39 @@ func ConditionEnabled(r ReleaseSpec, values map[string]any) (bool, error) { if len(r.Condition) == 0 { return true, nil } - conditionSplit := strings.Split(r.Condition, ".") - if len(conditionSplit) != 2 || conditionSplit[1] != "enabled" { + iValues := values + keys := strings.Split(r.Condition, ".") + if keys[len(keys)-1] != "enabled" { return false, fmt.Errorf("Condition value must be in the form 'foo.enabled' where 'foo' can be modified as necessary") } - if v, ok := values[conditionSplit[0]]; ok { - if v == nil { - return false, fmt.Errorf("environment values field '%s' is nil", conditionSplit[0]) - } - vm, ok := v.(map[string]any) + + currentKey := "" + for _, key := range keys[:len(keys)-1] { + currentKey = fmt.Sprintf("%s.%s", currentKey, key) + value, ok := iValues[key] if !ok { - return false, fmt.Errorf("environment values field '%s' is not a map", conditionSplit[0]) + return false, fmt.Errorf("environment values field '%s' not found", currentKey) } - vv, ok := vm["enabled"] + + iValues, ok = value.(map[string]interface{}) if !ok { - return false, nil - } - if vv == true { - return true, nil + return false, fmt.Errorf("environment values field '%s' is not a map", currentKey) } + } + + enabled, ok := iValues["enabled"] + + if !ok { return false, nil } - return false, fmt.Errorf("environment values does not contain field '%s'", conditionSplit[0]) + + e, ok := enabled.(bool) + + if !ok { + return false, nil + } + + return e, nil } func unmarkNeedsAndTransitives(filteredReleases []Release, allReleases []ReleaseSpec) { diff --git a/pkg/state/state_test.go b/pkg/state/state_test.go index c8dd1836..0504fb8a 100644 --- a/pkg/state/state_test.go +++ b/pkg/state/state_test.go @@ -2500,11 +2500,51 @@ func TestConditionEnabled(t *testing.T) { wantErr: true, }, { - name: "too long condition", + name: "nested values", condition: "rnd42.really.enabled", + values: map[string]any{ + "rnd42": map[string]any{ + "really": map[string]any{ + "enabled": true, + }, + }, + }, + want: true, + wantErr: false, + }, + { + name: "nested values enabled missing", + condition: "rnd42.really.ok", want: false, wantErr: true, }, + { + name: "nested values unknown key", + condition: "rnd42.unknown.enabled", + values: map[string]any{ + "rnd42": map[string]any{ + "really": map[string]any{ + "enabled": true, + }, + }, + }, + want: false, + wantErr: true, + }, + { + name: "nested values invalid type", + condition: "rnd42.invalid.enabled", + values: map[string]any{ + "rnd42": map[string]any{ + "invalid": "hello", + "really": map[string]any{ + "enabled": true, + }, + }, + }, + want: false, + wantErr: true, + }, { name: "empty", condition: "",