Fix CopyExact to work on a source string
This commit is contained in:
parent
3489a60154
commit
dcac7db0f0
|
@ -15,7 +15,7 @@ func CopyExact(dest interface{}, src interface{}) {
|
|||
panic(fmt.Sprintf("dest not addressable: %T", dest))
|
||||
}
|
||||
if sV.Kind() == reflect.String {
|
||||
sV = sV.Convert(dV.Type())
|
||||
sV = sV.Convert(reflect.SliceOf(dV.Type().Elem()))
|
||||
}
|
||||
if dV.Len() != sV.Len() {
|
||||
panic(fmt.Sprintf("dest len (%d) != src len (%d)", dV.Len(), sV.Len()))
|
||||
|
|
|
@ -44,3 +44,18 @@ func TestCopyLenMismatch(t *testing.T) {
|
|||
}()
|
||||
CopyExact(make([]byte, 2), "abc")
|
||||
}
|
||||
|
||||
func TestCopySrcString(t *testing.T) {
|
||||
dest := make([]byte, 3)
|
||||
CopyExact(dest, "lol")
|
||||
if string(dest) != "lol" {
|
||||
t.FailNow()
|
||||
}
|
||||
defer func() {
|
||||
r := recover()
|
||||
if r == nil {
|
||||
t.FailNow()
|
||||
}
|
||||
}()
|
||||
CopyExact(dest, "rofl")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue