Skip to content

Commit

Permalink
[dynamodb] Add BatchGetItem function (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsoo authored Oct 5, 2020
1 parent 6e5368f commit 2b3fa8e
Show file tree
Hide file tree
Showing 3 changed files with 474 additions and 0 deletions.
82 changes: 82 additions & 0 deletions dynamodb/client_op_batch_get_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package dynamodb

import (
SDK "github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)

// BatchGetAll executes batch_get_item operation
func (svc *DynamoDB) BatchGetAll(in BatchGetAllRequest) (*BatchGetAllResponse, error) {
o, err := svc.client.BatchGetItem(in.ToInput())
if err != nil {
return nil, err
}
return newBatchGetItemResponse(o), nil
}

type BatchGetAllRequest struct {
RequestItems map[string]KeysAndAttributes
ReturnConsumedCapacity string
}

func (r *BatchGetAllRequest) ToInput() *SDK.BatchGetItemInput {
i := SDK.BatchGetItemInput{}
i.RequestItems = make(map[string]*SDK.KeysAndAttributes, len(r.RequestItems))
for key, item := range r.RequestItems {
i.RequestItems[key] = item.ToSDK()
}
if r.ReturnConsumedCapacity != "" {
i.ReturnConsumedCapacity = &r.ReturnConsumedCapacity
}
return &i
}

type BatchGetAllResponse struct {
Responses map[string][]map[string]*SDK.AttributeValue
UnprocessedKeys map[string]KeysAndAttributes
ConsumedCapacity []ConsumedCapacity
}

func newBatchGetItemResponse(output *SDK.BatchGetItemOutput) *BatchGetAllResponse {
r := &BatchGetAllResponse{}
if output == nil {
return r
}
r.ConsumedCapacity = newConsumedCapacities(output.ConsumedCapacity)
r.Responses = output.Responses

r.UnprocessedKeys = make(map[string]KeysAndAttributes, len(output.UnprocessedKeys))
for key, val := range output.UnprocessedKeys {
r.UnprocessedKeys[key] = newKeysAndAttributes(val)
}
return r
}

// UnmarshalItems unmarshals given slice pointer struct from DynamoDB item result to mapping.
// e.g. err = Unmarshal(&[]*yourStruct)
// The struct tag `dynamodb:""` is used to unmarshal.
func (r BatchGetAllResponse) Unmarshal(v interface{}) error {
return r.UnmarshalWithTagName(v, defaultResultTag)
}

// UnmarshalWithTagName unmarshals given slice pointer struct and tag name from DynamoDB item result to mapping.
func (r BatchGetAllResponse) UnmarshalWithTagName(v interface{}, structTag string) error {
decoder := dynamodbattribute.NewDecoder()
decoder.TagKey = structTag

itemsMap := make(map[string]*SDK.AttributeValue, len(r.Responses))
for tableName, i := range r.Responses {
items := make([]*SDK.AttributeValue, len(i))
for i, m := range i {
items[i] = &SDK.AttributeValue{M: m}
}
itemsMap[tableName] = &SDK.AttributeValue{
L: items,
}
}
val := &SDK.AttributeValue{M: itemsMap}
if err := decoder.Decode(val, v); err != nil {
return err
}
return nil
}
73 changes: 73 additions & 0 deletions dynamodb/table_op_batch_get_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package dynamodb

import (
SDK "github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)

// BatchGet executes batch_get_item operation
func (t *Table) BatchGet(req BatchGetRequest) (*BatchGetResponse, error) {
res, err := t.service.client.BatchGetItem(req.ToInput(t.nameWithPrefix))
if err != nil {
return nil, err
}
return newBatchGetResponse(res, t.nameWithPrefix), nil
}

type BatchGetRequest struct {
RequestItems KeysAndAttributes
ReturnConsumedCapacity string
}

func (r *BatchGetRequest) ToInput(tableName string) *SDK.BatchGetItemInput {
i := SDK.BatchGetItemInput{}
i.RequestItems = make(map[string]*SDK.KeysAndAttributes)
i.RequestItems[tableName] = r.RequestItems.ToSDK()
if r.ReturnConsumedCapacity != "" {
i.ReturnConsumedCapacity = &r.ReturnConsumedCapacity
}
return &i
}

type BatchGetResponse struct {
Responses []map[string]*SDK.AttributeValue
UnprocessedKeys KeysAndAttributes
ConsumedCapacity []ConsumedCapacity
}

func newBatchGetResponse(output *SDK.BatchGetItemOutput, tableName string) *BatchGetResponse {
r := &BatchGetResponse{}
if output == nil {
return r
}
if res, ok := output.Responses[tableName]; ok {
r.Responses = res
}
if keys, ok := output.UnprocessedKeys[tableName]; ok {
r.UnprocessedKeys = newKeysAndAttributes(keys)
}
r.ConsumedCapacity = newConsumedCapacities(output.ConsumedCapacity)
return r
}

func (r BatchGetResponse) Unmarshal(v interface{}) error {
return r.UnmarshalWithTagName(v, defaultResultTag)
}

// UnmarshalWithTagName unmarshals given slice pointer sturct and tag name from DynamoDB item result to mapping.
func (r BatchGetResponse) UnmarshalWithTagName(v interface{}, structTag string) error {
decoder := dynamodbattribute.NewDecoder()
decoder.TagKey = structTag

items := make([]*SDK.AttributeValue, len(r.Responses))
for i, m := range r.Responses {
items[i] = &SDK.AttributeValue{M: m}
}
val := &SDK.AttributeValue{
L: items,
}
if err := decoder.Decode(val, v); err != nil {
return err
}
return nil
}
Loading

0 comments on commit 2b3fa8e

Please sign in to comment.