當我在更新 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
}
- 1 回答
- 0 關注
- 146 瀏覽
添加回答
舉報
0/150
提交
取消