Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transpose function - return empty list when value is empty #23299 #23321

Merged
merged 1 commit into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lang/funcs/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
},
})
Expand Down
4 changes: 2 additions & 2 deletions lang/funcs/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down