Skip to content

Commit

Permalink
Refactor Overwriting Combinations in Matrix
Browse files Browse the repository at this point in the history
This commit refactors the overwriting combinations function within fanning out the Matrix to reduce the number of calls as requested by Priti in PR: tektoncd#6341.
  • Loading branch information
EmmaMunley committed Mar 29, 2023
1 parent 9ff6034 commit 3b8a6f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
19 changes: 7 additions & 12 deletions pkg/apis/pipeline/v1/matrix_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,26 @@ func (m *Matrix) FanOut() []Params {
for _, parameter := range m.Params {
combinations = combinations.fanOutMatrixParams(parameter)
}
combinations = combinations.overwriteCombinations(includeCombinations)
combinations.overwriteCombinations(includeCombinations)
combinations = combinations.addNewCombinations(includeCombinations)
return combinations.toParams()
}

// overwriteCombinations replaces any missing include params in the initial
// matrix params combinations by overwriting the initial combinations with the
// include combinations
func (cs Combinations) overwriteCombinations(ics Combinations) Combinations {
func (cs Combinations) overwriteCombinations(ics Combinations) {
for _, paramCombination := range cs {
for _, includeCombination := range ics {
if paramCombination.contains(includeCombination) {
includeCombination.overwrite(paramCombination)
// overwrite the parameter name and value in existing combination
// with the include combination
for name, val := range includeCombination {
paramCombination[name] = val
}
}
}
}
return cs
}

// addNewCombinations creates a new combination for any include parameter
Expand All @@ -115,14 +118,6 @@ func (c Combination) contains(includeCombination Combination) bool {
return true
}

// overwrite the parameter name and value exists in combination with the include combination
func (c Combination) overwrite(oldCombination Combination) Combination {
for name, val := range c {
oldCombination[name] = val
}
return oldCombination
}

// shouldAddNewCombination returns true if the include parameter name exists but the value is
// missing from combinations
func (cs Combinations) shouldAddNewCombination(includeCombination map[string]string) bool {
Expand Down
22 changes: 7 additions & 15 deletions pkg/apis/pipeline/v1beta1/matrix_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,23 @@ func (m *Matrix) FanOut() []Params {
for _, parameter := range m.Params {
combinations = combinations.fanOutMatrixParams(parameter)
}
combinations = combinations.overwriteCombinations(includeCombinations)
combinations.overwriteCombinations(includeCombinations)
combinations = combinations.addNewCombinations(includeCombinations)
return combinations.toParams()
}

// overwriteCombinations replaces any missing include params in the initial
// matrix params combinations by overwriting the initial combinations with the
// include combinations
func (cs Combinations) overwriteCombinations(ics Combinations) Combinations {
func (cs Combinations) overwriteCombinations(ics Combinations) {
for _, paramCombination := range cs {
for _, includeCombination := range ics {
if paramCombination.contains(includeCombination) {
includeCombination.overwrite(paramCombination)
// overwrite the parameter name and value in existing combination
// with the include combination
for name, val := range includeCombination {
paramCombination[name] = val
}
}
}
}
return cs
}

// addNewCombinations creates a new combination for any include parameter
Expand All @@ -115,14 +115,6 @@ func (c Combination) contains(includeCombination Combination) bool {
return true
}

// overwrite the parameter name and value exists in combination with the include combination
func (c Combination) overwrite(oldCombination Combination) Combination {
for name, val := range c {
oldCombination[name] = val
}
return oldCombination
}

// shouldAddNewCombination returns true if the include parameter name exists but the value is
// missing from combinations
func (cs Combinations) shouldAddNewCombination(includeCombination map[string]string) bool {
Expand Down

0 comments on commit 3b8a6f6

Please sign in to comment.