diff --git a/lang/funcs/collection.go b/lang/funcs/collection.go index 50b52b3cf..2ea416875 100644 --- a/lang/funcs/collection.go +++ b/lang/funcs/collection.go @@ -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 }, }) diff --git a/lang/funcs/collection_test.go b/lang/funcs/collection_test.go index cec18c049..58fa4beff 100644 --- a/lang/funcs/collection_test.go +++ b/lang/funcs/collection_test.go @@ -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{