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
Adds support for

* Fixes aws#895 - Add support for attribute value marshaling
* Fixes aws#929 - Support Number type aliases of string for support for
  json.Number like types.
* Fixes aws#115 - Simplifies the rules of `null` vs skipped vs zero value
  for all types.
  * All nil pointers, map, slice members are serialized as NULL.
  * omitempty struct tag skips zero value of members with the struct
    tag.
  * Empty and Nil Sets (NS, BS, SS) are serialized to null
    by default unless `NullEmptySets` EncoderOptions is set to false. True
    by default. Nil sets are always serialized as NULL, unless the
    `omitempty` struct tag is used.
  * Adds `nullempty` and `nullemptyelem` struct tags to direct if the
    encoder should marshal the member as a AttributeValue NULL if the
    member's value is the type's zero value, (e.g. "" for string, 0 for
    number, nil for pointer/map/slice, false for bool)
  • Loading branch information
jasdel committed Dec 9, 2020
1 parent ff4fe28 commit 75ae24a
Show file tree
Hide file tree
Showing 15 changed files with 1,736 additions and 1,244 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 75ae24a

Please sign in to comment.