我有问题的代码(最小的可重现示例):
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
)
const (
tableName = "myTable"
)
var (
sess = session.Must(session.NewSession())
dynamodbClient = dynamodb.New(sess)
)
func main() {
update := expression.Set(
expression.Name("mymap.vitaly.zdanevich"),
expression.Value(true),
)
expr, _ := expression.NewBuilder().WithUpdate(update).Build()
updateItemInput := dynamodb.UpdateItemInput{
TableName: aws.String(tableName),
Key: map[string]*dynamodb.AttributeValue{
"pk": {
S: aws.String("test"),
},
},
UpdateExpression: expr.Update(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
}
fmt.Printf("%#v\n", updateItemInput)
dynamodbClient.UpdateItem(&updateItemInput)
}
结果更新项目输入:
{
ExpressionAttributeNames: {
#0: "mymap",
#1: "vitaly",
#2: "zdanevich"
},
ExpressionAttributeValues: {
:0: {
BOOL: true
}
},
Key: {
pk: {
S: "test"
},
sk: {
S: "test"
}
},
TableName: "myTable",
UpdateExpression: "SET #0.#1.#2 = :0\n"
}
是的,我知道 dot 用于访问内部地图值,但在这种情况下最好的策略是什么?我目前的解决方案:
expression.Name("mymap."+strings.Replace(username, ".", "_", -1))
慕工程0101907
相关分类