jinzhu / copier

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

Add support to copy from a nil slice pointer to a slice field when DeepCopy is enabled #118

Closed math-nao closed 2 years ago

math-nao commented 3 years ago

Reproducible Example

https://play.golang.org/p/H5N5ppK3vjY

package main

import "log"
import "github.com/jinzhu/copier"

func main() {
        type Sample1 struct {
        Foobar *[]string
    }
    type Sample2 struct {
        Foobar []string
    }

    from := Sample1{}
    to := Sample2{}
    err := copier.CopyWithOption(&to, from, copier.Option{
        DeepCopy: true,
    })
    log.Printf("DeepCopy enabled, err: %v", err)

    err = copier.Copy(&to, from)
    log.Printf("DeepCopy disabled, err: %v", err)

    log.Printf("from: %v", from)
    log.Printf("to: %v", to)
}

Output:
2009/11/10 23:00:00 DeepCopy enabled, err: copy from is invalid
2009/11/10 23:00:00 DeepCopy disabled, err: <nil>
2009/11/10 23:00:00 from: {<nil>}
2009/11/10 23:00:00 to: {[]}

Description

An error is triggered when copying a nil slice pointer to a slice field with DeepCopy, all following fields are not copied due to previous error.