Dynamodb.ScanInput – Cannot use 'expr.Names()' (type map[string]*string) as the type map[string]string

huangapple go评论76阅读模式
英文:

Dynamodb.ScanInput - Cannot use 'expr.Names()' (type map[string]*string) as the type map[string]string

问题

我需要使用过滤器在DynamoDB中扫描我的表格。我在互联网上找到了许多示例,但是当我尝试使用它们时,一直出现相同的错误。

filter := expression.Name("CreatedDate").LessThan(expression.Value(time.Now().UTC()))
expr, err := expression.NewBuilder().WithFilter(filter).Build()
if err != nil {
panic(err)
}
out, err := svc.Scan(context.TODO(), &dynamodb.ScanInput{
TableName: aws.String(tableName),
FilterExpression: expr.Filter(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
})
if err != nil {
panic(err)
}

在expr.Names()和expr.Values()上,我得到了错误信息

无法将'expr.Names()'(类型为map[string]*string)用作类型map[string]string

提前谢谢!

英文:

I need to scan my table in dynamodb using filtering. I found many examples in internet but when I try use them I have the same error all the time.

filter := expression.Name("CreatedDate").LessThan(expression.Value(time.Now().UTC()))
expr, err := expression.NewBuilder().WithFilter(filter).Build()
if err != nil {
	panic(err)
}
out, err := svc.Scan(context.TODO(), &dynamodb.ScanInput{
	TableName:                 aws.String(tableName),
	FilterExpression:          expr.Filter(),
	ExpressionAttributeNames:  expr.Names(),
	ExpressionAttributeValues: expr.Values(),
})
if err != nil {
	panic(err)
}

On expr.Names() and expr.Values() I got error

Cannot use 'expr.Names()' (type map[string]*string) as the type map[string]string

Thank you in advance!

答案1

得分: 4

您没有指明是编译错误还是在 panic 中显示。

无论如何,在使用 aws-sdk-go 的情况下,expr.Names()expr.Values() 应该是 map[string]*string 类型,除非使用的是 aws-sdk-go-v2

修复方法

更新您的导入语句,从

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"

更改为以下内容

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
英文:

You did not specify if it was a compilation error or it is shown in the panic.

Anway, the expr.Names() expr.Values() would be map[string]*string in case of usage expression from aws-sdk-go except aws-sdk-go-v2.

Fix

Update your imports from

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"

to something like below

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"

huangapple
  • 本文由 发表于 2022年4月15日 02:33:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/71876200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定