How to declare a ValueBuilder object to be a StringSet when doing an UpdateItem in Dynamodb using gosdk?

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

How to declare a ValueBuilder object to be a StringSet when doing an UpdateItem in Dynamodb using gosdk?

问题

注意:我将为你翻译以下内容:

注意:这可能看起来像是这个问题的重复,但它不同之处在于我需要在golang中执行此操作,而那个问题是使用JS。

我想在goSDK中对dynamodb项执行以下操作:

  1. UpdateExpression: 'ADD socialAccounts :socialAccountId',
  2. ExpressionAttributeValues: {
  3. ':socialAccountId': {
  4. 'SS': [socialAccountId]
  5. }
  6. },

通常在GoSDK中的操作如下:

  1. expression.Add(expression.Name("socialAccounts"), expression.Value([]string{"socialAccountId"}))

然而,SDK将字符串数组作为L(List)类型而不是SS(StringSet)类型处理。

记录的错误:

  1. Error: ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST, typeSet: ALLOWED_FOR_ADD_OPERAND

我无法在文档中找到任何指定类型的功能。

有人可以帮忙吗?
为什么我要这样做:

  1. 即使属性socialAccounts不存在,我也希望它能正常工作。
  2. 我不想使用带有list_appendSET操作,因为那样可能会引入重复元素。
英文:

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:

  1. UpdateExpression: 'ADD socialAccounts :socialAccountId',
  2. ExpressionAttributeValues: {
  3. ':socialAccountId': {
  4. 'SS': [socialAccountId]
  5. }
  6. },

Typically how that works out in GoSDK is:

  1. 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:

  1. 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:

  1. I want this to work even when the attribute socialAccounts does not exist.
  2. I don't want to use the SET opearation with list_append because that can introduce duplicate elements.

答案1

得分: 1

如果需要指定类型,请使用dynamodb.AttributeValue

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/service/dynamodb"
  6. "github.com/aws/aws-sdk-go/service/dynamodb/expression"
  7. )
  8. func main() {
  9. val := (&dynamodb.AttributeValue{}).SetSS(aws.StringSlice([]string{"socialAccountId"}))
  10. expression.Add(expression.Name("socialAccounts"), expression.Value(val))
  11. fmt.Printf("%s\n", val)
  12. }

输出结果:

  1. {
  2. "SS": ["socialAccountId"]
  3. }
英文:

Use dynamodb.AttributeValue if you need to specify the type:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/service/dynamodb"
  6. "github.com/aws/aws-sdk-go/service/dynamodb/expression"
  7. )
  8. func main() {
  9. val := (&dynamodb.AttributeValue{}).SetSS(aws.StringSlice([]string{"socialAccountId"}))
  10. expression.Add(expression.Name("socialAccounts"), expression.Value(val))
  11. fmt.Printf("%s\n", val)
  12. }

Output:

  1. {
  2. SS: ["socialAccountId"]
  3. }

答案2

得分: 0

我更喜欢@Zeke Lu的答案而不是我的答案。
但以下是一个"不得已而为之"的方法,应该可以完成所有任务。

  1. updateExpr := "ADD #SOCIAL :SOCIAL"
  2. expName := map[string]*string{"#SOCIAL": aws.String(SOCIAL)}
  3. expVal := map[string]*dynamodb.AttributeValue{":SOCIAL": {SS: aws.StringSlice(socialAccounts)}}
  4. _, errUpdate = tablesConnection.UpdateItem(&dynamodb.UpdateItemInput{
  5. ExpressionAttributeNames: expName,
  6. ExpressionAttributeValues: expVal,
  7. Key: key,
  8. TableName: aws.String(tableName),
  9. UpdateExpression: aws.String(updateExpr),
  10. })
英文:

I prefer @Zeke Lu 's answer over my own.
But the following is a "push-comes-to-shove" method that should accomplish everything.

  1. updateExpr := "ADD #SOCIAL :SOCIAL"
  2. expName := map[string]*string{"#SOCIAL": aws.String(SOCIAL)}
  3. expVal := map[string]*dynamodb.AttributeValue{":SOCIAL": {SS: aws.StringSlice(socialAccounts)}}
  4. _, errUpdate = tablesConnection.UpdateItem(&dynamodb.UpdateItemInput{
  5. ExpressionAttributeNames: expName,
  6. ExpressionAttributeValues: expVal,
  7. Key: key,
  8. TableName: aws.String(tableName),
  9. UpdateExpression: aws.String(updateExpr),
  10. })

huangapple
  • 本文由 发表于 2023年6月25日 23:55:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551263.html
匿名

发表评论

匿名网友

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

确定