jinzhu / copier

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

Nested structs and pointers don't get deep copied #57

Closed krishna-birla closed 3 years ago

krishna-birla commented 4 years ago

Sample Code

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

type Outer struct {
    Inner Inner
}

type Inner struct {
    intPtr *int
}

type DriverOptions struct {
    GenOptions map[string]interface{}
}

func main() {
    intVal := 5
    outer := Outer{
        Inner: Inner{
            intPtr: &intVal,
        },
    }
    from := DriverOptions{
        GenOptions: map[string]interface{}{
            "key": outer,
        },
    }
    to := DriverOptions{}
    if err := copier.Copy(&to, &from); nil != err {
        return
    }
    *to.GenOptions["key"].(Outer).Inner.intPtr = 6
    fmt.Println(*from.GenOptions["key"].(Outer).Inner.intPtr)
}

Output

6
math-nao commented 3 years ago

About your example, unexported fields cannot be copied. PR #70 adds support for copying to a map using interface.