2014-08-21 16:24:19 +08:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CopyExact(dest interface{}, src interface{}) {
|
|
|
|
dV := reflect.ValueOf(dest)
|
|
|
|
sV := reflect.ValueOf(src)
|
|
|
|
if dV.Kind() == reflect.Ptr {
|
|
|
|
dV = dV.Elem()
|
|
|
|
}
|
|
|
|
if dV.Kind() == reflect.Array && !dV.CanAddr() {
|
|
|
|
panic(fmt.Sprintf("dest not addressable: %T", dest))
|
|
|
|
}
|
|
|
|
if sV.Kind() == reflect.String {
|
2014-11-17 03:52:37 +08:00
|
|
|
sV = sV.Convert(reflect.SliceOf(dV.Type().Elem()))
|
2014-08-21 16:24:19 +08:00
|
|
|
}
|
2014-11-18 07:43:41 +08:00
|
|
|
if !sV.IsValid() {
|
|
|
|
panic("invalid source, probably nil")
|
|
|
|
}
|
2014-08-21 16:24:19 +08:00
|
|
|
if dV.Len() != sV.Len() {
|
|
|
|
panic(fmt.Sprintf("dest len (%d) != src len (%d)", dV.Len(), sV.Len()))
|
|
|
|
}
|
|
|
|
if dV.Len() != reflect.Copy(dV, sV) {
|
|
|
|
panic("dammit")
|
|
|
|
}
|
|
|
|
}
|