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 { if len(r.Condition) == 0 {
return true, nil return true, nil
} }
conditionSplit := strings.Split(r.Condition, ".") iValues := values
if len(conditionSplit) != 2 || conditionSplit[1] != "enabled" { 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") 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 { currentKey := ""
return false, fmt.Errorf("environment values field '%s' is nil", conditionSplit[0]) for _, key := range keys[:len(keys)-1] {
} currentKey = fmt.Sprintf("%s.%s", currentKey, key)
vm, ok := v.(map[string]any) value, ok := iValues[key]
if !ok { 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 { if !ok {
return false, nil return false, fmt.Errorf("environment values field '%s' is not a map", currentKey)
}
if vv == true {
return true, nil
} }
}
enabled, ok := iValues["enabled"]
if !ok {
return false, nil 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) { func unmarkNeedsAndTransitives(filteredReleases []Release, allReleases []ReleaseSpec) {

View File

@ -2500,11 +2500,51 @@ func TestConditionEnabled(t *testing.T) {
wantErr: true, wantErr: true,
}, },
{ {
name: "too long condition", name: "nested values",
condition: "rnd42.really.enabled", 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, want: false,
wantErr: true, 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", name: "empty",
condition: "", condition: "",