如何使用Golang过滤器构建器过滤空值?

时间:2019-04-30 09:07:26

标签: amazon-web-services go amazon-dynamodb aws-sdk-go

How to filter on NULL?开始,我正在努力复制Go代码以过滤特定列的空值:

import (
        "github.com/apex/log"
        "github.com/aws/aws-sdk-go-v2/aws"
        "github.com/aws/aws-sdk-go-v2/aws/endpoints"
        "github.com/aws/aws-sdk-go-v2/aws/external"
        "github.com/aws/aws-sdk-go-v2/service/dynamodb"
        "github.com/aws/aws-sdk-go-v2/service/dynamodb/expression"
)

// aws --profile dd dynamodb scan --table-name plocal2 --filter-expression 'paymentmethod = :null' --expression-attribute-values '{":null" :{"NULL":true}}'

var table = "plocal2"

func main() {
        cfg, err := external.LoadDefaultAWSConfig(external.WithSharedConfigProfile("dd"))
        if err != nil {
                log.WithError(err).Fatal("setting up credentials")
                return
        }
        cfg.Region = endpoints.ApSoutheast1RegionID
        db := dynamodb.New(cfg)
        filt := expression.Name("paymentmethod").Equal(expression.Value(expression.Null))
        expr, err := expression.NewBuilder().
                WithFilter(filt).
                Build()
        if err != nil {
                log.WithError(err).Error("failed to build expression")
                return
        }
        scanReq := db.ScanRequest(&dynamodb.ScanInput{
                ExpressionAttributeValues: expr.Values(),
                FilterExpression:          expr.Filter(),
                TableName:                 aws.String(table),
        })
        result, err := scanReq.Send()
        if err != nil {
                log.WithError(err).Error("failed to query table")
                return
        }
        log.Infof("%d", len(result.Items))
}

错误是:

2019/04/30 16:59:54 error failed to query table     error=ValidationException: Invalid FilterExpression: An expression attribute name used in the document path
 is not defined; attribute name: #0
        status code: 400, request id: 8CFTOAGVB7M1IM3DC6BFPEVLUNVV4KQNSO5AEMVJF66Q9ASUAAJG

有没有技巧提示如何调试这个问题,或者我使用expression builder出了什么问题?

真的希望有examples,因为如果使用MarshalMap,因为空字符串变为NULL,对我来说,对NULL进行过滤应该是一种很常见的用例!

0 个答案:

没有答案