Commit 24695efa authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Kamil Kisiel

encoder: Fix tuple wrt protocol version

- we can use EMPTY_TUPLE only if protocol >= 1
- also: if protocol >= 2 we can now use TUPLE{1,2,3} opcodes.
parent 08fb378e
......@@ -165,16 +165,35 @@ func (e *Encoder) encode(rv reflect.Value) error {
func (e *Encoder) encodeTuple(t Tuple) error {
l := len(t)
switch l {
case 0:
return e.emit(opEmptyTuple)
// protocol >= 2: [1-3]() -> TUPLE{1-3}
if e.config.Protocol >= 2 && (1 <= l && l <= 3) {
for i := range t {
err := e.encode(reflectValueOf(t[i]))
if err != nil {
return err
}
}
// TODO this are protocol 2 opcodes - check e.protocol before using them
//case 1:
//case 2:
//case 3:
var op byte
switch l {
case 1:
op = opTuple1
case 2:
op = opTuple2
case 3:
op = opTuple3
}
return e.emit(op)
}
// protocol >= 1: ø tuple -> EMPTY_TUPLE
if e.config.Protocol >= 1 && l == 0 {
return e.emit(opEmptyTuple)
}
// general case: MARK ... TUPLE
// TODO detect cycles and double references to the same object
err := e.emit(opMark)
if err != nil {
return err
......
......@@ -176,19 +176,31 @@ var tests = []TestEntry{
//I("\x8b\x09\x00\x00\x00\xffm\xa1b\x86\xce\xfd\xaa\x00.")), // LONG4 TODO
X("tuple()", Tuple{},
I("(t.")), // MARK + TUPLE
P0("(t."), // MARK + TUPLE
P1_(").")), // EMPTY_TUPLE
X("tuple((1,))", Tuple{int64(1)},
I("I1\n\x85.")), // TUPLE1 + INT
P0("(I1\nt."), // MARK + TUPLE + INT
P1("(K\x01t."), // MARK + TUPLE + BININT1
P2_("K\x01\x85."), // TUPLE1 + BININT1
I("I1\n\x85.")), // TUPLE1 + INT
X("tuple((1,2))", Tuple{int64(1), int64(2)},
I("(I1\nI2\ntp0\n."), // MARK + TUPLE + INT
I("I1\nI2\n\x86.")), // TUPLE2 + INT
P0("(I1\nI2\nt."), // MARK + TUPLE + INT
P1("(K\x01K\x02t."), // MARK + TUPLE + BININT1
P2_("K\x01K\x02\x86."), // TUPLE2 + BININT1
I("I1\nI2\n\x86.")), // TUPLE2 + INT
X("tuple((1,2,3))", Tuple{int64(1), int64(2), int64(3)},
I("I1\nI2\nI3\n\x87.")), // TUPLE3 + INT
P0("(I1\nI2\nI3\nt."), // MARK + TUPLE + INT
P1("(K\x01K\x02K\x03t."), // MARK + TUPLE + BININT1
P2_("K\x01K\x02K\x03\x87."), // TUPLE3 + BININT1
I("I1\nI2\nI3\n\x87.")), // TUPLE3 + INT
X("tuple(((1,2), (3,4)))", Tuple{Tuple{int64(1), int64(2)}, Tuple{int64(3), int64(4)}},
P0("((I1\nI2\nt(I3\nI4\ntt."), // MARK + INT + TUPLE
P1("((K\x01K\x02t(K\x03K\x04tt."), // MARK + BININT1 + TUPLE
P2_("K\x01K\x02\x86K\x03K\x04\x86\x86."), // BININT1 + TUPLE2
I("((I1\nI2\ntp0\n(I3\nI4\ntp1\ntp2\n.")),
X("list([])", []interface{}{},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment