Vendor aws-sdk-go (dep ensure) (#178)
This commit is contained in:
102
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/expression/readme.md
generated
vendored
Normal file
102
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/expression/readme.md
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
# Example
|
||||
|
||||
`scan` is an example how to use Amazon DynamoDB's `expression` package to fill
|
||||
the member fields of Amazon DynamoDB's Operation input types.
|
||||
|
||||
## Representing DynamoDB Expressions
|
||||
|
||||
In the example, the variable `filt` represents a `FilterExpression`. Note that
|
||||
DynamoDB item attributes are represented using the function `Name()` and
|
||||
DynamoDB item values are similarly represented using the function `Value()`. In
|
||||
this context, the string `"Artist"` represents the name of the item attribute
|
||||
that we want to evaluate and the string `"No One You Know"` represents the value
|
||||
we want to evaluate the item attribute against. The relationship between the two
|
||||
[operands](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Syntax)
|
||||
are specified using the method `Equal()`.
|
||||
|
||||
Similarly, the variable `proj` represents a `ProjectionExpression`. The list of
|
||||
item attribute names comprising the `ProjectionExpression` are specified as
|
||||
arguments to the function `NamesList()`. The `expression` package utilizes the
|
||||
type safety of Go and if an item value were to be used as an argument to the
|
||||
function `NamesList()`, a compile time error is returned. The pattern of
|
||||
representing DynamoDB Expressions by indicating relationships between `operands`
|
||||
with functions is consistent throughout the whole `expression` package.
|
||||
|
||||
```go
|
||||
filt := expression.Name("Artist").Equal(expression.Value("No One You Know"))
|
||||
// let :a be an ExpressionAttributeValue representing the string "No One You Know"
|
||||
// equivalent FilterExpression: "Artist = :a"
|
||||
|
||||
proj := expression.NamesList(expression.Name("SongTitle"), expression.Name("AlbumTitle"))
|
||||
// equivalent ProjectionExpression: "SongTitle, AlbumTitle"
|
||||
```
|
||||
|
||||
## Creating an `Expression`
|
||||
|
||||
In the example, the variable `expr` is an instance of an `Expression` type. An
|
||||
`Expression` is built using a builder pattern. First, a new `Builder` is
|
||||
initialized by the `NewBuilder()` function. Then, types representing DynamoDB
|
||||
Expressions are added to the `Builder` by methods `WithFilter()` and
|
||||
`WithProjection()`. The `Build()` method returns an instance of an `Expression`
|
||||
and an error. The error will be either an `InvalidParameterError` or an
|
||||
`UnsetParameterError`.
|
||||
|
||||
```go
|
||||
filt := expression.Name("Artist").Equal(expression.Value("No One You Know"))
|
||||
proj := expression.NamesList(expression.Name("SongTitle"), expression.Name("AlbumTitle"))
|
||||
|
||||
expr, err := expression.NewBuilder().WithFilter(filt).WithProjection(proj).Build()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
```
|
||||
|
||||
## Filling in the fields of a DynamoDB `Scan` API
|
||||
|
||||
In the example, the getter methods of the `Expression` type is used to get the
|
||||
formatted DynamoDB Expression strings. The `ExpressionAttributeNames` and
|
||||
`ExpressionAttributeValues` member field of the DynamoDB API must always be
|
||||
assigned when using an `Expression` since all item attribute names and values
|
||||
are aliased. That means that if the `ExpressionAttributeNames` and
|
||||
`ExpressionAttributeValues` member is not assigned with the corresponding
|
||||
`Names()` and `Values()` methods, the DynamoDB operation will run into a logic
|
||||
error.
|
||||
|
||||
```go
|
||||
filt := expression.Name("Artist").Equal(expression.Value("No One You Know"))
|
||||
proj := expression.NamesList(expression.Name("SongTitle"), expression.Name("AlbumTitle"))
|
||||
expr, err := expression.NewBuilder().WithFilter(filt).WithProjection(proj).Build()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
input := &dynamodb.ScanInput{
|
||||
ExpressionAttributeNames: expr.Names(),
|
||||
ExpressionAttributeValues: expr.Values(),
|
||||
FilterExpression: expr.Filter(),
|
||||
ProjectionExpression: expr.Projection(),
|
||||
TableName: aws.String("Music"),
|
||||
}
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
`go run -tags example scan.go -table "<table_name>" -region "<optional_region>"`
|
||||
|
||||
## Output
|
||||
|
||||
```
|
||||
{
|
||||
Count: #SomeNumber,
|
||||
Items: [{
|
||||
AlbumTitle: {
|
||||
#SomeAlbumTitle
|
||||
},
|
||||
SongTitle: {
|
||||
#SomeSongTitle
|
||||
}
|
||||
}],
|
||||
...
|
||||
ScannedCount: #SomeNumber,
|
||||
}
|
||||
```
|
||||
88
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/expression/scan.go
generated
vendored
Normal file
88
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/expression/scan.go
generated
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
// +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/expression"
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
// Create the Expression to fill the input struct with.
|
||||
filt := expression.Name("Artist").Equal(expression.Value("No One You Know"))
|
||||
proj := expression.NamesList(expression.Name("SongTitle"), expression.Name("AlbumTitle"))
|
||||
expr, err := expression.NewBuilder().WithFilter(filt).WithProjection(proj).Build()
|
||||
if err != nil {
|
||||
exitWithError(fmt.Errorf("failed to create the Expression, %v", err))
|
||||
}
|
||||
|
||||
// Build the query input parameters
|
||||
params := &dynamodb.ScanInput{
|
||||
ExpressionAttributeNames: expr.Names(),
|
||||
ExpressionAttributeValues: expr.Values(),
|
||||
FilterExpression: expr.Filter(),
|
||||
ProjectionExpression: expr.Projection(),
|
||||
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))
|
||||
}
|
||||
|
||||
fmt.Println(result)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
47
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/scanItems/README.md
generated
vendored
Normal file
47
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/scanItems/README.md
generated
vendored
Normal 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
|
||||
```
|
||||
98
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/scanItems/scanItems.go
generated
vendored
Normal file
98
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/scanItems/scanItems.go
generated
vendored
Normal 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
|
||||
}
|
||||
52
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/unitTest/README.md
generated
vendored
Normal file
52
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/unitTest/README.md
generated
vendored
Normal 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
|
||||
```
|
||||
41
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/unitTest/unitTest.go
generated
vendored
Normal file
41
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/unitTest/unitTest.go
generated
vendored
Normal 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
|
||||
}
|
||||
59
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/unitTest/unitTest_test.go
generated
vendored
Normal file
59
vendor/github.com/aws/aws-sdk-go/example/service/dynamodb/unitTest/unitTest_test.go
generated
vendored
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user