Skip to content

Commit

Permalink
fix #21: make sure that the type of scalar value is correct
Browse files Browse the repository at this point in the history
  • Loading branch information
huandu committed Nov 13, 2023
1 parent 3662a71 commit add54d9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions clone_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ type Unexported struct {
insider
}

type myString string

type insider struct {
i int
i8 int8
Expand All @@ -520,6 +522,7 @@ type insider struct {
f64 float64
c64 complex64
c128 complex128
sptr *myString
arr [4]string
arrPtr *[10]byte
ch chan bool
Expand All @@ -545,6 +548,7 @@ func (scalarWriter) Write(p []byte) (n int, err error) { return }

func testCloneUnexportedFields(t *testing.T, allocator *Allocator) {
a := assert.New(t)
var myStr myString = "myString"
unexported := &Unexported{
insider: insider{
i: -1,
Expand All @@ -564,6 +568,7 @@ func testCloneUnexportedFields(t *testing.T, allocator *Allocator) {
f64: 6.4,
c64: complex(6, 4),
c128: complex(12, 8),
sptr: &myStr,
arr: [4]string{
"a", "b", "c", "d",
},
Expand Down
28 changes: 27 additions & 1 deletion clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package clone

import "testing"
import (
"testing"

"github.com/huandu/go-assert"
)

func TestCloneAll(t *testing.T) {
for name, fn := range testFuncMap {
Expand All @@ -12,3 +16,25 @@ func TestCloneAll(t *testing.T) {
})
}
}

// TestIssue21 tests issue #21.
func TestIssue21(t *testing.T) {
a := assert.New(t)

type Foo string
type Bar struct {
foo *Foo
}

foo := Foo("hello")

src := Bar{
foo: &foo,
}

dst := Clone(src).(Bar)

a.Equal(dst.foo, src.foo)
a.Assert(dst.foo != src.foo)
a.Equal(dst, src)
}
5 changes: 5 additions & 0 deletions structtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ func copyScalarValue(src reflect.Value) reflect.Value {
return src
}

dst := newScalarValue(src)
return dst.Convert(src.Type())
}

func newScalarValue(src reflect.Value) reflect.Value {
// src is an unexported field value. Copy its value.
switch src.Kind() {
case reflect.Bool:
Expand Down

0 comments on commit add54d9

Please sign in to comment.