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

dynamodb/attributevalue: Add utility to (un)marshal Attribtue Values #948

Merged
merged 9 commits into from
Dec 15, 2020
Prev Previous commit
Next Next commit
Add support for encoding and decoding attribute value union
Adds support for

* Fixes #895 - Add support for attribute value marshaling
* Fixes #929 - Support Number type aliases of string for support for
  json.Number like types.
* Fixes #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 14, 2020
commit 30d0af0f9a052c8d6c2b2794c248717f3a89b0c8
87 changes: 87 additions & 0 deletions feature/dynamodb/attributevalue/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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"
)

// FromDynamoDBStreamsMap converts a map of Amazon DynamoDB Streams
// AttributeValues, and all nested members.
func FromDynamoDBStreamsMap(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 = FromDynamoDBStreams(value)
if err != nil {
return nil, err
}
}

return to, nil
}

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

return to, nil
}

// FromDynamoDBStreams converts an Amazon DynamoDB Streams AttributeValue, and
// all nested members.
func FromDynamoDBStreams(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 := FromDynamoDBStreamsList(tv.Value)
if err != nil {
return nil, err
}
return &ddb.AttributeValueMemberL{Value: values}, nil

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

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