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

This commit is contained in:
Parviz 2019-11-09 00:40:39 +07:00 committed by Martin Atkins
parent 9ff83d8308
commit 6eaf1c23f9
2 changed files with 6 additions and 3 deletions

View File

@ -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
},
})

View File

@ -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{