英文:
How to declare a ValueBuilder object to be a StringSet when doing an UpdateItem in Dynamodb using gosdk?
问题
注意:我将为你翻译以下内容:
注意:这可能看起来像是这个问题的重复,但它不同之处在于我需要在golang中执行此操作,而那个问题是使用JS。
我想在goSDK中对dynamodb项执行以下操作:
UpdateExpression: 'ADD socialAccounts :socialAccountId',
ExpressionAttributeValues: {
':socialAccountId': {
'SS': [socialAccountId]
}
},
通常在GoSDK中的操作如下:
expression.Add(expression.Name("socialAccounts"), expression.Value([]string{"socialAccountId"}))
然而,SDK将字符串数组作为L
(List)类型而不是SS
(StringSet)类型处理。
记录的错误:
Error: ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST, typeSet: ALLOWED_FOR_ADD_OPERAND
我无法在文档中找到任何指定类型的功能。
有人可以帮忙吗?
为什么我要这样做:
- 即使属性
socialAccounts
不存在,我也希望它能正常工作。 - 我不想使用带有
list_append
的SET
操作,因为那样可能会引入重复元素。
英文:
NOTE: Might seem like a duplicate of this but it's different because I need to do this via golang while that is in JS.
I want to do the following operation in goSDK on a dynamodb item:
UpdateExpression: 'ADD socialAccounts :socialAccountId',
ExpressionAttributeValues: {
':socialAccountId': {
'SS': [socialAccountId]
}
},
Typically how that works out in GoSDK is:
expression.Add(expression.Name("socialAccounts"), expression.Value([]string{"socialAccountId"}))
However the SDK is not taking the array of strings as a SS
(StringSet) type, but instead as a L
(List) type.
Logged Error:
Error: ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST, typeSet: ALLOWED_FOR_ADD_OPERAND
I'm unable to find any functionality in the docs that specify how to mention a type.
Can anyone help?
Why I want to do it this way:
- I want this to work even when the attribute
socialAccounts
does not exist. - I don't want to use the
SET
opearation withlist_append
because that can introduce duplicate elements.
答案1
得分: 1
如果需要指定类型,请使用dynamodb.AttributeValue
:
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
)
func main() {
val := (&dynamodb.AttributeValue{}).SetSS(aws.StringSlice([]string{"socialAccountId"}))
expression.Add(expression.Name("socialAccounts"), expression.Value(val))
fmt.Printf("%s\n", val)
}
输出结果:
{
"SS": ["socialAccountId"]
}
英文:
Use dynamodb.AttributeValue
if you need to specify the type:
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
)
func main() {
val := (&dynamodb.AttributeValue{}).SetSS(aws.StringSlice([]string{"socialAccountId"}))
expression.Add(expression.Name("socialAccounts"), expression.Value(val))
fmt.Printf("%s\n", val)
}
Output:
{
SS: ["socialAccountId"]
}
答案2
得分: 0
我更喜欢@Zeke Lu的答案而不是我的答案。
但以下是一个"不得已而为之"的方法,应该可以完成所有任务。
updateExpr := "ADD #SOCIAL :SOCIAL"
expName := map[string]*string{"#SOCIAL": aws.String(SOCIAL)}
expVal := map[string]*dynamodb.AttributeValue{":SOCIAL": {SS: aws.StringSlice(socialAccounts)}}
_, errUpdate = tablesConnection.UpdateItem(&dynamodb.UpdateItemInput{
ExpressionAttributeNames: expName,
ExpressionAttributeValues: expVal,
Key: key,
TableName: aws.String(tableName),
UpdateExpression: aws.String(updateExpr),
})
英文:
I prefer @Zeke Lu 's answer over my own.
But the following is a "push-comes-to-shove" method that should accomplish everything.
updateExpr := "ADD #SOCIAL :SOCIAL"
expName := map[string]*string{"#SOCIAL": aws.String(SOCIAL)}
expVal := map[string]*dynamodb.AttributeValue{":SOCIAL": {SS: aws.StringSlice(socialAccounts)}}
_, errUpdate = tablesConnection.UpdateItem(&dynamodb.UpdateItemInput{
ExpressionAttributeNames: expName,
ExpressionAttributeValues: expVal,
Key: key,
TableName: aws.String(tableName),
UpdateExpression: aws.String(updateExpr),
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论