doumang20060820 2019-03-26 05:06
浏览 74

DynamoDB多个过滤条件,给出错误-buildTree错误:未设置参数:ConditionBuilder

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")))
  • 写回答

1条回答 默认 最新

  • doubipiao1611 2019-03-26 10:37
    关注

    ConditionBuilder has mode field

    type ConditionBuilder struct {
        operandList   []OperandBuilder
        conditionList []ConditionBuilder
        mode          conditionMode
    }
    

    The zero value of mode is unsetCond. When build condition, unsetCond raises the error.

    https://github.com/aws/aws-sdk-go/blob/7798c2e0edc02ba058f7672d32f4ebf6603b5fc6/service/dynamodb/expression/condition.go#L1415

    case unsetCond:
            return exprNode{}, newUnsetParameterError("buildTree", "ConditionBuilder")
    

    In your code, if queryParam != “param1” and queryParam != “param2”, the param1Condition and param2Condition is zero value of ConditionBuilder, which fails on build.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?