Skip to content

Commit

Permalink
Add support for encoding and decoding attribute value union
Browse files Browse the repository at this point in the history
* Fixes aws#895
* Fixes aws#929
* Fixes aws#115 to encode `null` for nil map/slice unless `omitempty` is
  set. Simplifies the rules of `null` vs skipped vs zero value for all
  types.
  • Loading branch information
jasdel committed Dec 8, 2020
1 parent ff4fe28 commit 9d551f4
Show file tree
Hide file tree
Showing 14 changed files with 1,319 additions and 1,203 deletions.
89 changes: 89 additions & 0 deletions feature/dynamodb/attributevalue/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package attributevalue

import (
"fmt"

ddb "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
streams "github.com/aws/aws-sdk-go-v2/service/dynamodbstreams/types"
)

// FromDynamoDBStreamMap converts a map of Amazon DynamoDB Streams
// AttributeValues, and all nested members, to map of Amazon DynamoDB
// AttributeValues.
func FromDynamoDBStreamMap(from map[string]streams.AttributeValue) (to map[string]ddb.AttributeValue, err error) {
to = make(map[string]ddb.AttributeValue, len(from))
for field, value := range from {
to[field], err = FromDynamoDBStream(value)
if err != nil {
return nil, err
}
}

return to, nil
}

// FromDynamoDBStreamList converts a slice of Amazon DynamoDB Streams
// AttributeValues, and all nested members, to slice of Amazon DynamoDB
// AttributeValues.
func FromDynamoDBStreamList(from []streams.AttributeValue) (to []ddb.AttributeValue, err error) {
to = make([]ddb.AttributeValue, len(from))
for i := 0; i < len(from); i++ {
to[i], err = FromDynamoDBStream(from[i])
if err != nil {
return nil, err
}
}

return to, nil
}

// FromDynamoDBStream converts an Amazon DynamoDB Streams AttributeValue, and
// all nested members, to an Amazon DynamoDB AttributeValue.
func FromDynamoDBStream(from streams.AttributeValue) (ddb.AttributeValue, error) {
switch tv := from.(type) {
case *streams.AttributeValueMemberNULL:
return &ddb.AttributeValueMemberNULL{Value: tv.Value}, nil

case *streams.AttributeValueMemberBOOL:
return &ddb.AttributeValueMemberBOOL{Value: tv.Value}, nil

case *streams.AttributeValueMemberB:
return &ddb.AttributeValueMemberB{Value: tv.Value}, nil

case *streams.AttributeValueMemberBS:
bs := make([][]byte, len(tv.Value))
for i := 0; i < len(tv.Value); i++ {
bs[i] = append([]byte{}, tv.Value[i]...)
}
return &ddb.AttributeValueMemberBS{Value: bs}, nil

case *streams.AttributeValueMemberN:
return &ddb.AttributeValueMemberN{Value: tv.Value}, nil

case *streams.AttributeValueMemberNS:
return &ddb.AttributeValueMemberNS{Value: append([]string{}, tv.Value...)}, nil

case *streams.AttributeValueMemberS:
return &ddb.AttributeValueMemberS{Value: tv.Value}, nil

case *streams.AttributeValueMemberSS:
return &ddb.AttributeValueMemberSS{Value: append([]string{}, tv.Value...)}, nil

case *streams.AttributeValueMemberL:
values, err := FromDynamoDBStreamList(tv.Value)
if err != nil {
return nil, err
}
return &ddb.AttributeValueMemberL{Value: values}, nil

case *streams.AttributeValueMemberM:
values, err := FromDynamoDBStreamMap(tv.Value)
if err != nil {
return nil, err
}
return &ddb.AttributeValueMemberM{Value: values}, nil

default:
return nil, fmt.Errorf("unknown Amazon DynamoDB Streams AttributeValue union member, %T", from)
}
}
Loading

0 comments on commit 9d551f4

Please sign in to comment.