forked from aws/aws-dax-go
/
projection.go
201 lines (170 loc) · 5.21 KB
/
projection.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
A copy of the License is located at
http://www.apache.org/licenses/LICENSE-2.0
or in the "license" file accompanying this file. This file is distributed
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
package client
import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/dynamodb"
"sort"
"strconv"
"strings"
)
type documentPathElement struct {
index int
name string
}
type documentPath struct {
elements []documentPathElement
}
func documentPathElementFromIndex(idx int) documentPathElement {
return documentPathElement{index: idx, name: ""}
}
func documentPathElementFromName(nm string) documentPathElement {
return documentPathElement{index: -1, name: nm}
}
func buildProjectionOrdinals(projectionExpression *string, expressionAttributeNames map[string]*string) ([]documentPath, error) {
if projectionExpression == nil || *projectionExpression == "" {
return nil, nil
}
terms := strings.Split(*projectionExpression, ",")
dps := make([]documentPath, 0, len(terms))
for _, t := range terms {
dp, err := buildDocumentPath(strings.TrimSpace(t), expressionAttributeNames)
if err != nil {
return nil, err
}
dps = append(dps, dp)
}
return dps, nil
}
func buildDocumentPath(path string, expressionAttributeNames map[string]*string) (documentPath, error) {
var substitutes map[string]*string
if expressionAttributeNames != nil {
substitutes = expressionAttributeNames
}
res := strings.Split(path, ".")
var elements []documentPathElement
for _, re := range res {
idx := strings.Index(re, "[")
if idx == -1 {
elements = append(elements, documentPathElementFromName(getOrDefault(substitutes, re, re)))
continue
}
if idx == 0 {
return documentPath{}, awserr.New(request.InvalidParameterErrCode, "invalid path: "+path, nil)
}
pre := re[0:idx]
elements = append(elements, documentPathElementFromName(getOrDefault(substitutes, pre, pre)))
for idx != -1 {
re = re[idx+1:]
idx = strings.Index(re, "]")
if idx == -1 {
return documentPath{}, awserr.New(request.InvalidParameterErrCode, "invalid path: "+path, nil)
}
lidx, err := strconv.Atoi(re[:idx])
if err != nil {
return documentPath{}, err
}
elements = append(elements, documentPathElementFromIndex(lidx))
re = re[idx+1:]
idx = strings.Index(re, "[")
if idx > 0 {
return documentPath{}, awserr.New(request.InvalidParameterErrCode, "invalid path: "+path, nil)
}
}
if len(elements) == 0 {
return documentPath{}, awserr.New(request.InvalidParameterErrCode, "invalid path: "+path, nil)
}
}
return documentPath{elements: elements}, nil
}
func getOrDefault(m map[string]*string, key, value string) string {
v, ok := m[key]
if ok {
return *v
} else {
return value
}
}
type itemNode struct {
children map[documentPathElement]*itemNode
value *dynamodb.AttributeValue
}
func (in *itemNode) toAttribute() *dynamodb.AttributeValue {
if in == nil {
return nil
}
if in.value != nil {
return in.value
}
val := dynamodb.AttributeValue{}
if len(in.children) != 0 {
m := in.children
keys := make([]documentPathElement, 0, len(m))
for k := range m {
keys = append(keys, k)
}
// it is safe to assume keys do not have a mix of array and map indexes as the request succeeded in DynamoDB at this point
if keys[0].index < 0 {
val.M = make(map[string]*dynamodb.AttributeValue, len(keys))
for k, v := range m {
val.M[k.name] = v.toAttribute()
}
} else {
// order in the response item should be same as order in actual item, not the one in projection expression
// eg: projection expression "list[1],list[0]" returns "list[valAt(0),valAt(1)]
sort.Slice(keys, func(i, j int) bool { return keys[i].index < keys[j].index })
val.L = make([]*dynamodb.AttributeValue, 0, len(keys))
for _, k := range keys {
val.L = append(val.L, m[k].toAttribute())
}
}
}
return &val
}
type itemBuilder struct {
root *itemNode
}
func (ib *itemBuilder) insert(path documentPath, value *dynamodb.AttributeValue) {
if ib.root == nil {
var children map[documentPathElement]*itemNode
ib.root = &itemNode{children: children}
}
ib.insertNode(ib.root, path.elements, value)
}
func (ib *itemBuilder) toItem() map[string]*dynamodb.AttributeValue {
item := make(map[string]*dynamodb.AttributeValue)
if ib.root == nil {
return item
}
c := ib.root.children
for k, v := range c { // top level attribute names are strings
item[k.name] = v.toAttribute()
}
return item
}
func (ib *itemBuilder) insertNode(node *itemNode, elements []documentPathElement, value *dynamodb.AttributeValue) {
if len(elements) == 0 {
node.value = value
return
}
e := elements[0]
if node.children == nil {
node.children = make(map[documentPathElement]*itemNode)
}
n, ok := node.children[e]
if !ok {
n = &itemNode{}
node.children[e] = n
}
ib.insertNode(n, elements[1:], value)
}