Skip to content

Commit

Permalink
add ErrNoInput, returned when calling batch/tx APIs without input
Browse files Browse the repository at this point in the history
see #174
  • Loading branch information
guregu committed Sep 2, 2021
1 parent d08fe1c commit e8baa0f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
15 changes: 14 additions & 1 deletion batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,23 @@ func TestBatchGetEmptySets(t *testing.T) {

results = []widget{}
err = table.Batch("UserID", "Time").Get(keysToCheck[:len(keysToCheck)-1]...).Consistent(true).All(&results)
if err != nil {
if err != ErrNotFound {
t.Error(err)
}
if len(results) != 0 {
t.Error("batch get empty set, unexpected length:", len(results), "want:", 0)
}
}

func TestBatchEmptyInput(t *testing.T) {
table := testDB.Table(testTable)
err := table.Batch("UserID", "Time").Get().All(nil)
if err != ErrNoInput {
t.Error("unexpected error", err)
}

_, err = table.Batch("UserID", "Time").Write().Run()
if err != ErrNoInput {
t.Error("unexpected error", err)
}
}
4 changes: 4 additions & 0 deletions batchget.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ type bgIter struct {
}

func newBGIter(bg *BatchGet, fn unmarshalFunc, err error) *bgIter {
if err == nil && len(bg.reqs) == 0 {
err = ErrNoInput
}

iter := &bgIter{
bg: bg,
err: err,
Expand Down
3 changes: 3 additions & 0 deletions batchwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func (bw *BatchWrite) RunWithContext(ctx aws.Context) (wrote int, err error) {
if bw.err != nil {
return 0, bw.err
}
if len(bw.ops) == 0 {
return 0, ErrNoInput
}

// TODO: this could be made to be more efficient,
// by combining unprocessed items with the next request.
Expand Down
12 changes: 12 additions & 0 deletions tx.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package dynamo

import (
"errors"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/gofrs/uuid"
)

// ErrNoInput is returned when APIs that can take multiple inputs are run with zero inputs.
// For example, in a transaction with no operations.
var ErrNoInput = errors.New("dynamo: no input items")

type getTxOp interface {
getTxItem() (*dynamodb.TransactGetItem, error)
}
Expand Down Expand Up @@ -143,6 +149,9 @@ func (tx *GetTx) AllWithContext(ctx aws.Context, out interface{}) error {
}

func (tx *GetTx) input() (*dynamodb.TransactGetItemsInput, error) {
if len(tx.items) == 0 {
return nil, ErrNoInput
}
input := &dynamodb.TransactGetItemsInput{}
for _, item := range tx.items {
tgi, err := item.getTxItem()
Expand Down Expand Up @@ -269,6 +278,9 @@ func (tx *WriteTx) RunWithContext(ctx aws.Context) error {
}

func (tx *WriteTx) input() (*dynamodb.TransactWriteItemsInput, error) {
if len(tx.items) == 0 {
return nil, ErrNoInput
}
input := &dynamodb.TransactWriteItemsInput{}
for _, item := range tx.items {
wti, err := item.writeTxItem()
Expand Down
11 changes: 11 additions & 0 deletions tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,15 @@ func TestTx(t *testing.T) {

t.Logf("1: %+v 2: %+v 3: %+v", record1, record2, record3)
t.Logf("All: %+v (len: %d)", records, len(records))

// no input
err = testDB.GetTx().All(nil)
if err != ErrNoInput {
t.Error("unexpected error", err)
}

err = testDB.WriteTx().Run()
if err != ErrNoInput {
t.Error("unexpected error", err)
}
}

0 comments on commit e8baa0f

Please sign in to comment.