-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkv.go
64 lines (52 loc) · 1.8 KB
/
kv.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package dynastore
import (
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
var (
reservedFields = map[string]string{"id": "S", "name": "S", "version": "N", "expires": "N", "payload": "A"}
)
// KVPairPage provides a page of keys with next token
// to enable paging
type KVPairPage struct {
Keys []*KVPair `json:"keys"`
LastKey string `json:"last_key"`
}
// KVPair represents {Key, Value, Version} tuple, internally
// this uses a *dynamodb.AttributeValue which can be used to
// store strings, slices or structs
type KVPair struct {
Partition string `dynamodbav:"id"`
Key string `dynamodbav:"name"`
Version int64 `dynamodbav:"version"`
Expires int64 `dynamodbav:"expires"`
// handled separately to enable an number of stored values
value *dynamodb.AttributeValue
fields map[string]*dynamodb.AttributeValue
}
// BytesValue use the attribute to return a slice of bytes, a nil will be returned if it is empty or nil
func (kv *KVPair) BytesValue() []byte {
var buf []byte
err := dynamodbattribute.Unmarshal(kv.value, &buf)
if err != nil {
return nil
}
return buf
}
// StringValue use the attribute to return a slice of bytes, an empty string will be returned if it is empty or nil
func (kv *KVPair) StringValue() string {
var str string
err := dynamodbattribute.Unmarshal(kv.value, &str)
if err != nil {
return str
}
return str
}
// DecodeValue decode using dynamodbattribute
func (kv *KVPair) DecodeValue(out interface{}) error {
return dynamodbattribute.Unmarshal(kv.value, out)
}
// DecodeFields decode the extra fields, which are typically index attributes, stored in the DynamoDB record using dynamodbattribute
func (kv *KVPair) DecodeFields(out interface{}) error {
return dynamodbattribute.UnmarshalMap(kv.fields, out)
}