I am building REST APIs, using Lambda and DynamoDB in GO.
I need to query the data based on multiple filters.
The number of filters can be varying based on the number of query parameters user has provided, while calling the REST API.
As per the below post, I had developed the code to add multiple conditions.
AWS SDK for Go - DynamoDb - Add multiple conditions to FilterExpression
But when I invoke the function, I get below error, in the logs.-
buildTree error: unset parameter: ConditionBuilder
The Filter expression is not applied and the scan returns all the results.
Here is the code snippet.
for queryParam, queryParamValue := range searchParams {
fmt.Println("queryParam:", queryParam, "=>", "queryParamValue:", queryParamValue)
if queryParam == “param1” {
param1Condition = expression.Name(“param1”).Equal(expression.Value(queryParamValue))
}
if queryParam == “param2” {
param2Condition = expression.Name(“param2”).Equal(expression.Value(queryParamValue))
}
}
sampleExpr, errSample := expression.NewBuilder().
WithCondition(param1Condition.Or(param2Condition)).
Build()
if errSample != nil {
fmt.Println("Error in building Sample Expr ", errSample)
} else {
fmt.Println("sampleExpr ", sampleExpr)
}
input := &dynamodb.ScanInput{
ExpressionAttributeNames: sampleExpr.Names(),
ExpressionAttributeValues: sampleExpr.Values(),
FilterExpression: sampleExpr.Filter(),
TableName: aws.String(deviceInfotable),
}
But if I create the expression in different way, it works.
filt := expression.Name("param1").Equal(expression.Value("valu1")).Or(expression.Name("param2").Equal(expression.Value("value2")))