-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dynamodb] Add BatchGetItem function (#71)
- Loading branch information
Showing
3 changed files
with
474 additions
and
0 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,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 | ||
} |
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,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 | ||
} |
Oops, something went wrong.