-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
161 lines (144 loc) · 3.78 KB
/
query.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
package dynago
import (
"errors"
"fmt"
"reflect"
"time"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// Query represents a Query operation.
type Query struct {
input *dynamodb.QueryInput
dynago *Dynago
items interface{}
err error
}
// Query returns a Query operation.
func (d *Dynago) Query(items interface{}) *Query {
return &Query{
input: &dynamodb.QueryInput{
ConsistentRead: &d.config.DefaultConsistentRead,
TableName: &d.config.DefaultTableName,
},
items: items,
dynago: d,
}
}
// TableName sets the table.
func (q *Query) TableName(name string) *Query {
q.input.TableName = &name
return q
}
// IndexName sets the index.
func (q *Query) IndexName(index string) *Query {
q.input.IndexName = &index
return q
}
// Select sets which attributes will be selected.
func (q *Query) Select(attrs string) *Query {
q.input.Select = &attrs
return q
}
// Limit sets the Limit.
func (q *Query) Limit(limit int64) *Query {
q.input.Limit = &limit
return q
}
// ProjectionExpression sets the ProjectionExpression.
func (q *Query) ProjectionExpression(exp string) *Query {
q.input.ProjectionExpression = &exp
return q
}
// FilterExpression sets the FilterExpression.
func (q *Query) FilterExpression(exp string) *Query {
q.input.FilterExpression = &exp
return q
}
// ExclusiveStartKey sets the ExclusiveStartKey.
func (q *Query) ExclusiveStartKey(key map[string]*dynamodb.AttributeValue) *Query {
q.input.ExclusiveStartKey = key
return q
}
// ScanIndexForward sets whether or not to scan index forward.
func (q *Query) ScanIndexForward(val bool) *Query {
q.input.ScanIndexForward = &val
return q
}
// ConsistentRead sets ConsistentRead.
func (q *Query) ConsistentRead(val bool) *Query {
q.input.ConsistentRead = &val
return q
}
// KeyConditionExpression sets the KeyConditionExpression.
func (q *Query) KeyConditionExpression(exp string) *Query {
q.input.KeyConditionExpression = &exp
return q
}
// ExpressionAttributeValue sets an ExpressionAttributeValue.
func (q *Query) ExpressionAttributeValue(key string, val interface{}, layout ...string) *Query {
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 *Query) ExpressionAttributeName(name string, sub string) *Query {
if q.input.ExpressionAttributeNames == nil {
q.input.ExpressionAttributeNames = make(map[string]*string)
}
q.input.ExpressionAttributeNames[name] = &sub
return q
}
// Exec executes the operation.
func (q *Query) Exec() error {
if q.err != nil {
return q.err
}
rv := reflect.ValueOf(q.items)
if rv.Kind() != reflect.Pointer {
return errors.New("dynago: dynago.Query.Exec: v must be pointer")
}
for rv.Kind() == reflect.Pointer {
rv = reflect.Indirect(rv)
}
var err error
output, err := q.dynago.ddb.Query(q.input)
if err != nil {
return fmt.Errorf("d.ddb.Query: %w", err)
}
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.Query.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
}