-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
matrix_types.go
82 lines (71 loc) · 2.79 KB
/
matrix_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Copyright 2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package matrix
import (
"strconv"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
)
// Combinations is a slice of combinations of Parameters from a Matrix.
type Combinations []*Combination
// Combination is a specific combination of Parameters from a Matrix.
type Combination struct {
// MatrixID is an identification of a combination from Parameters in a Matrix.
MatrixID string
// Params is a specific combination of Parameters in a Matrix.
Params []v1beta1.Param
}
func (combinations Combinations) fanOut(param v1beta1.Param) Combinations {
if len(combinations) == 0 {
return initializeCombinations(param)
}
return combinations.distribute(param)
}
func (combinations Combinations) distribute(param v1beta1.Param) Combinations {
// when there are existing combinations, this is a non-first parameter in the matrix, and we need to distribute
// it among the existing combinations
var expandedCombinations Combinations
var count int
for _, value := range param.Value.ArrayVal {
for _, combination := range combinations {
expandedCombinations = append(expandedCombinations, createCombination(count, param.Name, value, combination.Params))
count++
}
}
return expandedCombinations
}
func initializeCombinations(param v1beta1.Param) Combinations {
// when there are no existing combinations, this is the first parameter in the matrix, so we initialize the
// combinations with the first Parameter
var combinations Combinations
for i, value := range param.Value.ArrayVal {
combinations = append(combinations, createCombination(i, param.Name, value, []v1beta1.Param{}))
}
return combinations
}
func createCombination(i int, name string, value string, parameters []v1beta1.Param) *Combination {
return &Combination{
MatrixID: strconv.Itoa(i),
Params: append(parameters, v1beta1.Param{
Name: name,
Value: v1beta1.ParamValue{Type: v1beta1.ParamTypeString, StringVal: value},
}),
}
}
// ToMap converts a list of Combinations to a map where the key is the matrixId and the values are Parameters.
func (combinations Combinations) ToMap() map[string][]v1beta1.Param {
m := map[string][]v1beta1.Param{}
for _, combination := range combinations {
m[combination.MatrixID] = combination.Params
}
return m
}