Skip to content

Commit

Permalink
Update permutations.go
Browse files Browse the repository at this point in the history
Update permutations.go

Update permutations.go

Update permutations.go

Update permutations.go

Update permutations.go
  • Loading branch information
susesgartner committed Dec 19, 2024
1 parent 98e341c commit cf6b338
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions pkg/config/operations/permutations/permutations.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
// Relationship structs are used to create an association between a parent value and either a set of permutations or the value of a different key
type Relationship struct {
ParentValue any `json:"parentValue" yaml:"parentValue"`
ChildKeyPath []string `json:"childKeyPath" yaml:"childkeyPath"`
ChildKeyPathValue any `json:"childKeyPathValue" yaml:"childkeyPathValue"`
ChildPermutations []Permutation `json:"childPermutations" yaml:"childPermutations"`
}

Expand All @@ -25,8 +23,6 @@ type Permutation struct {
func CreateRelationship(parentValue any, childKeyPath []string, childKeyPathValue any, childPermutations []Permutation) Relationship {
return Relationship{
ParentValue: parentValue,
ChildKeyPath: childKeyPath,
ChildKeyPathValue: childKeyPathValue,
ChildPermutations: childPermutations,
}
}
Expand All @@ -49,6 +45,7 @@ func Permute(permutations []Permutation, baseConfig map[string]any) ([]map[strin
return configs, err
}

//Apply the current permutation and all its relationships
for _, keyPathValue := range permutations[0].KeyPathValues {
permutedConfig, err := mapOperations.DeepCopyMap(baseConfig)
if err != nil {
Expand All @@ -60,38 +57,22 @@ func Permute(permutations []Permutation, baseConfig map[string]any) ([]map[strin
return nil, err
}

subPermutations := false
for _, relationship := range permutations[0].KeyPathValueRelationships {
if relationship.ParentValue == keyPathValue {
if len(relationship.ChildKeyPath) > 1 && relationship.ChildKeyPathValue != nil {
permutedConfig, err = mapOperations.ReplaceValue(relationship.ChildKeyPath, relationship.ChildKeyPathValue, permutedConfig)
if err != nil {
return nil, err
}
}

var relationshipPermutedConfigs []map[string]any
if len(relationship.ChildPermutations) > 0 {
subPermutations = true
relationshipPermutedConfigs, err = Permute(relationship.ChildPermutations, permutedConfig)
if err != nil {
return nil, err
}
}

configs = append(configs, relationshipPermutedConfigs...)
relationshipConfigs := []map[string]any{permutedConfig}
if len(permutations[0].KeyPathValueRelationships) != 0 && permutations[0].KeyPathValueRelationships != nil {
relationshipConfigs, err = applyRelationships(permutedConfig, permutations[0].KeyPathValueRelationships, keyPathValue)
if err != nil {
return nil, err
}
}

if !subPermutations {
configs = append(configs, permutedConfig)
}
configs = append(configs, relationshipConfigs...)
}

var finalConfigs []map[string]any
if len(permutations) == 1 {
return configs, nil
} else {
//Apply the rest of the permutations on top of the current permutation
for _, config := range configs {
permutedConfigs, err := Permute(permutations[1:], config)
if err != nil {
Expand All @@ -104,3 +85,37 @@ func Permute(permutations []Permutation, baseConfig map[string]any) ([]map[strin

return finalConfigs, err
}

// applyRelationships iterates over a list of relationships structs and applies them to a config
func applyRelationships(config map[string]any, relationships []Relationship, keyPathValue any) ([]map[string]any, error) {
var err error
var relationshipConfigs []map[string]any

permutedConfigs := []map[string]any{config}
if (len(relationships[0].ChildPermutations) > 0) && (relationships[0].ParentValue == keyPathValue) {
//Apply the first relationship's permutations
permutedConfigs, err = Permute(relationships[0].ChildPermutations, config)
if err != nil {
return nil, err
}
}

relationshipConfigs = append(relationshipConfigs, permutedConfigs...)

var finalConfigs []map[string]any
if len(relationships) == 1 {
return relationshipConfigs, nil
} else {
//Apply the rest of the relationships to all of the current configs
for _, config := range relationshipConfigs {
relationshipConfigs, err = applyRelationships(config, relationships[1:], keyPathValue)
if err != nil {
return nil, err
}
finalConfigs = append(finalConfigs, relationshipConfigs...)
}
}

return finalConfigs, nil

}

0 comments on commit cf6b338

Please sign in to comment.