forked from aws/aws-sdk-go-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for encoding and decoding attribute value union
* 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
Showing
14 changed files
with
1,319 additions
and
1,203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.