-
Notifications
You must be signed in to change notification settings - Fork 4
/
godynamo.go
146 lines (134 loc) · 3.62 KB
/
godynamo.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
package godynamo
import (
"database/sql/driver"
"errors"
"reflect"
"strconv"
"github.com/aws/aws-sdk-go-v2/aws/transport/http"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/aws/smithy-go"
"github.com/btnguyen2k/consu/reddo"
)
var (
dataTypes = map[string]types.ScalarAttributeType{
"BINARY": "B",
"B": "B",
"NUMBER": "N",
"N": "N",
"STRING": "S",
"S": "S",
}
keyTypes = map[string]types.KeyType{
"HASH": "HASH",
"RANGE": "RANGE",
}
tableClasses = map[string]types.TableClass{
"STANDARD": types.TableClassStandard,
"STANDARD_IA": types.TableClassStandardInfrequentAccess,
}
)
var (
typeM = reflect.TypeOf(make(map[string]interface{}))
typeL = reflect.TypeOf(make([]interface{}, 0))
typeS = reddo.TypeString
typeBool = reddo.TypeBool
typeN = reddo.TypeFloat
typeTime = reddo.TypeTime
)
// IsAwsError returns true if err is an AWS-specific error, and it matches awsErrCode.
func IsAwsError(err error, awsErrCode string) bool {
var aerr *smithy.OperationError
if errors.As(err, &aerr) {
var herr *http.ResponseError
if errors.As(aerr.Err, &herr) {
return reflect.TypeOf(herr.Err).Elem().Name() == awsErrCode
}
}
//if aerr, ok := err.(*smithy.OperationError); ok {
// if herr, ok := aerr.Err.(*http.ResponseError); ok {
// return reflect.TypeOf(herr.Err).Elem().Name() == awsErrCode
// }
//}
return false
}
// ValuesToNamedValues transforms a []driver.Value to []driver.NamedValue.
//
// @Available since v0.2.0
func ValuesToNamedValues(values []driver.Value) []driver.NamedValue {
result := make([]driver.NamedValue, len(values))
for i, v := range values {
result[i] = driver.NamedValue{Name: "$" + strconv.Itoa(i+1), Ordinal: i, Value: v}
}
return result
}
// ToAttributeValueUnsafe marshals a Go value to AWS AttributeValue, ignoring error.
//
// @Available since v0.2.0
func ToAttributeValueUnsafe(value interface{}) types.AttributeValue {
av, _ := ToAttributeValue(value)
return av
}
// ToAttributeValue marshals a Go value to AWS AttributeValue.
func ToAttributeValue(value interface{}) (types.AttributeValue, error) {
switch v := value.(type) {
case types.AttributeValueMemberB:
return &v, nil
case types.AttributeValueMemberBOOL:
return &v, nil
case types.AttributeValueMemberBS:
return &v, nil
case types.AttributeValueMemberL:
return &v, nil
case types.AttributeValueMemberM:
return &v, nil
case types.AttributeValueMemberN:
return &v, nil
case types.AttributeValueMemberNS:
return &v, nil
case types.AttributeValueMemberNULL:
return &v, nil
case types.AttributeValueMemberS:
return &v, nil
case types.AttributeValueMemberSS:
return &v, nil
default:
return attributevalue.Marshal(value)
}
}
// nameFromAttributeValue returns the name of the attribute value.
//
// e.g.
//
// types.AttributeValueMemberB -> "B"
// types.AttributeValueMemberBOOL -> "BOOL"
func nameFromAttributeValue(v interface{}) string {
// De-reference pointer
for reflect.TypeOf(v).Kind() == reflect.Ptr {
v = reflect.ValueOf(v).Elem().Interface()
}
switch v.(type) {
case types.AttributeValueMemberB:
return "B"
case types.AttributeValueMemberBOOL:
return "BOOL"
case types.AttributeValueMemberBS:
return "BS"
case types.AttributeValueMemberL:
return "L"
case types.AttributeValueMemberM:
return "M"
case types.AttributeValueMemberN:
return "N"
case types.AttributeValueMemberNS:
return "NS"
case types.AttributeValueMemberNULL:
return "NULL"
case types.AttributeValueMemberS:
return "S"
case types.AttributeValueMemberSS:
return "SS"
default:
return ""
}
}