From fa5d40f0137ecea52e616fb7ca2b09112e65dca2 Mon Sep 17 00:00:00 2001 From: Oleg Jukovec Date: Wed, 18 May 2022 17:37:21 +0300 Subject: [PATCH] msgpack: add wrappers for EncodeUint/EncodeInt EncodeUint and EncodeInt have different argument types in msgpack.v2[1][2] and msgpack.v5[3][4]. The wrappers will simplify migration to msgpack.v5. 1. https://pkg.go.dev/github.com/vmihailenco/msgpack@v2.9.2+incompatible#Encoder.EncodeUint 2. https://pkg.go.dev/github.com/vmihailenco/msgpack@v2.9.2+incompatible#Encoder.EncodeInt 3. https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#Encoder.EncodeUint 4. https://pkg.go.dev/github.com/vmihailenco/msgpack/v5#Encoder.EncodeInt Part of #124 --- client_tools.go | 16 +++++----- datetime/datetime_test.go | 2 +- datetime/msgpack_helper_test.go | 4 +++ example_custom_unpacking_test.go | 2 +- msgpack.go | 8 +++++ msgpack_helper_test.go | 4 +++ prepared.go | 12 ++++---- request.go | 50 ++++++++++++++++---------------- stream.go | 6 ++-- tarantool_test.go | 2 +- 10 files changed, 61 insertions(+), 45 deletions(-) diff --git a/client_tools.go b/client_tools.go index 88da9c577..a5d6fbba9 100644 --- a/client_tools.go +++ b/client_tools.go @@ -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 } @@ -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 } @@ -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 } @@ -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) } @@ -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 } diff --git a/datetime/datetime_test.go b/datetime/datetime_test.go index 035b1025b..176f8a40c 100644 --- a/datetime/datetime_test.go +++ b/datetime/datetime_test.go @@ -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 { diff --git a/datetime/msgpack_helper_test.go b/datetime/msgpack_helper_test.go index ba283db18..7b045a863 100644 --- a/datetime/msgpack_helper_test.go +++ b/datetime/msgpack_helper_test.go @@ -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) } diff --git a/example_custom_unpacking_test.go b/example_custom_unpacking_test.go index cff50da50..4087c5620 100644 --- a/example_custom_unpacking_test.go +++ b/example_custom_unpacking_test.go @@ -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 { diff --git a/msgpack.go b/msgpack.go index bcdcc06ce..e48e14a21 100644 --- a/msgpack.go +++ b/msgpack.go @@ -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 || diff --git a/msgpack_helper_test.go b/msgpack_helper_test.go index 8623059e5..fcadbc573 100644 --- a/msgpack_helper_test.go +++ b/msgpack_helper_test.go @@ -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)) +} diff --git a/prepared.go b/prepared.go index 91c5238a7..6f63e3bb1 100644 --- a/prepared.go +++ b/prepared.go @@ -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) } diff --git a/request.go b/request.go index 625380306..cfa40e522 100644 --- a/request.go +++ b/request.go @@ -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) } @@ -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) } @@ -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) } diff --git a/stream.go b/stream.go index 009f6c0aa..3a03ec68f 100644 --- a/stream.go +++ b/stream.go @@ -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 } @@ -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 } diff --git a/tarantool_test.go b/tarantool_test.go index ea0bc784d..18ebb0506 100644 --- a/tarantool_test.go +++ b/tarantool_test.go @@ -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