为了账号安全,请及时绑定邮箱和手机立即绑定

使用结构更新值

使用结构更新值

Go
繁星点点滴滴 2023-05-15 14:33:50
当我在更新 dynamodb 表的结构中更新空字符串值时,我被卡住了。目前我有这个结构type Client struct {    ClientID       *string    `json:"client_key,omitempty"`    Name           *string    `json:"client_name,omitempty"`    Address        *string    `json:"address,omitempty"`    Country        *string    `json:"country,omitempty"`    City           *string    `json:"city,omitempty"`    State          *string    `json:"state,omitempty"`    PostCode       *string    `json:"post_code,omitempty"`    CreatedAt      *time.Time `json:"created_at,omitempty"`}更新项目时的这段代码keyAttr, err := dynamodbattribute.MarshalMap(key)if err != nil {    return nil, err}valAttr, err := dynamodbattribute.MarshalMap(attributes)if err != nil {    return nil, err}keyAttrwill be used for the KeyfieldvalAttr将用于ExpressionAttributeValues现场。请注意,为了节省空间,我没有包括完整的更新字段功能。但如果你要求,我会这样做。目前该函数运行良好,除非我用空字符串更新了其中一个字段。例如client.Address = aws.String("")。虽然我可以使用 dynamodb 将我的空字符串转换为null,但由于标签,我似乎无法找到更新它的方法omitempty。我需要 omitempty 标签来忽略所有nil值。但是,我刚刚研究过该omitempty标记还省略了空字符串值。目前我必须像这样在我的函数中创建一个结构。type client struct {    Name     *string `json:"client_name"`    Address  *string `json:"address"`    Country  *string `json:"country"`    City     *string `json:"city"`    State    *string `json:"state"`    PostCode *string `json:"post_code"`}但我不喜欢重复事情。所以,问题是:有没有更好的方法来做到这一点?你们如何将结构与dynamodb一起使用?
查看完整描述

1 回答

?
烙印99

TA贡献1829条经验 获得超13个赞

经过多次尝试,我终于明白了。我没有测试它,所以我不知道它是否有错误。但它现在似乎对我有用。


json.Marshal所以我所做的是首先对结构进行编码,然后json.Unmarshal使用map[string]interface{}. 然后,我用dynamodbattribute.Marshal它来转换成map[string]*AttributeValue


这是代码:


var temp map[string]interface{}

json.Unmarshal(tempStr, &temp)

valAttr, err := dynamodbattribute.MarshalMap(temp)

if err != nil {

    return nil, err

}


查看完整回答
反对 回复 2023-05-15
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信