Skip to content

Commit

Permalink
service/dynamodb/dynamodbattribute: Decode AttributeValue types as de…
Browse files Browse the repository at this point in the history
…fined (#3308)
  • Loading branch information
skmcgrail committed May 8, 2020
1 parent c4ad4ab commit 33b6b7e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Expand Up @@ -3,3 +3,4 @@
### SDK Enhancements

### SDK Bugs
* `service/dynamodb/dynamodbattribute`: Simplified decode logic to decode AttributeValue as it is defined ([#3308](https://github.com/aws/aws-sdk-go/pull/3308))
12 changes: 6 additions & 6 deletions service/dynamodb/dynamodbattribute/decode.go
Expand Up @@ -174,23 +174,23 @@ func (d *Decoder) decode(av *dynamodb.AttributeValue, v reflect.Value, fieldTag
}

switch {
case len(av.B) != 0 || (av.B != nil && d.EnableEmptyCollections):
case av.B != nil:
return d.decodeBinary(av.B, v)
case av.BOOL != nil:
return d.decodeBool(av.BOOL, v)
case len(av.BS) != 0 || (av.BS != nil && d.EnableEmptyCollections):
case av.BS != nil:
return d.decodeBinarySet(av.BS, v)
case len(av.L) != 0 || (av.L != nil && d.EnableEmptyCollections):
case av.L != nil:
return d.decodeList(av.L, v)
case len(av.M) != 0 || (av.M != nil && d.EnableEmptyCollections):
case av.M != nil:
return d.decodeMap(av.M, v)
case av.N != nil:
return d.decodeNumber(av.N, v, fieldTag)
case len(av.NS) != 0 || (av.NS != nil && d.EnableEmptyCollections):
case av.NS != nil:
return d.decodeNumberSet(av.NS, v)
case av.S != nil: // DynamoDB does not allow for empty strings, so we do not consider the length or EnableEmptyCollections flag here
return d.decodeString(av.S, v, fieldTag)
case len(av.SS) != 0 || (av.SS != nil && d.EnableEmptyCollections):
case av.SS != nil:
return d.decodeStringSet(av.SS, v)
}

Expand Down
43 changes: 43 additions & 0 deletions service/dynamodb/dynamodbattribute/decode_test.go
Expand Up @@ -170,6 +170,49 @@ func TestUnmarshal(t *testing.T) {
Type: reflect.TypeOf(uint8(0)),
},
},
// -------
// Empty Values
// -------
{
in: &dynamodb.AttributeValue{B: []byte{}},
actual: &[]byte{},
expected: []byte{},
},
{
in: &dynamodb.AttributeValue{BS: [][]byte{}},
actual: &[][]byte{},
expected: [][]byte{},
},
{
in: &dynamodb.AttributeValue{L: []*dynamodb.AttributeValue{}},
actual: &[]interface{}{},
expected: []interface{}{},
},
{
in: &dynamodb.AttributeValue{M: map[string]*dynamodb.AttributeValue{}},
actual: &map[string]interface{}{},
expected: map[string]interface{}{},
},
{
in: &dynamodb.AttributeValue{N: aws.String("")},
actual: new(int),
err: fmt.Errorf("invalid syntax"),
},
{
in: &dynamodb.AttributeValue{NS: []*string{}},
actual: &[]*string{},
expected: []*string{},
},
{
in: &dynamodb.AttributeValue{S: aws.String("")},
actual: new(string),
expected: "",
},
{
in: &dynamodb.AttributeValue{SS: []*string{}},
actual: &[]*string{},
expected: []*string{},
},
}

for i, c := range cases {
Expand Down
3 changes: 2 additions & 1 deletion service/dynamodb/dynamodbattribute/shared_test.go
Expand Up @@ -2,6 +2,7 @@ package dynamodbattribute

import (
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -383,7 +384,7 @@ func assertConvertTest(t *testing.T, i int, actual, expected interface{}, err, e
i++
if expectedErr != nil {
if err != nil {
if e, a := expectedErr, err; !reflect.DeepEqual(e, a) {
if e, a := expectedErr, err; !strings.Contains(a.Error(), e.Error()) {
t.Errorf("case %d expect %v, got %v", i, e, a)
}
} else {
Expand Down

0 comments on commit 33b6b7e

Please sign in to comment.