Skip to content

Commit

Permalink
Function correctly for non-string keys
Browse files Browse the repository at this point in the history
  • Loading branch information
aidansteele committed Jan 17, 2019
1 parent 5303293 commit f1e9d13
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,33 +122,51 @@ func queryForArgs(api dynamodbiface.DynamoDBAPI, args []string) (*dynamodb.Query
table := args[0]
tableDescription, _ := tableDescription(api, table)

attrType := func(name string) string {
for _, def := range tableDescription.AttributeDefinitions {
if name == *def.AttributeName {
return *def.AttributeType
}
}
panic(fmt.Sprintf("unknown key: %s", name))
}

partitionKeyValue := args[1]
partitionKeyName := *tableDescription.KeySchema[0].AttributeName

expression := ""
names := map[string]*string{}
values := map[string]*dynamodb.AttributeValue{
":partitionKey": {S: &partitionKeyValue},
values := map[string]*dynamodb.AttributeValue{}

setValue := func(values map[string]*dynamodb.AttributeValue, name, key, value string) {
typ := attrType(name)
switch typ {
case dynamodb.ScalarAttributeTypeS:
values[key] = &dynamodb.AttributeValue{S: &value}
case dynamodb.ScalarAttributeTypeB:
values[key] = &dynamodb.AttributeValue{B: []byte(value)}
case dynamodb.ScalarAttributeTypeN:
values[key] = &dynamodb.AttributeValue{N: &value}
}
}

setValue(values, partitionKeyName, ":partitionKey", partitionKeyValue)

if len(args) == 2 { // table, partition value
expression = "#partitionKey = :partitionKey"
values = map[string]*dynamodb.AttributeValue{
":partitionKey": {S: &partitionKeyValue},
}
names = map[string]*string{
"#partitionKey": &partitionKeyName,
}
} else if len(args) == 3 { // table, partition value, sort (value|expression)
sortKeyName := *tableDescription.KeySchema[1].AttributeName
expr := parseSortExpr(args[2])
expression = fmt.Sprintf("#partitionKey = :partitionKey and %s", expr.expression)
for k, v := range expr.values {
v := v
values[k] = &dynamodb.AttributeValue{S: &v}
setValue(values, sortKeyName, k, v)
}
names = map[string]*string{
"#partitionKey": &partitionKeyName,
"#skey": tableDescription.KeySchema[1].AttributeName,
"#skey": &sortKeyName,
}
}

Expand Down

0 comments on commit f1e9d13

Please sign in to comment.