Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 60 additions & 19 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
# yamllint disable-line rule:line-length
# yaml-language-server: $schema=https://golangci-lint.run/jsonschema/golangci.jsonschema.json
version: "2"
run:
timeout: 40s
timeout: 240s
tests: true
allow-parallel-runners: true

issues:
max-issues-per-linter: 0
max-same-issues: 0

formatters:
enable:
- gofumpt
- goimports

linters:
default: none
enable:
- errcheck
- errorlint
- gocritic
- gocyclo
- gocognit
Expand All @@ -20,13 +28,14 @@ linters:
- govet
- ineffassign
- staticcheck
- wsl_v5
- nilnesserr
- nakedret
- unused
- wsl

settings:
revive:
severity: warning
confidence: 0.3
confidence: 0.8
rules:
- name: blank-imports
- name: context-as-argument
Expand Down Expand Up @@ -55,8 +64,7 @@ linters:
- name: identical-branches
- name: defer
arguments:
-
- loop
- - loop
- method-call
- return
- name: string-of-int
Expand All @@ -72,33 +80,66 @@ linters:
arguments:
- 5
- name: modifies-value-receiver
- name: modifies-parameter
- name: unnecessary-stmt
gocyclo:
min-complexity: 9
gocognit:
min-complexity: 10
errcheck:
check-type-assertions: true
govet:
gocritic:
enable-all: true
disabled-checks:
- ifElseChain
- unnecessaryBlock
- sprintfQuotedString
- deferUnlambda
- paramTypeCombine
- builtinShadow
- octalLiteral
- rangeValCopy
- nestingReduce
- httpNoBody
- regexpSimplify
- externalErrorReassign
- hugeParam
- importShadow
- unnamedResult
- filepathJoin
- commentedOutCode
wsl_v5:
allow-first-in-block: true
allow-whole-block: false
branch-max-lines: 2
enable:
- shadow
wsl:
allow-assign-and-call: true
allow-cuddle-declarations: true
force-err-cuddling: true
- err
disable:
- assign
- decl
gosec:
excludes:
- G104
- "G115"
- "G602"
staticcheck:
checks:
- all
- -QF1008

errcheck:
check-type-assertions: true
govet:
enable:
- shadow
- nilness

exclusions:
generated: disable
rules:
- path: (.+)_test\.go
- path: autogenerated\.go$
linters:
- gocyclo
- gosec
- gocognit

- path: '(.+)_test\.go'
linters:
- errcheck
- staticcheck
- wsl
- wsl_v5
72 changes: 58 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Amazon DynamoDB testing library written in Go.

## Usage

### In-memory client (existing)

Create the dynamodb client:

```go
Expand Down Expand Up @@ -53,6 +55,36 @@ if err != nil {

**NOTE** these methods only support string attributes.

### HTTP server mode (new)

You can now run minidyn as an HTTP server compatible with the DynamoDB JSON API. This is handy for using `httptest.NewServer` and real AWS SDK clients without swapping implementations.

```go
import (
"net/http/httptest"

miniserver "github.com/truora/minidyn/server"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)

srv := httptest.NewServer(miniserver.NewServer())
defer srv.Close()

cfg, _ := config.LoadDefaultConfig(ctx,
config.WithEndpointResolverWithOptions(
aws.EndpointResolverWithOptionsFunc(func(service, region string, _ ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{URL: srv.URL, PartitionID: "aws", SigningRegion: "us-east-1"}, nil
})),
)
ddb := dynamodb.NewFromConfig(cfg)

// use ddb as usual: CreateTable, PutItem, Query, etc.
```

Supported operations in HTTP mode include CreateTable, DescribeTable, UpdateTable, DeleteTable, PutItem, GetItem, UpdateItem, DeleteItem, Query, Scan, and BatchWriteItem.

## Language interpreter

This library has an interpreter implementation for the DynamoDB Expressions.
Expand All @@ -62,22 +94,22 @@ This library has an interpreter implementation for the DynamoDB Expressions.
#### Types

| Name | Type | Short | Supported? |
|-----------|----------|-------|-----------|
| Number | scalar | N | y |
| String | scalar | S | y |
| Binary | scalar | B | y |
| Bool | scalar | BOOL | y |
| Null | scalar | NULL | y |
| List | document | L | y |
| Map | document | M | y |
| StringSet | set | SS | y |
| NumberSet | set | NS | y |
| BinarySet | set | BS | y |
| --------- | -------- | ----- | ---------- |
| Number | scalar | N | y |
| String | scalar | S | y |
| Binary | scalar | B | y |
| Bool | scalar | BOOL | y |
| Null | scalar | NULL | y |
| List | document | L | y |
| Map | document | M | y |
| StringSet | set | SS | y |
| NumberSet | set | NS | y |
| BinarySet | set | BS | y |

#### Expressions

| |Syntax | Supported? |
|----------------------------------------------|-------------------------------------------------------------------------------------|------------|
| | Syntax | Supported? |
| -------------------------------------------- | ----------------------------------------------------------------------------------- | ---------- |
| operand comparator operand | = <> < <= > and >= | y |
| operand BETWEEN operand AND operand | N,S,B | y |
| operand IN ( operand (',' operand (, ...) )) | | y |
Expand All @@ -91,13 +123,25 @@ This library has an interpreter implementation for the DynamoDB Expressions.
#### Expressions

| | Syntax | Supported? |
|----------|------------------------------|------------|
| -------- | ---------------------------- | ---------- |
| SET | SET action [, action] ... | y |
| REMOVE | REMOVE action [, action] ... | y |
| ADD | ADD action [, action] ... | y |
| DELETE | DELETE action [, action] ... | y |
| function | list_append, if_not_exists | y |

## Developer notes

### Regenerating HTTP request structs

The HTTP server uses generated JSON input shapes in `server/requests.go` so we can cleanly unmarshal DynamoDB JSON without the SDK’s `AttributeValue` interfaces. If you update DynamoDB inputs or need to refresh these shapes, run:

```bash
go run ./tools/generate_requests
```

This will rewrite `server/requests.go` based on the AWS SDK v2 DynamoDB input types, replacing `AttributeValue` interfaces with the concrete JSON-friendly `AttributeValue` defined in `server/types.go`.

### What to do when the interpreter does not work properly?

When it happens you can override the intepretation using like this:
Expand Down
32 changes: 16 additions & 16 deletions aws-v1/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func TestUpdateTable(t *testing.T) {
},
},
GlobalSecondaryIndexUpdates: []*dynamodb.GlobalSecondaryIndexUpdate{
&dynamodb.GlobalSecondaryIndexUpdate{
{
Create: &dynamodb.CreateGlobalSecondaryIndexAction{
IndexName: aws.String("newIndex"),
KeySchema: []*dynamodb.KeySchemaElement{
Expand Down Expand Up @@ -494,7 +494,7 @@ func TestUpdateTable(t *testing.T) {

input = &dynamodb.UpdateTableInput{
GlobalSecondaryIndexUpdates: []*dynamodb.GlobalSecondaryIndexUpdate{
&dynamodb.GlobalSecondaryIndexUpdate{
{
Update: &dynamodb.UpdateGlobalSecondaryIndexAction{
IndexName: aws.String("newIndex"),
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
Expand All @@ -511,7 +511,7 @@ func TestUpdateTable(t *testing.T) {

input = &dynamodb.UpdateTableInput{
GlobalSecondaryIndexUpdates: []*dynamodb.GlobalSecondaryIndexUpdate{
&dynamodb.GlobalSecondaryIndexUpdate{
{
Delete: &dynamodb.DeleteGlobalSecondaryIndexAction{
IndexName: aws.String("newIndex"),
},
Expand Down Expand Up @@ -1012,7 +1012,7 @@ func TestUpdateExpressions(t *testing.T) {
},
ReturnValues: aws.String("UPDATED_NEW"),
UpdateExpression: aws.String("ADD lvl :one"),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{":one": &dynamodb.AttributeValue{N: aws.String("1")}},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{":one": {N: aws.String("1")}},
},
verify: func(tc *testing.T, client dynamodbiface.DynamoDBAPI) {
a := assert.New(tc)
Expand Down Expand Up @@ -1140,7 +1140,7 @@ func TestQueryWithContext(t *testing.T) {
c.Len(out.Items, 1)
c.Empty(out.LastEvaluatedKey)

var pokemonQueried = pokemon{}
pokemonQueried := pokemon{}

err = dynamodbattribute.UnmarshalMap(out.Items[0], &pokemonQueried)
c.NoError(err)
Expand Down Expand Up @@ -1721,7 +1721,7 @@ func TestBatchWriteItemWithContext(t *testing.T) {
c.NoError(err)

requests := []*dynamodb.WriteRequest{
&dynamodb.WriteRequest{
{
PutRequest: &dynamodb.PutRequest{
Item: item,
},
Expand Down Expand Up @@ -1757,10 +1757,10 @@ func TestBatchWriteItemWithContext(t *testing.T) {

_, err = client.BatchWriteItemWithContext(context.Background(), &dynamodb.BatchWriteItemInput{
RequestItems: map[string][]*dynamodb.WriteRequest{
tableName: []*dynamodb.WriteRequest{
&dynamodb.WriteRequest{
tableName: {
{
DeleteRequest: &dynamodb.DeleteRequest{Key: map[string]*dynamodb.AttributeValue{
"id": &dynamodb.AttributeValue{S: aws.String("001")},
"id": {S: aws.String("001")},
}},
},
},
Expand All @@ -1777,11 +1777,11 @@ func TestBatchWriteItemWithContext(t *testing.T) {

_, err = client.BatchWriteItemWithContext(context.Background(), &dynamodb.BatchWriteItemInput{
RequestItems: map[string][]*dynamodb.WriteRequest{
tableName: []*dynamodb.WriteRequest{
&dynamodb.WriteRequest{
tableName: {
{
DeleteRequest: &dynamodb.DeleteRequest{Key: map[string]*dynamodb.AttributeValue{
"id": &dynamodb.AttributeValue{S: aws.String("001")},
"type": &dynamodb.AttributeValue{S: aws.String("grass")},
"id": {S: aws.String("001")},
"type": {S: aws.String("grass")},
}},
PutRequest: &dynamodb.PutRequest{Item: item},
},
Expand All @@ -1793,8 +1793,8 @@ func TestBatchWriteItemWithContext(t *testing.T) {

_, err = client.BatchWriteItemWithContext(context.Background(), &dynamodb.BatchWriteItemInput{
RequestItems: map[string][]*dynamodb.WriteRequest{
tableName: []*dynamodb.WriteRequest{
&dynamodb.WriteRequest{},
tableName: {
{},
},
},
})
Expand Down Expand Up @@ -1834,7 +1834,7 @@ func TestBatchWriteItemWithFailingDatabase(t *testing.T) {
c.NoError(err)

requests := []*dynamodb.WriteRequest{
&dynamodb.WriteRequest{
{
PutRequest: &dynamodb.PutRequest{
Item: item,
},
Expand Down
5 changes: 2 additions & 3 deletions aws-v2/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1986,9 +1986,8 @@ func TestBatchWriteItemWithFailingDatabase(t *testing.T) {
}

output, err := client.BatchWriteItem(context.Background(), input)
c.NoError(err)

c.NotEmpty(output.UnprocessedItems)
c.EqualError(err, "InternalServerError: emulated error")
c.Nil(output)
}

func TestTransactWriteItems(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion core/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func TestSearchData(t *testing.T) {
queryInput.started = true

result, lastItem = newTable.SearchData(queryInput)
c.Equal([]map[string]*types.Item{{}}, result)
c.Equal([]map[string]*types.Item{}, result)
c.Equal(map[string]*types.Item{}, lastItem)

queryInput.ExclusiveStartKey = item
Expand Down
Loading
Loading