@@ -122,33 +122,51 @@ func queryForArgs(api dynamodbiface.DynamoDBAPI, args []string) (*dynamodb.Query
122
122
table := args [0 ]
123
123
tableDescription , _ := tableDescription (api , table )
124
124
125
+ attrType := func (name string ) string {
126
+ for _ , def := range tableDescription .AttributeDefinitions {
127
+ if name == * def .AttributeName {
128
+ return * def .AttributeType
129
+ }
130
+ }
131
+ panic (fmt .Sprintf ("unknown key: %s" , name ))
132
+ }
133
+
125
134
partitionKeyValue := args [1 ]
126
135
partitionKeyName := * tableDescription .KeySchema [0 ].AttributeName
127
136
128
137
expression := ""
129
138
names := map [string ]* string {}
130
- values := map [string ]* dynamodb.AttributeValue {
131
- ":partitionKey" : {S : & partitionKeyValue },
139
+ values := map [string ]* dynamodb.AttributeValue {}
140
+
141
+ setValue := func (values map [string ]* dynamodb.AttributeValue , name , key , value string ) {
142
+ typ := attrType (name )
143
+ switch typ {
144
+ case dynamodb .ScalarAttributeTypeS :
145
+ values [key ] = & dynamodb.AttributeValue {S : & value }
146
+ case dynamodb .ScalarAttributeTypeB :
147
+ values [key ] = & dynamodb.AttributeValue {B : []byte (value )}
148
+ case dynamodb .ScalarAttributeTypeN :
149
+ values [key ] = & dynamodb.AttributeValue {N : & value }
150
+ }
132
151
}
133
152
153
+ setValue (values , partitionKeyName , ":partitionKey" , partitionKeyValue )
154
+
134
155
if len (args ) == 2 { // table, partition value
135
156
expression = "#partitionKey = :partitionKey"
136
- values = map [string ]* dynamodb.AttributeValue {
137
- ":partitionKey" : {S : & partitionKeyValue },
138
- }
139
157
names = map [string ]* string {
140
158
"#partitionKey" : & partitionKeyName ,
141
159
}
142
160
} else if len (args ) == 3 { // table, partition value, sort (value|expression)
161
+ sortKeyName := * tableDescription .KeySchema [1 ].AttributeName
143
162
expr := parseSortExpr (args [2 ])
144
163
expression = fmt .Sprintf ("#partitionKey = :partitionKey and %s" , expr .expression )
145
164
for k , v := range expr .values {
146
- v := v
147
- values [k ] = & dynamodb.AttributeValue {S : & v }
165
+ setValue (values , sortKeyName , k , v )
148
166
}
149
167
names = map [string ]* string {
150
168
"#partitionKey" : & partitionKeyName ,
151
- "#skey" : tableDescription . KeySchema [ 1 ]. AttributeName ,
169
+ "#skey" : & sortKeyName ,
152
170
}
153
171
}
154
172
0 commit comments