skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

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

  2. 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("%sn", val)
    }
    

    Output:

    {
      SS: ["socialAccountId"]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search