NOTE:可能看起来像this的复制品,但它不同,因为我需要通过Golang来完成,而那是在JS中.

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

 UpdateExpression: 'ADD socialAccounts :socialAccountId',
 ExpressionAttributeValues: {
   ':socialAccountId': {
      'SS': [socialAccountId]
    }
 },

在GoSDK中,这通常是这样的:

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

然而,SDK没有将字符串数组作为SS(StringSet)类型,而是将其作为L(List)类型.

记录的错误:

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

我找不到任何指定如何提及类型的功能in the docs.

Can anyone help?
Why I want to do it this way:

  1. 我希望即使在属性socialAccounts不存在的情况下,这也能起作用.
  2. 我不想对list_append使用SET操作,因为那样会引入重复的元素.

推荐答案

如果需要指定类型,请使用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"]
}

Go相关问答推荐

有没有更简单的方法在Go中编写这个逻辑?

如何在定制普罗米修斯出口商中测试动态计量注册?

按键值排序字符串- Golang

如何在出现错误时停止从通道读取?

golang testscript .txtar 语法,用于 stderr 或 stdout 中包含的文本

Golang Fiber Render - 将数据发送到多个布局

Golang telegram 机器人

Docker 执行失败并显示cmd/ENTRYPOINT 中的命令未找到

linter 警告:返回值被忽略

当我的 go build 成功时,如何修复我的 docker build 失败? Dockerfile 包括 go mod 下载

错误!在为 age-viewer-go 运行 wails dev 或 wails build 命令时

Go Template if 条件

使用 Golang 在字符串中循环重复数据

如何获取多个 url 参数值

从golang中的url加载图像

为什么我不能使用来自 gocloak 中 Login() 的访问令牌在 KeyCloak 中创建新客户端?

正确编码 JWT

带有 grpc 的 protobuf 用于拆分包中的 Go

map和struct golang的合并

Go generics:我会在哪里使用 any 而不是 interface{}?