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

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

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

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

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

  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:

 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:

  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

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),
})

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:

确定