Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the method Value() (driver.Value, error) will be call 3 times,自定义类型的 Value() (driver.Value, error),会被调用 3 次 #7288

Open
niluan304 opened this issue Nov 22, 2024 · 4 comments
Assignees
Labels

Comments

@niluan304
Copy link

niluan304 commented Nov 22, 2024

GORM Playground Link

如果是必要的话,我会提供的...
If necessary, I will try to do it...

go-gorm/playground#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 中了。

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Nov 22, 2024
Copy link

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.ioSearch Before Asking

Copy link

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.ioSearch Before Asking

@niluan304 niluan304 changed the title the method Value() (sql.Value, error) will be call 3 times,自定义类型的 Value() (sql.Value, error),会被调用 3 次 the method Value() (driver.Value, error) will be call 3 times,自定义类型的 Value() (driver.Value, error),会被调用 3 次 Nov 22, 2024
Copy link

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.ioSearch Before Asking

@github-actions github-actions bot added status:stale type:with reproduction steps with reproduction steps and removed type:missing reproduction steps missing reproduction steps labels Nov 22, 2024
@niluan304
Copy link
Author

niluan304 commented Nov 22, 2024

被自己的愚蠢,气笑了
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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants