Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boost: encode/decode varchars for redis #17

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions go/sqltypes/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package sqltypes

import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -47,6 +48,8 @@ var (

// ErrIncompatibleTypeCast indicates a casting problem
ErrIncompatibleTypeCast = errors.New("Cannot convert value to desired type")

varcharBytes = []byte{'"', 'V', 'A', 'R', 'C', 'H', 'A', 'R', ':'}
)

// BinWriter interface is used for encoding values.
Expand Down Expand Up @@ -405,6 +408,8 @@ func (v Value) IsDateTime() bool {
// It's not a complete implementation.
func (v Value) MarshalJSON() ([]byte, error) {
switch {
case v.typ == VarChar:
return json.Marshal(string(append(varcharBytes[1:], v.val...)))
case v.IsQuoted() || v.typ == Bit:
return json.Marshal(v.ToString())
case v.typ == Null:
Expand All @@ -427,6 +432,11 @@ func (v *Value) UnmarshalJSON(b []byte) error {
err = json.Unmarshal(b, &ival)
val = ival
case '"':
if len(b) > 8 && bytes.Equal(b[0:len(varcharBytes)], varcharBytes) {
val = string(b[len(varcharBytes) : len(b)-1])
break
}

var bval []byte
err = json.Unmarshal(b, &bval)
val = bval
Expand Down
Loading