-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
attr.go
141 lines (131 loc) · 3.31 KB
/
attr.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
package dynamo
import (
"fmt"
"strconv"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// Item is a type alias for the raw DynamoDB item type.
type Item = map[string]types.AttributeValue
type shapeKey byte
func (sk shapeKey) String() string { return string(rune(sk)) }
func (sk shapeKey) GoString() string { return fmt.Sprintf("shape(%s)", sk.String()) }
const (
shapeB shapeKey = 'B'
shapeBOOL shapeKey = 'T'
shapeN shapeKey = 'N'
shapeS shapeKey = 'S'
shapeL shapeKey = 'L'
shapeM shapeKey = 'M'
shapeBS shapeKey = 'b'
shapeNS shapeKey = 'n'
shapeSS shapeKey = 's'
shapeNULL shapeKey = '0'
shapeAny shapeKey = '_'
shapeInvalid shapeKey = 0
)
func shapeOf(av types.AttributeValue) shapeKey {
if av == nil {
return shapeInvalid
}
switch av.(type) {
case *types.AttributeValueMemberB:
return shapeB
case *types.AttributeValueMemberBS:
return shapeBS
case *types.AttributeValueMemberBOOL:
return shapeBOOL
case *types.AttributeValueMemberN:
return shapeN
case *types.AttributeValueMemberS:
return shapeS
case *types.AttributeValueMemberL:
return shapeL
case *types.AttributeValueMemberNS:
return shapeNS
case *types.AttributeValueMemberSS:
return shapeSS
case *types.AttributeValueMemberM:
return shapeM
case *types.AttributeValueMemberNULL:
return shapeNULL
}
return shapeAny
}
// av2iface converts an av into interface{}.
func av2iface(av types.AttributeValue) (interface{}, error) {
switch v := av.(type) {
case *types.AttributeValueMemberB:
return v.Value, nil
case *types.AttributeValueMemberBS:
return v.Value, nil
case *types.AttributeValueMemberBOOL:
return v.Value, nil
case *types.AttributeValueMemberN:
return strconv.ParseFloat(v.Value, 64)
case *types.AttributeValueMemberS:
return v.Value, nil
case *types.AttributeValueMemberL:
list := make([]interface{}, 0, len(v.Value))
for _, item := range v.Value {
iface, err := av2iface(item)
if err != nil {
return nil, err
}
list = append(list, iface)
}
return list, nil
case *types.AttributeValueMemberNS:
set := make([]float64, 0, len(v.Value))
for _, n := range v.Value {
f, err := strconv.ParseFloat(n, 64)
if err != nil {
return nil, err
}
set = append(set, f)
}
return set, nil
case *types.AttributeValueMemberSS:
return v.Value, nil
case *types.AttributeValueMemberM:
m := make(map[string]interface{}, len(v.Value))
for k, v := range v.Value {
iface, err := av2iface(v)
if err != nil {
return nil, err
}
m[k] = iface
}
return m, nil
case *types.AttributeValueMemberNULL:
return nil, nil
}
return nil, fmt.Errorf("dynamo: unsupported AV: %#v", av)
}
func avTypeName(av types.AttributeValue) string {
if av == nil {
return "<nil>"
}
switch av.(type) {
case *types.AttributeValueMemberB:
return "binary"
case *types.AttributeValueMemberBS:
return "binary set"
case *types.AttributeValueMemberBOOL:
return "boolean"
case *types.AttributeValueMemberN:
return "number"
case *types.AttributeValueMemberS:
return "string"
case *types.AttributeValueMemberL:
return "list"
case *types.AttributeValueMemberNS:
return "number set"
case *types.AttributeValueMemberSS:
return "string set"
case *types.AttributeValueMemberM:
return "map"
case *types.AttributeValueMemberNULL:
return "null"
}
return "<empty>"
}