當(dāng)我在更新 dynamodb 表的結(jié)構(gòu)中更新空字符串值時,我被卡住了。目前我有這個結(jié)構(gòu)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現(xiàn)場。請注意,為了節(jié)省空間,我沒有包括完整的更新字段功能。但如果你要求,我會這樣做。目前該函數(shù)運行良好,除非我用空字符串更新了其中一個字段。例如client.Address = aws.String("")。雖然我可以使用 dynamodb 將我的空字符串轉(zhuǎn)換為null,但由于標簽,我似乎無法找到更新它的方法omitempty。我需要 omitempty 標簽來忽略所有nil值。但是,我剛剛研究過該omitempty標記還省略了空字符串值。目前我必須像這樣在我的函數(shù)中創(chuàng)建一個結(jié)構(gòu)。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"`}但我不喜歡重復(fù)事情。所以,問題是:有沒有更好的方法來做到這一點?你們?nèi)绾螌⒔Y(jié)構(gòu)與dynamodb一起使用?
1 回答

烙印99
TA貢獻1829條經(jīng)驗 獲得超13個贊
經(jīng)過多次嘗試,我終于明白了。我沒有測試它,所以我不知道它是否有錯誤。但它現(xiàn)在似乎對我有用。
json.Marshal所以我所做的是首先對結(jié)構(gòu)進行編碼,然后json.Unmarshal使用map[string]interface{}. 然后,我用dynamodbattribute.Marshal它來轉(zhuǎn)換成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 關(guān)注
- 128 瀏覽
添加回答
舉報
0/150
提交
取消