From: Matt Joiner Date: Sun, 16 Nov 2014 19:52:37 +0000 (-0600) Subject: Fix CopyExact to work on a source string X-Git-Tag: v1.0.0~1539 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=dcac7db0f0b4a47d91e57be5f1723e1f4481b711;p=btrtrc.git Fix CopyExact to work on a source string --- diff --git a/util/copy.go b/util/copy.go index c7d64cc2..793a99cd 100644 --- a/util/copy.go +++ b/util/copy.go @@ -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())) diff --git a/util/copy_test.go b/util/copy_test.go index 450e5c4b..284840a3 100644 --- a/util/copy_test.go +++ b/util/copy_test.go @@ -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") +}