1
0
mirror of https://github.com/go-task/task.git synced 2025-06-15 00:15:10 +02:00

feat: loop over a matrix (#1767)

This commit is contained in:
Pete Davison
2024-09-02 20:29:00 +01:00
committed by GitHub
parent 1cb5daf73e
commit 281d259e6e
7 changed files with 145 additions and 18 deletions

View File

@ -271,9 +271,13 @@ func itemsFromFor(
) ([]any, []string, error) {
var keys []string // The list of keys to loop over (only if looping over a map)
var values []any // The list of values to loop over
// Get the list from a matrix
if f.Matrix != nil {
return asAnySlice(product(f.Matrix)), nil, nil
}
// Get the list from the explicit for list
if len(f.List) > 0 {
values = f.List
return f.List, nil, nil
}
// Get the list from the task sources
if f.From == "sources" {
@ -322,3 +326,46 @@ func itemsFromFor(
}
return values, keys, nil
}
// product generates the cartesian product of the input map of slices.
func product(inputMap map[string][]any) []map[string]any {
if len(inputMap) == 0 {
return nil
}
// Extract the keys and corresponding slices
keys := make([]string, 0, len(inputMap))
slices := make([][]any, 0, len(inputMap))
for key, slice := range inputMap {
keys = append(keys, key)
slices = append(slices, slice)
}
// Start with an empty product result
result := []map[string]any{{}}
// Iterate over each slice in the slices
for i, slice := range slices {
var newResult []map[string]any
// For each combination in the current result
for _, combination := range result {
// Append each element from the current slice to the combinations
for _, item := range slice {
newComb := make(map[string]any, len(combination))
// Copy the existing combination
for k, v := range combination {
newComb[k] = v
}
// Add the current item with the corresponding key
newComb[keys[i]] = item
newResult = append(newResult, newComb)
}
}
// Update result with the new combinations
result = newResult
}
return result
}