jinzhu / copier

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

In case of recursive struct, zero length slice become nil #128

Closed s-takehana closed 7 months ago

s-takehana commented 2 years ago

Reproducible Example

package main

import (
    "fmt"

    "github.com/jinzhu/copier"
)

type SrcStruct1 struct {
    Field1 string
    Field2 []*SrcStruct1
    Field3 []SrcStruct1
    Field4 []*SrcStruct2
    Field5 []SrcStruct2
}

type SrcStruct2 struct {
    Field string
}

type DestStruct1 struct {
    Field1 string
    Field2 []*DestStruct1
    Field3 []DestStruct1
    Field4 []*DestStruct2
    Field5 []DestStruct2
}

type DestStruct2 struct {
    Field string
}

func main() {
    src := SrcStruct1{
        Field1: "Field1",
        Field2: []*SrcStruct1{},
        Field3: []SrcStruct1{},
        Field4: []*SrcStruct2{},
        Field5: []SrcStruct2{},
    }

    var dest DestStruct1
    copier.Copy(&dest, &src)

    fmt.Printf("%#v\n", dest)
}

Description

Output

main.DestStruct1{Field1:"Field1", Field2:[]*main.DestStruct1(nil), Field3:[]main.DestStruct1(nil), Field4:[]*main.DestStruct2{}, Field5:[]main.DestStruct2{}}
s-takehana commented 7 months ago

Maybe fixed.

main.DestStruct1{Field1:"Field1", Field2:[]*main.DestStruct1{}, Field3:[]main.DestStruct1{}, Field4:[]*main.DestStruct2{}, Field5:[]main.DestStruct2{}}