Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nil Slice Becomes Empty Slice After Copying #215

Closed
helloqiu opened this issue Jul 10, 2024 · 0 comments · Fixed by #216
Closed

Nil Slice Becomes Empty Slice After Copying #215

helloqiu opened this issue Jul 10, 2024 · 0 comments · Fixed by #216
Assignees

Comments

@helloqiu
Copy link
Contributor

Reproducible Example

Check https://go.dev/play/p/wIwXSvhuj4j

package main

import (
	"fmt"
	"reflect"

	"github.com/jinzhu/copier"
)

type A struct {
	SomeField []string
}

type B struct {
	SomeField []uint
}

func main() {
	a := A{}
	b := B{}
	if err := copier.Copy(&b, &a); err != nil {
		panic(err)
	}
	anotherB := B{}
	if !reflect.DeepEqual(b, anotherB) {
		fmt.Println("b and anotherB should be equal")
	}
	fmt.Printf("a.SomeField : len(%d), is-nil:(%t)\n", len(a.SomeField), a.SomeField == nil)
	fmt.Printf("b.SomeField : len(%d), is-nil:(%t)\n", len(b.SomeField), b.SomeField == nil)
	fmt.Printf("anotherB.SomeField : len(%d), is-nil:(%t)\n", len(anotherB.SomeField), anotherB.SomeField == nil)
}

Result:

b and anotherB should be equal
a.SomeField : len(0), is-nil:(true)
b.SomeField : len(0), is-nil:(false)
anotherB.SomeField : len(0), is-nil:(true)

Description

If two struts have two fields with the same name but they are slice of different types, the value on the target struct would be assigned to an empty slice rather than a nil slice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants