go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
37.09k stars 3.94k forks source link

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

Open niluan304 opened 1 week ago

niluan304 commented 1 week ago

GORM Playground Link

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

https://github.com/go-gorm/playground/pull/773

Description

My ENV

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[bot] commented 1 week ago

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[bot] commented 1 week ago

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[bot] commented 1 week ago

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 commented 1 week ago

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