/
dynamodb.go
161 lines (130 loc) · 3.67 KB
/
dynamodb.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package dynamodb
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
)
const (
defaultRegion string = "ap-southeast-1"
defaultTableName string = "sample-table"
keyNameString string = "Key"
)
type serviceImpl struct {
tableName string
db dynamodbiface.DynamoDBAPI
}
// Config provides abstraction over DynamoDB service configuration.
// It controls the following parameters:
// * Region - AWS region. If not specified in ~/.aws/config file
// the region is set to `defaultRegion` constant
// * TableName - Name of DynamoDB table. If not set explicitly, the
// service will be initialized with value set in the
// `defaultTableName` constant
// * Endpoint - Only applicable for testing using a local instance
// of DynamoDB e.g. https://dynamodb:8000. Leave blank
// to operate on a table in AWS.
type Config struct {
Region string
TableName string
Endpoint string
}
// Item provides abstraction over DynamoDB item (entry).
// * Key - Represents index of the item
// * Value - Represents value assigned to the index in `Key`
type Item struct {
Key string
Value string
}
// Service interface provides CRUD abstraction over DynamoDB table
type Service interface {
GetItem(key string) (*Item, error)
PutItem(key string, value string) error
DeleteItem(key string) error
}
func initContext(config Config) *dynamodb.DynamoDB {
awsConfig := aws.Config{}
if len(config.Region) > 0 {
awsConfig.Region = aws.String(config.Region)
} else {
awsConfig.Region = aws.String(defaultRegion)
}
if len(config.Region) > 0 {
awsConfig.Endpoint = aws.String(config.Endpoint)
}
sesh := session.Must(session.NewSession(&awsConfig))
return dynamodb.New(sesh)
}
// InitService initializes the service with parameters supplied
// in the `config` parameter
func InitService(config Config) Service {
tabName := config.TableName
if len(tabName) <= 0 {
tabName = defaultTableName
}
context := serviceImpl{
tableName: tabName,
db: initContext(config),
}
return context
}
func (service serviceImpl) GetItem(key string) (*Item, error) {
result, err := service.db.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(service.tableName),
Key: map[string]*dynamodb.AttributeValue{
keyNameString: {
S: aws.String(key),
},
},
})
if err != nil {
return nil, err
}
if result.Item == nil {
log.Printf("Couldn't find item with key: %s", key)
return nil, nil
}
item := Item{}
err = dynamodbattribute.UnmarshalMap(result.Item, &item)
if err != nil {
panic(fmt.Sprintf("Failed to unmarshal Record, %v", err))
}
return &item, err
}
func (service serviceImpl) PutItem(key string, value string) error {
item := Item{
Key: key,
Value: value,
}
marshalledItem, err := dynamodbattribute.MarshalMap(item)
if err != nil {
panic(fmt.Sprintf("Failed to marshal Record, %v", err))
}
_, err = service.db.PutItem(&dynamodb.PutItemInput{
TableName: aws.String(service.tableName),
Item: marshalledItem,
})
if err != nil {
return err
}
log.Printf("Successfully put item with key: %s", key)
return nil
}
func (service serviceImpl) DeleteItem(key string) error {
_, err := service.db.DeleteItem(&dynamodb.DeleteItemInput{
TableName: aws.String(service.tableName),
Key: map[string]*dynamodb.AttributeValue{
keyNameString: {
S: aws.String(key),
},
},
})
if err != nil {
return err
}
log.Printf("Successfully deleted item with key: %s", key)
return nil
}