lang/funcs: don't panic when transpose produces empty result

pull/23323/head
Parviz 7 years ago committed by Martin Atkins
parent 9ff83d8308
commit 6eaf1c23f9

@ -1179,7 +1179,6 @@ func sliceIndexes(args []cty.Value) (int, int, bool, error) {
return startIndex, endIndex, startKnown && endKnown, nil
}
// TransposeFunc contructs a function that takes a map of lists of strings and
// TransposeFunc constructs a function that takes a map of lists of strings and
// swaps the keys and values to produce a new map of lists of strings.
var TransposeFunc = function.New(&function.Spec{
@ -1226,6 +1225,10 @@ var TransposeFunc = function.New(&function.Spec{
outputMap[outKey] = cty.ListVal(values)
}
if len(outputMap) == 0 {
return cty.MapValEmpty(cty.List(cty.String)), nil
}
return cty.MapVal(outputMap), nil
},
})

@ -2903,8 +2903,8 @@ func TestTranspose(t *testing.T) {
cty.MapVal(map[string]cty.Value{
"key1": cty.ListValEmpty(cty.String),
}),
cty.NilVal,
true,
cty.MapValEmpty(cty.List(cty.String)),
false,
},
{ // bad map - value not a list
cty.MapVal(map[string]cty.Value{

Loading…
Cancel
Save