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

internal/keyvaluetags: Allow KeyValueTags type passthrough in New(), add unit testing, and fix potential panic #19011

Merged
merged 2 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 15 additions & 12 deletions aws/internal/keyvaluetags/key_value_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,20 +491,18 @@ func (tags KeyValueTags) UrlEncode() string {
return values.Encode()
}

// New creates KeyValueTags from common Terraform Provider SDK types.
// Supports map[string]string, map[string]*string, map[string]interface{}, and []interface{}.
// New creates KeyValueTags from common types or returns an empty KeyValueTags.
//
// Supports various Terraform Plugin SDK types including map[string]string,
// map[string]*string, map[string]interface{}, and []interface{}.
// When passed []interface{}, all elements are treated as keys and assigned nil values.
// When passed KeyValueTags or its underlying type implementation, returns itself.
func New(i interface{}) KeyValueTags {
switch value := i.(type) {
case KeyValueTags:
return value
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
case map[string]*TagData:
kvtm := make(KeyValueTags, len(value))

for k, v := range value {
tagData := v
kvtm[k] = tagData
}

return kvtm
return KeyValueTags(value)
case map[string]string:
kvtm := make(KeyValueTags, len(value))

Expand Down Expand Up @@ -533,8 +531,13 @@ func New(i interface{}) KeyValueTags {
kvtm := make(KeyValueTags, len(value))

for k, v := range value {
str := v.(string)
kvtm[k] = &TagData{Value: &str}
kvtm[k] = &TagData{}

str, ok := v.(string)

if ok {
kvtm[k].Value = &str
}
}

return kvtm
Expand Down
133 changes: 133 additions & 0 deletions aws/internal/keyvaluetags/key_value_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2011,6 +2011,139 @@ func TestKeyValueTagsUrlEncode(t *testing.T) {
}
}

func TestNew(t *testing.T) {
testCases := []struct {
name string
source interface{}
want map[string]string
}{
{
name: "empty_KeyValueTags",
source: KeyValueTags{},
want: map[string]string{},
},
{
name: "empty_map_string_TagDataPointer",
source: map[string]*TagData{},
want: map[string]string{},
},
{
name: "empty_map_string_interface",
source: map[string]interface{}{},
want: map[string]string{},
},
{
name: "empty_map_string_string",
source: map[string]string{},
want: map[string]string{},
},
{
name: "empty_map_string_stringPointer",
source: map[string]*string{},
want: map[string]string{},
},
{
name: "empty_slice_interface",
source: []interface{}{},
want: map[string]string{},
},
{
name: "non_empty_KeyValueTags",
source: KeyValueTags{
"key1": &TagData{
Value: nil,
},
"key2": &TagData{
Value: testStringPtr(""),
},
"key3": &TagData{
Value: testStringPtr("value3"),
},
},
want: map[string]string{
"key1": "",
"key2": "",
"key3": "value3",
},
},
{
name: "non_empty_map_string_TagDataPointer",
source: map[string]*TagData{
"key1": {
Value: nil,
},
"key2": {
Value: testStringPtr(""),
},
"key3": {
Value: testStringPtr("value3"),
},
},
want: map[string]string{
"key1": "",
"key2": "",
"key3": "value3",
},
},
{
name: "non_empty_map_string_interface",
source: map[string]interface{}{
"key1": nil,
"key2": "",
"key3": "value3",
},
want: map[string]string{
"key1": "",
"key2": "",
"key3": "value3",
},
},
{
name: "non_empty_map_string_string",
source: map[string]string{
"key1": "",
"key2": "value2",
},
want: map[string]string{
"key1": "",
"key2": "value2",
},
},
{
name: "non_empty_map_string_stringPointer",
source: map[string]*string{
"key1": nil,
"key2": testStringPtr(""),
"key3": testStringPtr("value3"),
},
want: map[string]string{
"key1": "",
"key2": "",
"key3": "value3",
},
},
{
name: "non_empty_slice_interface",
source: []interface{}{
"key1",
"key2",
},
want: map[string]string{
"key1": "",
"key2": "",
},
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
got := New(testCase.source).Map()

testKeyValueTagsVerifyMap(t, got, testCase.want)
})
}
}

func TestTagDataEqual(t *testing.T) {
testCases := []struct {
name string
Expand Down