GORM Playground Link
~~如果是必要的话,我会提供的...~~ ~~If necessary, I will try to do it...~~
https://github.com/go-gorm/playground/pull/773
Description
My ENV
- go version 1.22.2
- gorm.io/gorm v1.25.12
- gorm.io/driver/mysql v1.5.7
What I do
type Model struct {
gorm.Model
JsonField JsonField `gorm:"type:json;not null;" json:"jsonField"`
}
type JsonField struct {
Array []string `json:"array"`
}
func (j JsonField) Value() (driver.Value, error) {
for i := range j.Array {
j.Array[i] += "1" // 我想要修改(加密)原来的字段,I want to modify/encrypt the fields
}
data, err := json.Marshal(j)
if err != nil {
return nil, fmt.Errorf("json marshal fail, err: %w", err)
}
return data, nil
}
func WhatIdo () {
data := Model{
JsonField: JsonField{
Array: []string{"A", "B", "C"},
},
}
DB.WithContext(context.TODO()).Model(&Model{}).
Create(&data)
}
I got
INSERT INTO
`models` (
`created_at`,
`updated_at`,
`deleted_at`,
`json_field`
)
VALUES
(
'2024-11-22 21:16:31.699',
'2024-11-22 21:16:31.699',
NULL,
-- should be '{"array":["A1","B1","C1"]}'
'{"array":["A111","B111","C111"]}'
)
看起来,这行代码,被执行了 3次:
j.Array[i] += "1" // 我想要修改(加密)原来的字段,I want to modify/encrypt the fields
Others
可能这是一个低级问题,但我对 gorm 不够熟悉。
我也尝试过 debug,却因为我的水平问题,迷失在 stack 中了。
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: niluan304
被自己的愚蠢,气笑了 So stupid —— me.
func (j JsonField) Value() (driver.Value, error) {
for i := range j.Array {
j.Array[i] += "1" // 我想要修改(加密)原来的字段,I want to modify/encrypt the fields
}
data, err := json.Marshal(j)
if err != nil {
return nil, fmt.Errorf("json marshal fail, err: %w", err)
}
return data, nil
}
should be
func (j JsonField) Value() (driver.Value, error) {
clone := slices.Clone(j.Array) // don't update receiver !!!
for i := range clone {
clone[i] += "1" // 我想要修改(加密)原来的字段,I want to modify/encrypt the fields
}
data, err := json.Marshal(JsonField{
Array: clone,
})
if err != nil {
return nil, fmt.Errorf("json marshal fail, err: %w", err)
}
return data, nil
}