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

resource/aws_dynamodb_table_item: Correctly handle List and Map attribute JSON values #30712

Merged
merged 7 commits into from
Apr 13, 2023
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
3 changes: 3 additions & 0 deletions .changelog/30712.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_dynamodb_table_item: Would report spurious diffs when List and Map attributes were changed out-of-band
```
12 changes: 12 additions & 0 deletions internal/maps/maps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package maps

// ApplyToAll returns a new map containing the results of applying the function `f` to each element of the original map `m`.
func ApplyToAll[K comparable, T, U any](m map[K]T, f func(T) U) map[K]U {
n := make(map[K]U, len(m))

for k, v := range m {
n[k] = f(v)
}

return n
}
52 changes: 52 additions & 0 deletions internal/maps/maps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package maps

import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestApplyToAll(t *testing.T) {
t.Parallel()

type testCase struct {
input map[int]string
expected map[int]string
}
tests := map[string]testCase{
"three elements": {
input: map[int]string{
1: "one",
2: "two",
3: "3"},
expected: map[int]string{
1: "ONE",
2: "TWO",
3: "3"},
},
"one element": {
input: map[int]string{
123: "abcdEFGH"},
expected: map[int]string{
123: "ABCDEFGH"},
},
"zero elements": {
input: map[int]string{},
expected: map[int]string{},
},
}

for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
t.Parallel()

got := ApplyToAll(test.input, strings.ToUpper)

if diff := cmp.Diff(got, test.expected); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
})
}
}
68 changes: 47 additions & 21 deletions internal/service/dynamodb/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"

"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/hashicorp/terraform-provider-aws/internal/maps"
"github.com/hashicorp/terraform-provider-aws/internal/slices"
)

func ExpandTableItemAttributes(input string) (map[string]*dynamodb.AttributeValue, error) {
Expand All @@ -24,34 +26,58 @@ func ExpandTableItemAttributes(input string) (map[string]*dynamodb.AttributeValu
func flattenTableItemAttributes(attrs map[string]*dynamodb.AttributeValue) (string, error) {
buf := bytes.NewBufferString("")
encoder := json.NewEncoder(buf)
err := encoder.Encode(attrs)

a := make(map[string]attributeValue, len(attrs))
for k, v := range attrs {
a[k] = attributeValue(*v)
}
err := encoder.Encode(a)
if err != nil {
return "", fmt.Errorf("Encoding failed: %s", err)
}

var rawData map[string]map[string]interface{}
return buf.String(), nil
}

// Reserialize so we get rid of the nulls
decoder := json.NewDecoder(strings.NewReader(buf.String()))
err = decoder.Decode(&rawData)
if err != nil {
return "", fmt.Errorf("Decoding failed: %s", err)
}
type attributeValue dynamodb.AttributeValue

for _, value := range rawData {
for typeName, typeVal := range value {
if typeVal == nil {
delete(value, typeName)
}
}
}
func (f attributeValue) MarshalJSON() ([]byte, error) {
thing := map[string]any{}

rawBuffer := bytes.NewBufferString("")
rawEncoder := json.NewEncoder(rawBuffer)
err = rawEncoder.Encode(rawData)
if err != nil {
return "", fmt.Errorf("Re-encoding failed: %s", err)
if f.B != nil {
thing["B"] = f.B
}
if f.BOOL != nil {
thing["BOOL"] = f.BOOL
}
if f.BS != nil {
thing["BS"] = f.BS
}
if f.L != nil {
thing["L"] = slices.ApplyToAll(f.L, func(t *dynamodb.AttributeValue) attributeValue {
return attributeValue(*t)
})
}
if f.M != nil {
thing["M"] = maps.ApplyToAll(f.M, func(t *dynamodb.AttributeValue) attributeValue {
return attributeValue(*t)
})
}
if f.N != nil {
thing["N"] = f.N
}
if f.NS != nil {
thing["NS"] = f.NS
}
if f.NULL != nil {
thing["NULL"] = f.NULL
}
if f.S != nil {
thing["S"] = f.S
}
if f.SS != nil {
thing["SS"] = f.SS
}

return rawBuffer.String(), nil
return json.Marshal(thing)
}
Loading