Skip to content

Commit

Permalink
msgpack: add wrappers for EncodeUint/EncodeInt
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-jukovec committed Aug 4, 2022
1 parent 921b893 commit fa5d40f
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 45 deletions.
16 changes: 8 additions & 8 deletions client_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type IntKey struct {

func (k IntKey) EncodeMsgpack(enc *encoder) error {
enc.EncodeArrayLen(1)
enc.EncodeInt(k.I)
encodeInt(enc, int64(k.I))
return nil
}

Expand All @@ -20,7 +20,7 @@ type UintKey struct {

func (k UintKey) EncodeMsgpack(enc *encoder) error {
enc.EncodeArrayLen(1)
enc.EncodeUint(k.I)
encodeUint(enc, uint64(k.I))
return nil
}

Expand All @@ -44,8 +44,8 @@ type IntIntKey struct {

func (k IntIntKey) EncodeMsgpack(enc *encoder) error {
enc.EncodeArrayLen(2)
enc.EncodeInt(k.I1)
enc.EncodeInt(k.I2)
encodeInt(enc, int64(k.I1))
encodeInt(enc, int64(k.I2))
return nil
}

Expand All @@ -59,7 +59,7 @@ type Op struct {
func (o Op) EncodeMsgpack(enc *encoder) error {
enc.EncodeArrayLen(3)
enc.EncodeString(o.Op)
enc.EncodeInt(o.Field)
encodeInt(enc, int64(o.Field))
return enc.Encode(o.Arg)
}

Expand Down Expand Up @@ -147,9 +147,9 @@ type OpSplice struct {
func (o OpSplice) EncodeMsgpack(enc *encoder) error {
enc.EncodeArrayLen(5)
enc.EncodeString(o.Op)
enc.EncodeInt(o.Field)
enc.EncodeInt(o.Pos)
enc.EncodeInt(o.Len)
encodeInt(enc, int64(o.Field))
encodeInt(enc, int64(o.Pos))
encodeInt(enc, int64(o.Len))
enc.EncodeString(o.Replace)
return nil
}
2 changes: 1 addition & 1 deletion datetime/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (c *Tuple2) EncodeMsgpack(e *encoder) error {
if err := e.EncodeArrayLen(3); err != nil {
return err
}
if err := e.EncodeUint(c.Cid); err != nil {
if err := encodeUint(e, uint64(c.Cid)); err != nil {
return err
}
if err := e.EncodeString(c.Orig); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions datetime/msgpack_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
type encoder = msgpack.Encoder
type decoder = msgpack.Decoder

func encodeUint(e *encoder, v uint64) error {
return e.EncodeUint(uint(v))
}

func marshal(v interface{}) ([]byte, error) {
return msgpack.Marshal(v)
}
Expand Down
2 changes: 1 addition & 1 deletion example_custom_unpacking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *Tuple2) EncodeMsgpack(e *encoder) error {
if err := e.EncodeArrayLen(3); err != nil {
return err
}
if err := e.EncodeUint(c.Cid); err != nil {
if err := encodeUint(e, uint64(c.Cid)); err != nil {
return err
}
if err := e.EncodeString(c.Orig); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions msgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ func newDecoder(r io.Reader) *decoder {
return msgpack.NewDecoder(r)
}

func encodeUint(e *encoder, v uint64) error {
return e.EncodeUint(uint(v))
}

func encodeInt(e *encoder, v int64) error {
return e.EncodeInt(int(v))
}

func msgpackIsUint(code byte) bool {
return code == msgpcode.Uint8 || code == msgpcode.Uint16 ||
code == msgpcode.Uint32 || code == msgpcode.Uint64 ||
Expand Down
4 changes: 4 additions & 0 deletions msgpack_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ import (

type encoder = msgpack.Encoder
type decoder = msgpack.Decoder

func encodeUint(e *encoder, v uint64) error {
return e.EncodeUint(uint(v))
}
12 changes: 6 additions & 6 deletions prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ type Prepared struct {

func fillPrepare(enc *encoder, expr string) error {
enc.EncodeMapLen(1)
enc.EncodeUint(KeySQLText)
encodeUint(enc, KeySQLText)
return enc.EncodeString(expr)
}

func fillUnprepare(enc *encoder, stmt Prepared) error {
enc.EncodeMapLen(1)
enc.EncodeUint(KeyStmtID)
return enc.EncodeUint(uint(stmt.StatementID))
encodeUint(enc, KeyStmtID)
return encodeUint(enc, uint64(stmt.StatementID))
}

func fillExecutePrepared(enc *encoder, stmt Prepared, args interface{}) error {
enc.EncodeMapLen(2)
enc.EncodeUint(KeyStmtID)
enc.EncodeUint(uint(stmt.StatementID))
enc.EncodeUint(KeySQLBind)
encodeUint(enc, KeyStmtID)
encodeUint(enc, uint64(stmt.StatementID))
encodeUint(enc, KeySQLBind)
return encodeSQLBind(enc, args)
}

Expand Down
50 changes: 25 additions & 25 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ import (
)

func fillSearch(enc *encoder, spaceNo, indexNo uint32, key interface{}) error {
enc.EncodeUint(KeySpaceNo)
enc.EncodeUint(uint(spaceNo))
enc.EncodeUint(KeyIndexNo)
enc.EncodeUint(uint(indexNo))
enc.EncodeUint(KeyKey)
encodeUint(enc, KeySpaceNo)
encodeUint(enc, uint64(spaceNo))
encodeUint(enc, KeyIndexNo)
encodeUint(enc, uint64(indexNo))
encodeUint(enc, KeyKey)
return enc.Encode(key)
}

func fillIterator(enc *encoder, offset, limit, iterator uint32) {
enc.EncodeUint(KeyIterator)
enc.EncodeUint(uint(iterator))
enc.EncodeUint(KeyOffset)
enc.EncodeUint(uint(offset))
enc.EncodeUint(KeyLimit)
enc.EncodeUint(uint(limit))
encodeUint(enc, KeyIterator)
encodeUint(enc, uint64(iterator))
encodeUint(enc, KeyOffset)
encodeUint(enc, uint64(offset))
encodeUint(enc, KeyLimit)
encodeUint(enc, uint64(limit))
}

func fillInsert(enc *encoder, spaceNo uint32, tuple interface{}) error {
enc.EncodeMapLen(2)
enc.EncodeUint(KeySpaceNo)
enc.EncodeUint(uint(spaceNo))
enc.EncodeUint(KeyTuple)
encodeUint(enc, KeySpaceNo)
encodeUint(enc, uint64(spaceNo))
encodeUint(enc, KeyTuple)
return enc.Encode(tuple)
}

Expand All @@ -45,19 +45,19 @@ func fillUpdate(enc *encoder, spaceNo, indexNo uint32, key, ops interface{}) err
if err := fillSearch(enc, spaceNo, indexNo, key); err != nil {
return err
}
enc.EncodeUint(KeyTuple)
encodeUint(enc, KeyTuple)
return enc.Encode(ops)
}

func fillUpsert(enc *encoder, spaceNo uint32, tuple, ops interface{}) error {
enc.EncodeMapLen(3)
enc.EncodeUint(KeySpaceNo)
enc.EncodeUint(uint(spaceNo))
enc.EncodeUint(KeyTuple)
encodeUint(enc, KeySpaceNo)
encodeUint(enc, uint64(spaceNo))
encodeUint(enc, KeyTuple)
if err := enc.Encode(tuple); err != nil {
return err
}
enc.EncodeUint(KeyDefTuple)
encodeUint(enc, KeyDefTuple)
return enc.Encode(ops)
}

Expand All @@ -68,25 +68,25 @@ func fillDelete(enc *encoder, spaceNo, indexNo uint32, key interface{}) error {

func fillCall(enc *encoder, functionName string, args interface{}) error {
enc.EncodeMapLen(2)
enc.EncodeUint(KeyFunctionName)
encodeUint(enc, KeyFunctionName)
enc.EncodeString(functionName)
enc.EncodeUint(KeyTuple)
encodeUint(enc, KeyTuple)
return enc.Encode(args)
}

func fillEval(enc *encoder, expr string, args interface{}) error {
enc.EncodeMapLen(2)
enc.EncodeUint(KeyExpression)
encodeUint(enc, KeyExpression)
enc.EncodeString(expr)
enc.EncodeUint(KeyTuple)
encodeUint(enc, KeyTuple)
return enc.Encode(args)
}

func fillExecute(enc *encoder, expr string, args interface{}) error {
enc.EncodeMapLen(2)
enc.EncodeUint(KeySQLText)
encodeUint(enc, KeySQLText)
enc.EncodeString(expr)
enc.EncodeUint(KeySQLBind)
encodeUint(enc, KeySQLBind)
return encodeSQLBind(enc, args)
}

Expand Down
6 changes: 3 additions & 3 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func fillBegin(enc *encoder, txnIsolation TxnIsolationLevel, timeout time.Durati
}

if hasTimeout {
err = enc.EncodeUint(KeyTimeout)
err = encodeUint(enc, KeyTimeout)
if err != nil {
return err
}
Expand All @@ -56,12 +56,12 @@ func fillBegin(enc *encoder, txnIsolation TxnIsolationLevel, timeout time.Durati
}

if hasIsolationLevel {
err = enc.EncodeUint(KeyTxnIsolation)
err = encodeUint(enc, KeyTxnIsolation)
if err != nil {
return err
}

err = enc.Encode(txnIsolation)
err = encodeUint(enc, uint64(txnIsolation))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (m *Member) EncodeMsgpack(e *encoder) error {
if err := e.EncodeString(m.Name); err != nil {
return err
}
if err := e.EncodeUint(m.Val); err != nil {
if err := encodeUint(e, uint64(m.Val)); err != nil {
return err
}
return nil
Expand Down

0 comments on commit fa5d40f

Please sign in to comment.