jinzhu / copier

Copier for golang, copy value from struct to struct and more
MIT License
5.52k stars 485 forks source link

fix: failed valuer convert return unexpected true #214

Closed laushunyu closed 3 months ago

laushunyu commented 4 months ago

when the field of struct implement driver.Valuer and cannot convert to dest type directly, copier.set() will return a unexpected (true, nil)

testcode:

package main

import (
    "database/sql/driver"
    "encoding/json"
    "fmt"

    "github.com/jinzhu/copier"
)

type IntArray []int

func (a IntArray) Value() (driver.Value, error) {
    return json.Marshal(a)
}

type Int int

type From struct {
    Data IntArray
}

type To struct {
    Data []Int
}

func main() {
    var (
        from = From{
            Data: IntArray{1, 2, 3},
        }
        to To
    )
    if err := copier.Copy(&to, from); err != nil {
        panic(err)
    }

    fmt.Printf("%#v", to)
}

output:

main.To{Data:[]main.Int(nil)}

fixed:

main.To{Data:[]main.Int{1, 2, 3}}
laushunyu commented 4 months ago

@jinzhu review needed pls

@jinzhu 带带弟弟

jinzhu commented 3 months ago

Hi @laushunyu

Can you add some tests for this?

laushunyu commented 3 months ago

Hi @laushunyu

Can you add some tests for this?

Hi!

testcase added.

in current master:

=== RUN   TestValuerConv
    copier_converter_test.go:272: copier failed from copier_test.From{Data:copier_test.IntArray{1, 2, 3}} to copier_test.To{Data:[]copier_test.Int(nil)}
--- FAIL: TestValuerConv (0.00s)

FAIL

after fixed:

=== RUN   TestValuerConv
copier_test.To{Data:[]copier_test.Int{1, 2, 3}}--- PASS: TestValuerConv (0.00s)
PASS