From 161ec8db6d2b5cc2cf5c650fbd14a7f3d542cacd Mon Sep 17 00:00:00 2001 From: Hari Bhaskaran Date: Tue, 21 Oct 2014 12:09:51 -0700 Subject: [PATCH] Support writing to postgres 'text' values from both strings as well as []byte. If the input is already []byte, this will avoid having the caller convert to string and then back to []byte. Potentially saves some allocs. --- values.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/values.go b/values.go index 8e670187..0641d33f 100644 --- a/values.go +++ b/values.go @@ -986,14 +986,17 @@ func decodeText(vr *ValueReader) string { } func encodeText(w *WriteBuf, value interface{}) error { - s, ok := value.(string) - if !ok { + switch t := value.(type) { + case string: + w.WriteInt32(int32(len(t))) + w.WriteBytes([]byte(t)) + case []byte: + w.WriteInt32(int32(len(t))) + w.WriteBytes(t) + default: return fmt.Errorf("Expected string, received %T", value) } - w.WriteInt32(int32(len(s))) - w.WriteBytes([]byte(s)) - return nil }