Skip to content

Commit

Permalink
Merge pull request #17 from vinted/vinted/zygis/proto-encoder-decoder…
Browse files Browse the repository at this point in the history
…-redis

boost: encode/decode varchars for redis
  • Loading branch information
DeathBorn authored Mar 28, 2024
2 parents 4e063e2 + 00375f8 commit 9385783
Showing 1 changed file with 10 additions and 0 deletions.
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

0 comments on commit 9385783

Please sign in to comment.