Skip to content

Commit

Permalink
config: Remove "setproduct" function, which is now in lang/funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
apparentlymart committed Jan 16, 2019
1 parent da51e72 commit 4e14ab7
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 105 deletions.
53 changes: 0 additions & 53 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ func Funcs() map[string]ast.Function {
"min": interpolationFuncMin(),
"pathexpand": interpolationFuncPathExpand(),
"pow": interpolationFuncPow(),
"setproduct": interpolationFuncSetProduct(),
"uuid": interpolationFuncUUID(),
"replace": interpolationFuncReplace(),
"rsadecrypt": interpolationFuncRsaDecrypt(),
Expand Down Expand Up @@ -1740,55 +1739,3 @@ func interpolationFuncRsaDecrypt() ast.Function {
},
}
}

// interpolationFuncSetProduct implements the "setproduct" function
// that returns the cartesian product of two or more lists or sets
func interpolationFuncSetProduct() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeList},
ReturnType: ast.TypeList,
Variadic: true,
VariadicType: ast.TypeList,
Callback: func(args []interface{}) (interface{}, error) {
if len(args) < 2 {
return nil, fmt.Errorf("must provide at least two arguments")
}

total := 1
for _, arg := range args {
total *= len(arg.([]ast.Variable))
}

if total == 0 {
return nil, fmt.Errorf("empty list provided")
}

product := make([][]ast.Variable, total)

b := make([]ast.Variable, total*len(args))
n := make([]int, len(args))
s := 0

for i := range product {
e := s + len(args)
pi := b[s:e]
product[i] = pi
s = e

for j, n := range n {
pi[j] = args[j].([]ast.Variable)[n]
}

for j := len(n) - 1; j >= 0; j-- {
n[j]++
if n[j] < len(args[j].([]ast.Variable)) {
break
}
n[j] = 0
}
}

return listVariableSliceToVariableValue(product), nil
},
}
}
52 changes: 0 additions & 52 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2960,55 +2960,3 @@ H7CurtMwALQ/n/6LUKFmjRZjqbKX9SO2QSaC3grd6sY9Tu+bZjLe
},
})
}

func TestInterpolateFuncSetProduct(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${setproduct(list("dev", "qas", "prd"), list("applicationA", "applicationB", "applicationC"))}`,
[]interface{}{
[]interface{}{"dev", "applicationA"},
[]interface{}{"dev", "applicationB"},
[]interface{}{"dev", "applicationC"},
[]interface{}{"qas", "applicationA"},
[]interface{}{"qas", "applicationB"},
[]interface{}{"qas", "applicationC"},
[]interface{}{"prd", "applicationA"},
[]interface{}{"prd", "applicationB"},
[]interface{}{"prd", "applicationC"}},
false,
},
{
`${setproduct(list("A", "B"), list("C", "D"), list("E", "F"))}`,
[]interface{}{
[]interface{}{"A", "C", "E"},
[]interface{}{"A", "C", "F"},
[]interface{}{"A", "D", "E"},
[]interface{}{"A", "D", "F"},
[]interface{}{"B", "C", "E"},
[]interface{}{"B", "C", "F"},
[]interface{}{"B", "D", "E"},
[]interface{}{"B", "D", "F"},
},
false,
},
{
`${setproduct(list(), list(), list())}`,
nil,
true,
},
{
`${setproduct(list("foo"),list("bar"))}`,
[]interface{}{
[]interface{}{"foo", "bar"},
},
false,
},
{
`${setproduct(list("foo"))}`,
nil,
true,
},
},
})
}

0 comments on commit 4e14ab7

Please sign in to comment.