dongman5539 2018-12-04 22:13
浏览 13
已采纳

使用结构更新值

I'm stuck when I'm updating an empty string values in a struct for updating dynamodb table.

Currently I have this struct

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"`
}

And this code when updating an item

keyAttr, err := dynamodbattribute.MarshalMap(key)
if err != nil {
    return nil, err
}
valAttr, err := dynamodbattribute.MarshalMap(attributes)
if err != nil {
    return nil, err
}

keyAttr will be used for the Key field and valAttr will be used in ExpressionAttributeValues field. Note that I didn't include the complete updating fields function to save space. But I will do that if you ask for it.

Currently the function is running fine except when I updated one of the field with empty string. E.g. client.Address = aws.String(""). While I'm fine with dynamodb converting my empty string to null, I can't seem to find a way to update it because of the omitempty tag.

I need the omitempty tag to ignore all nil values. However, I just researched that the omitempty tag also omits empty string values. Currently I have to make a struct in my function like this.

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"`
}

But i'm not a fan of repeating things. So, the question is: is there any better way of doing this? How do you guys use structs with dynamodb?

EDIT

Based on @Peter's comment, it seems that json.Encode() does print the empty string if it's not nil.

{"client_key":"test","username":"test","email":"","first_name":"test","last_name":"","phone":"","title":"","email_verified":false,"phone_verified":false,"updated_at":"2018-12-06T14:04:56.2743841+11:00"}

The problem seems to be in dynamodbattribute.MarshalMap function

  • 写回答

1条回答 默认 最新

  • donglou8371 2018-12-06 03:35
    关注

    After several trials, I finally got it. I didn't test it so I don't know whether it's buggy or not. But it seems to work for me right now.

    So what I did was encode the struct with json.Marshal first then use json.Unmarshal with a map[string]interface{}. Then, I use dynamodbattribute.Marshal to convert it to map[string]*AttributeValue

    Here's the code:

    var temp map[string]interface{}
    json.Unmarshal(tempStr, &temp)
    valAttr, err := dynamodbattribute.MarshalMap(temp)
    if err != nil {
        return nil, err
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 touchsocket udp组播
  • ¥20 MAC怎么安装Silverlight 插件?以及安装了怎么启用
  • ¥15 VS2012中查询语句无法填入解析,数值传不进去
  • ¥15 gis系统开发出现命名空间“ESRI.ArcGIS”中不存在类型或命名空间名“Analyst3D”报错
  • ¥15 怎么让ai定时给我发信息 c#或者python
  • ¥15 scrapy的Error
  • ¥15 RBF-VSG姚凤军论文复现问题
  • ¥30 开发一个APP商城在制作tabbar的时候运行不了代码没有检查出错误,但是显示不出tabbar,以下为运行结果,如何解决?
  • ¥15 多网卡服务器中winform如何绑定指定网卡
  • ¥15 关于#python#pandas#的问题,想要实现:多个TXT导入Excel,进行分列,不同txt之间都从第一行开始,请各位专家解答!