]> Sergey Matveev's repositories - btrtrc.git/blob - util/copy.go
util/loghttp: Don't implicitly expose pprof
[btrtrc.git] / util / copy.go
1 package util
2
3 import (
4         "fmt"
5         "reflect"
6 )
7
8 func CopyExact(dest interface{}, src interface{}) {
9         dV := reflect.ValueOf(dest)
10         sV := reflect.ValueOf(src)
11         if dV.Kind() == reflect.Ptr {
12                 dV = dV.Elem()
13         }
14         if dV.Kind() == reflect.Array && !dV.CanAddr() {
15                 panic(fmt.Sprintf("dest not addressable: %T", dest))
16         }
17         if sV.Kind() == reflect.String {
18                 sV = sV.Convert(reflect.SliceOf(dV.Type().Elem()))
19         }
20         if !sV.IsValid() {
21                 panic("invalid source, probably nil")
22         }
23         if dV.Len() != sV.Len() {
24                 panic(fmt.Sprintf("dest len (%d) != src len (%d)", dV.Len(), sV.Len()))
25         }
26         if dV.Len() != reflect.Copy(dV, sV) {
27                 panic("dammit")
28         }
29 }