Fix CopyExact to work on a source string

This commit is contained in:
Matt Joiner 2014-11-16 13:52:37 -06:00
parent 3489a60154
commit dcac7db0f0
2 changed files with 16 additions and 1 deletions

View File

@ -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()))

View File

@ -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")
}