Allow for conditions to have a deeper nested structure. (#1360)

* allow conditions to have a deeper nested structure

Signed-off-by: Nick Van Dyck <vandyck.nick@outlook.com>
This commit is contained in:
Nick Van Dyck 2024-03-12 03:04:04 +01:00 committed by GitHub
parent 5c6eab5e69
commit 2155fce121
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 66 additions and 15 deletions

View File

@ -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) {

View File

@ -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: "",