Switch to using the dep tool and update all the dependencies

This commit is contained in:
Nick Craig-Wood 2017-05-11 15:39:54 +01:00
parent 5135ff73cb
commit 98c2d2c41b
5321 changed files with 4483201 additions and 5922 deletions

View file

@ -0,0 +1,47 @@
# Example
`scanItems` is an example how to use Amazon DynamoDB's Scan API operation with the SDK's `dynamodbattributes.UnmarshalListOfMaps` to unmarshal the Scan response's `Items` `[]map[string]*dynamodb.AttributeValue` field. This unmarshaler can be used with all `[]map[string]*dynamodb.AttributeValue` type fields.
## Go Type
The `Item` time will be used by the example to unmarshal the DynamoDB table's items to.
```go
type Item struct {
Key int
Desc string
Data map[string]interface{}
}
```
Use Go tags to define what the name is of the attribute in your DynamoDB table. See [AWS SDK for Go API Reference: Marshal](https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/dynamodbattribute/#Marshal) for more information.
In DynamoDB the structure of the item to be returned will be:
```json
{
"Data": {
"Value 1": "abc",
"Value 2": 1234567890
},
"Desc": "First ddb item",
"Key": 1
}
```
## Usage
`go run -tags example scanItems.go -table "<table_name>" -region "<optional_region>"`
## Output
```
0: Key: 123, Desc: An item in the DynamoDB table
Num Data Values: 0
1: Key: 2, Desc: Second ddb item
Num Data Values: 2
- "A Field": 123
- "Another Field": abc
2: Key: 1, Desc: First ddb item
Num Data Values: 2
- "Value 1": abc
- "Value 2": 1234567890
```

View file

@ -0,0 +1,98 @@
// +build example
package main
import (
"flag"
"fmt"
"os"
"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"
)
func exitWithError(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
func main() {
cfg := Config{}
if err := cfg.Load(); err != nil {
exitWithError(fmt.Errorf("failed to load config, %v", err))
}
// Create the config specifying the Region for the DynamoDB table.
// If Config.Region is not set the region must come from the shared
// config or AWS_REGION environment variable.
awscfg := &aws.Config{}
if len(cfg.Region) > 0 {
awscfg.WithRegion(cfg.Region)
}
// Create the session that the DynamoDB service will use.
sess := session.Must(session.NewSession(awscfg))
// Create the DynamoDB service client to make the query request with.
svc := dynamodb.New(sess)
// Build the query input parameters
params := &dynamodb.ScanInput{
TableName: aws.String(cfg.Table),
}
if cfg.Limit > 0 {
params.Limit = aws.Int64(cfg.Limit)
}
// Make the DynamoDB Query API call
result, err := svc.Scan(params)
if err != nil {
exitWithError(fmt.Errorf("failed to make Query API call, %v", err))
}
items := []Item{}
// Unmarshal the Items field in the result value to the Item Go type.
err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &items)
if err != nil {
exitWithError(fmt.Errorf("failed to unmarshal Query result items, %v", err))
}
// Print out the items returned
for i, item := range items {
fmt.Printf("%d: Key: %d, Desc: %s\n", i, item.Key, item.Desc)
fmt.Printf("\tNum Data Values: %d\n", len(item.Data))
for k, v := range item.Data {
fmt.Printf("\t- %q: %v\n", k, v)
}
}
}
type Item struct {
Key int
Desc string
Data map[string]interface{}
}
type Config struct {
Table string // required
Region string // optional
Limit int64 // optional
}
func (c *Config) Load() error {
flag.Int64Var(&c.Limit, "limit", 0, "Limit is the max items to be returned, 0 is no limit")
flag.StringVar(&c.Table, "table", "", "Table to Query on")
flag.StringVar(&c.Region, "region", "", "AWS Region the table is in")
flag.Parse()
if len(c.Table) == 0 {
flag.PrintDefaults()
return fmt.Errorf("table name is required.")
}
return nil
}

View file

@ -0,0 +1,52 @@
# Example
You can instantiate `*dynamodb.DynamoDB` and pass that as a parameter to all
methods connecting to DynamoDB, or as `unitTest` demonstrates, create your own
`type` and pass it along as a field.
## Test-compatible DynamoDB field
If you use `*dynamodb.DynamoDB` as a field, you will be unable to unit test it,
as documented in #88. Cast it instead as `dynamodbiface.DynamoDBAPI`:
```go
type ItemGetter struct {
DynamoDB dynamodbiface.DynamoDBAPI
}
```
## Querying actual DynamoDB
You'll need an `*aws.Config` and `*session.Session` for these to work correctly:
```go
// Setup
var getter = new(ItemGetter)
var config *aws.Config = &aws.Config{Region: aws.String("us-west-2"),}
var sess *session.Session = session.NewSession(config)
var svc *dynamodb.DynamoDB = dynamodb.New()
getter.DynamoDB = dynamodbiface.DynamoDBAPI(svc)
// Finally
getter.DynamoDB.GetItem(/* ... */)
```
## Querying in tests
Construct a `fakeDynamoDB` and add the necessary methods for each of those
structs (custom ones for `ItemGetter` and [whatever methods you're using for
DynamoDB](https://github.com/aws/aws-sdk-go/blob/master/service/dynamodb/dynamodbiface/interface.go)),
and you're good to go!
```go
type fakeDynamoDB struct {
dynamodbiface.DynamoDBAPI
}
var getter = new(ItemGetter)
getter.DynamoDB = &fakeDynamoDB{}
// And to run it (assuming you've mocked fakeDynamoDB.GetItem)
getter.DynamoDB.GetItem(/* ... */)
```
## Output
```
$ go test -tags example -cover
PASS
coverage: 100.0% of statements
ok _/Users/shatil/workspace/aws-sdk-go/example/service/dynamodb/unitTest 0.008s
```

View file

@ -0,0 +1,41 @@
// +build example
// Package unitTest demonstrates how to unit test, without needing to pass a
// connector to every function, code that uses DynamoDB.
package unitTest
import (
"github.com/aws/aws-sdk-go/aws"
"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"
)
// ItemGetter can be assigned a DynamoDB connector like:
// svc := dynamodb.DynamoDB(sess)
// getter.DynamoDB = dynamodbiface.DynamoDBAPI(svc)
type ItemGetter struct {
DynamoDB dynamodbiface.DynamoDBAPI
}
// Get a value from a DynamoDB table containing entries like:
// {"id": "my primary key", "value": "valuable value"}
func (ig *ItemGetter) Get(id string) (value string) {
var input = &dynamodb.GetItemInput{
Key: map[string]*dynamodb.AttributeValue{
"id": {
S: aws.String(id),
},
},
TableName: aws.String("my_table"),
AttributesToGet: []*string{
aws.String("value"),
},
}
if output, err := ig.DynamoDB.GetItem(input); err == nil {
if _, ok := output.Item["value"]; ok {
dynamodbattribute.Unmarshal(output.Item["value"], &value)
}
}
return
}

View file

@ -0,0 +1,59 @@
// +build example
// Unit tests for package unitTest.
package unitTest
import (
"errors"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
)
// A fakeDynamoDB instance. During testing, instatiate ItemGetter, then simply
// assign an instance of fakeDynamoDB to it.
type fakeDynamoDB struct {
dynamodbiface.DynamoDBAPI
payload map[string]string // Store expected return values
err error
}
// Mock GetItem such that the output returned carries values identical to input.
func (fd *fakeDynamoDB) GetItem(input *dynamodb.GetItemInput) (*dynamodb.GetItemOutput, error) {
output := new(dynamodb.GetItemOutput)
output.Item = make(map[string]*dynamodb.AttributeValue)
for key, value := range fd.payload {
output.Item[key] = &dynamodb.AttributeValue{
S: aws.String(value),
}
}
return output, fd.err
}
func TestItemGetterGet(t *testing.T) {
expectedKey := "expected key"
expectedValue := "expected value"
getter := new(ItemGetter)
getter.DynamoDB = &fakeDynamoDB{
payload: map[string]string{"id": expectedKey, "value": expectedValue},
}
if actualValue := getter.Get(expectedKey); actualValue != expectedValue {
t.Errorf("Expected %q but got %q", expectedValue, actualValue)
}
}
// When DynamoDB.GetItem returns a non-nil error, expect an empty string.
func TestItemGetterGetFail(t *testing.T) {
expectedKey := "expected key"
expectedValue := "expected value"
getter := new(ItemGetter)
getter.DynamoDB = &fakeDynamoDB{
payload: map[string]string{"id": expectedKey, "value": expectedValue},
err: errors.New("any error"),
}
if actualValue := getter.Get(expectedKey); len(actualValue) > 0 {
t.Errorf("Expected %q but got %q", expectedValue, actualValue)
}
}