globusdigital / deep-copy

Deep copy generator
BSD 3-Clause "New" or "Revised" License
150 stars 34 forks source link

Bug: problem with pointer/non-pointer values mismatch #15

Closed egawata closed 3 years ago

egawata commented 3 years ago

When a field in a struct requires a non-pointer value, and --pointer-receiver is specified, incorrect functions are generated.

For example:

package sample

type Foo struct {
    B Bar
}

type Bar struct {
    S string
}

Run the command:

$ deep-copy -type Foo -type Bar --pointer-receiver -o sample/sample_deepcopy.go ./sample

It generates:

// generated by deep-copy -type Foo -type Bar --pointer-receiver -o sample/sample_deepcopy.go ./sample; DO NOT EDIT.

package sample

// DeepCopy generates a deep copy of *Foo
func (o *Foo) DeepCopy() *Foo {
    var cp Foo = *o
    cp.B = o.B.DeepCopy()
    return &cp
}

// DeepCopy generates a deep copy of *Bar
func (o *Bar) DeepCopy() *Bar {
    var cp Bar = *o
    return &cp
}

o.B.DeepCopy() returns *Bar, but Foo.B want Bar(non-pointer), so it would fail at compile time.

sample/sample_deepcopy.go:8:7: cannot use o.B.DeepCopy() (type *Bar) as type Bar in assignment

The same problem is also found when a field requires pointer value and --pointer-receiver is not used.