-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathquery_result.go
46 lines (39 loc) · 1.49 KB
/
query_result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package dynamodb
import (
SDK "github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
const defaultResultTag = "dynamodb"
// QueryResult is struct for result of Query operation.
type QueryResult struct {
Items []map[string]*SDK.AttributeValue
LastEvaluatedKey map[string]*SDK.AttributeValue
Count int64
ScannedCount int64
}
// ToSliceMap converts result to slice of map.
func (r QueryResult) ToSliceMap() []map[string]interface{} {
m := make([]map[string]interface{}, len(r.Items))
for i, item := range r.Items {
// benachmark: https://gist.github.com/evalphobia/c1b436ef15038bc9fc9c588ca0163c93#gistcomment-3120916
m[i] = UnmarshalAttributeValue(item)
}
return m
}
// Unmarshal unmarshals given slice pointer sturct from DynamoDB item result to mapping.
// e.g. err = Unmarshal(&[]*yourStruct)
// The struct tag `dynamodb:""` is used to unmarshal.
func (r QueryResult) 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 QueryResult) UnmarshalWithTagName(v interface{}, structTag string) error {
decoder := dynamodbattribute.NewDecoder()
decoder.TagKey = structTag
items := make([]*SDK.AttributeValue, len(r.Items))
for i, m := range r.Items {
items[i] = &SDK.AttributeValue{M: m}
}
err := decoder.Decode(&SDK.AttributeValue{L: items}, v)
return err
}