-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.go
172 lines (153 loc) · 3.95 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package dynago
import (
"errors"
"fmt"
"reflect"
"time"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// Scan represents a Scan operation.
type Scan struct {
input *dynamodb.ScanInput
output *ScanOutput
dynago *Dynago
items interface{}
err error
}
// ScanOutput represents the output of a scan command.
type ScanOutput struct {
LastEvaluatedKey map[string]*dynamodb.AttributeValue
}
// Scan returns a Scan operation.
func (d *Dynago) Scan(items interface{}) *Scan {
return &Scan{
input: &dynamodb.ScanInput{
ConsistentRead: &d.config.DefaultConsistentRead,
TableName: &d.config.DefaultTableName,
},
items: items,
dynago: d,
}
}
// TableName sets the table.
func (q *Scan) TableName(name string) *Scan {
q.input.TableName = &name
return q
}
// Segment sets Segment.
func (q *Scan) Segment(segment int64) *Scan {
q.input.Segment = &segment
return q
}
// TotalSegments sets TotalSegments.
func (q *Scan) TotalSegments(segments int64) *Scan {
q.input.TotalSegments = &segments
return q
}
// IndexName sets the IndexName.
func (q *Scan) IndexName(index string) *Scan {
q.input.IndexName = &index
return q
}
// Select sets which attributes will be selected.
func (q *Scan) Select(attrs string) *Scan {
q.input.Select = &attrs
return q
}
// Limit sets the Limit.
func (q *Scan) Limit(limit int64) *Scan {
q.input.Limit = &limit
return q
}
// ProjectionExpression sets the ProjectionExpression.
func (q *Scan) ProjectionExpression(exp string) *Scan {
q.input.ProjectionExpression = &exp
return q
}
// FilterExpression sets the FilterExpression.
func (q *Scan) FilterExpression(exp string) *Scan {
q.input.FilterExpression = &exp
return q
}
// ExclusiveStartKey sets the ExclusiveStartKey.
func (q *Scan) ExclusiveStartKey(key map[string]*dynamodb.AttributeValue) *Scan {
q.input.ExclusiveStartKey = key
return q
}
// ConsistentRead sets ConsistentRead.
func (q *Scan) ConsistentRead(val bool) *Scan {
q.input.ConsistentRead = &val
return q
}
// ExpressionAttributeValue sets an ExpressionAttributeValue.
func (q *Scan) ExpressionAttributeValue(key string, val interface{}, layout ...string) *Scan {
if q.input.ExpressionAttributeValues == nil {
q.input.ExpressionAttributeValues = make(map[string]*dynamodb.AttributeValue)
}
lay := time.RFC3339
if len(layout) > 0 {
lay = layout[0]
}
av, err := q.dynago.simpleMarshal(reflect.ValueOf(val), lay)
if err != nil {
q.err = err
}
q.input.ExpressionAttributeValues[key] = av
return q
}
// ExpressionAttributeName sets an ExpressionAttributeName.
func (q *Scan) ExpressionAttributeName(name string, sub string) *Scan {
if q.input.ExpressionAttributeNames == nil {
q.input.ExpressionAttributeNames = make(map[string]*string)
}
q.input.ExpressionAttributeNames[name] = &sub
return q
}
func (q *Scan) Output(output *ScanOutput) *Scan {
q.output = output
return q
}
// Exec executes the operation.
func (q *Scan) Exec() error {
rv := reflect.ValueOf(q.items)
if rv.Kind() != reflect.Pointer {
return errors.New("dynago: dynago.Scan.Exec: v must be pointer")
}
for rv.Kind() == reflect.Pointer {
rv = reflect.Indirect(rv)
}
var err error
output, err := q.dynago.ddb.Scan(q.input)
if err != nil {
return fmt.Errorf("d.ddb.GetItem: %w", err)
}
if q.output != nil {
q.output.LastEvaluatedKey = output.LastEvaluatedKey
}
rt := reflect.TypeOf(q.items)
for rt.Kind() == reflect.Pointer {
rt = rt.Elem()
}
ft := rt.Elem()
indirect := true
if ft.Kind() == reflect.Pointer {
ft = ft.Elem()
indirect = false
}
if ft.Kind() == reflect.Pointer {
return errors.New("dynago: dynago.Scan.Exec: elements of v can not be pointers to pointers")
}
s := reflect.MakeSlice(rt, len(output.Items), len(output.Items))
for i, item := range output.Items {
iv := reflect.New(ft)
if err := q.dynago.Unmarshal(item, iv.Interface()); err != nil {
return fmt.Errorf("q.dynago.Unmarshal: %w", err)
}
if indirect {
iv = reflect.Indirect(iv)
}
s.Index(i).Set(iv)
}
rv.Set(s)
return nil
}