/
scan.go
102 lines (80 loc) · 2.68 KB
/
scan.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
package mockdynamodb
import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// Scan is not implemented. It will panic in all cases.
func (db *DynamoDb) Scan(input *dynamodb.ScanInput) (*dynamodb.ScanOutput, error) {
db.Lock()
defer db.Unlock()
return db.scan(input)
}
// ScanWithContext is not implemented. It will panic in all cases.
func (db *DynamoDb) ScanWithContext(_ aws.Context, input *dynamodb.ScanInput, _ ...request.Option) (*dynamodb.ScanOutput, error) {
db.Lock()
defer db.Unlock()
return db.scan(input)
}
// ScanRequest is not implemented. It will panic in all cases.
func (db *DynamoDb) ScanRequest(*dynamodb.ScanInput) (*request.Request, *dynamodb.ScanOutput) {
panic("ScanRequest is not implemented")
}
// ScanPages is not implemented. It will panic in all cases.
func (db *DynamoDb) ScanPages(i *dynamodb.ScanInput, fn func(*dynamodb.ScanOutput, bool) bool) error {
db.Lock()
defer db.Unlock()
return db.scanPages(i, fn)
}
// ScanPagesWithContext is not implemented. It will panic in all cases.
func (db *DynamoDb) ScanPagesWithContext(_ aws.Context, i *dynamodb.ScanInput, fn func(*dynamodb.ScanOutput, bool) bool, _ ...request.Option) error {
db.Lock()
defer db.Unlock()
return db.scanPages(i, fn)
}
func (db *DynamoDb) scan(input *dynamodb.ScanInput) (*dynamodb.ScanOutput, error) {
err := checkRequiredFields(map[string]interface{}{
"ScanInput.TableName": input.TableName,
})
if err != nil {
return &dynamodb.ScanOutput{}, err
}
if table := db.GetTable(*input.TableName); table != nil {
table.receivedScanInputs = append(table.receivedScanInputs, *input)
if table.moreScanOutputs() {
o := table.popReturnScanOutput()
if o != nil {
return o, nil
}
return nil, errors.New("nil ScanOutput")
}
return nil, errors.New("no ScanOutputs to return")
}
return &dynamodb.ScanOutput{}, errorNonExistentTable()
}
func (db *DynamoDb) scanPages(input *dynamodb.ScanInput, fn func(*dynamodb.ScanOutput, bool) bool) error {
err := checkRequiredFields(map[string]interface{}{
"ScanInput.TableName": input.TableName,
})
if err != nil {
return err
}
if table := db.GetTable(*input.TableName); table != nil {
table.receivedScanInputs = append(table.receivedScanInputs, *input)
if table.moreScanOutputs() {
for ok := true; ok; ok = table.moreScanOutputs() {
output := table.popReturnScanOutput()
if output == nil {
return errors.New("nil ScanOutput")
}
if !fn(output, false) {
return errors.New("output handler function returned false")
}
}
return nil
}
return errors.New("no ScanOutputs to return")
}
return errorNonExistentTable()
}