forked from dugancathal/dynago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
90 lines (77 loc) · 2.23 KB
/
helpers.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
package dynago
import (
"log"
"strings"
)
type expressionAttributes struct {
ExpressionAttributeValues Document `json:",omitempty"`
ExpressionAttributeNames map[string]string `json:",omitempty"`
}
// Helper for a variety of endpoint types to build a params dictionary.
func (e *expressionAttributes) paramHelper(key string, value interface{}) {
e.assignParams([]Param{{key, value}})
}
// Helper to build multi-params dictionary
func (e *expressionAttributes) paramsHelper(params []Params) {
if len(params) == 0 {
return
}
output := make([]Param, 0, len(params))
for _, p := range params {
output = append(output, p.AsParams()...)
}
e.assignParams(output)
}
func (e *expressionAttributes) assignParams(params []Param) {
var copyValues, copyNames bool
for _, p := range params {
if strings.HasPrefix(p.Key, "#") {
if !copyNames {
copyNames = true
eaNameCopy(&e.ExpressionAttributeNames, 1)
}
e.ExpressionAttributeNames[p.Key] = p.Value.(string)
} else {
if !copyValues {
copyValues = true
paramCopy(&e.ExpressionAttributeValues, len(params))
}
e.ExpressionAttributeValues[p.Key] = p.Value
}
}
}
func paramCopy(doc *Document, extendBy int) Document {
params := make(Document, len(*doc)+extendBy)
if *doc != nil {
for k, v := range *doc {
params[k] = v
}
}
*doc = params
return params
}
func eaNameCopy(doc *map[string]string, extendBy int) {
names := make(map[string]string, len(*doc)+extendBy)
for k, v := range *doc {
names[k] = v
}
*doc = names
}
/*
Debug sets the debug mode.
This is a set of bit-flags you can use to set up how much debugging dynago uses:
dynago.Debug = dynago.DebugRequests | dynago.DebugResponses
Debug flags are copied into any executors, requesters, etc at creation time so
the flags must be set before creating any Executor or client for them to take
effect.
*/
var Debug DebugFlags
// DebugFunc is set to control the target of debug output.
var DebugFunc func(format string, v ...interface{}) = func(format string, v ...interface{}) {
log.Printf("Dynago DEBUG: "+format, v...)
}
// HasFlag is a convenience method to check if a value has a flag:
// Debug.HasFlag(DebugRequests)
func (v DebugFlags) HasFlag(flag DebugFlags) bool {
return (v & flag) != 0
}