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.
This commit is contained in:
Hari Bhaskaran 2014-10-21 12:09:51 -07:00
parent 3beff78461
commit 161ec8db6d

View File

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