Fix pgtype.Inet.AssignTo assigning reference

AssignTo should always assign copy.
Added documentation for AssignTo interface.
This commit is contained in:
Jack Christensen 2017-04-04 21:13:00 -05:00
parent e5c48b17f2
commit 52b58b88a6
2 changed files with 10 additions and 3 deletions

View File

@ -70,13 +70,19 @@ func (src *Inet) AssignTo(dst interface{}) error {
case Present:
switch v := dst.(type) {
case *net.IPNet:
*v = *src.IPNet
*v = net.IPNet{
IP: make(net.IP, len(src.IPNet.IP)),
Mask: make(net.IPMask, len(src.IPNet.Mask)),
}
copy(v.IP, src.IPNet.IP)
copy(v.Mask, src.IPNet.Mask)
return nil
case *net.IP:
if oneCount, bitCount := src.IPNet.Mask.Size(); oneCount != bitCount {
return fmt.Errorf("cannot assign %v to %T", src, dst)
}
*v = src.IPNet.IP
*v = make(net.IP, len(src.IPNet.IP))
copy(*v, src.IPNet.IP)
return nil
default:
if nextDst, retry := GetAssignToDstType(dst); retry {

View File

@ -89,7 +89,8 @@ type Value interface {
// possible, then Get() returns Value.
Get() interface{}
// AssignTo converts and assigns the Value to dst.
// AssignTo converts and assigns the Value to dst. It MUST make a deep copy of
// any reference types.
AssignTo(dst interface{}) error
}