Commit 95f42543 authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Kamil Kisiel

encoder: Fix struct encoding wrt protocol version

Similarly to dict, for struct encoding switch from protocol 1 opcodes
into always using protocol 0 opcodes, which is by the way 1 byte
shorter.

For the reference - for structs, unlike maps, the order of emitted keys
is well-defined - it is the order of fields as they are defined in the
struct. This way we can precisely test encoder output on structs with
more than 1 field.
parent bb7b117b
...@@ -466,7 +466,7 @@ func (e *Encoder) encodeStruct(st reflect.Value) error { ...@@ -466,7 +466,7 @@ func (e *Encoder) encodeStruct(st reflect.Value) error {
structTags := getStructTags(st) structTags := getStructTags(st)
err := e.emit(opEmptyDict, opMark) err := e.emit(opMark)
if err != nil { if err != nil {
return err return err
} }
...@@ -503,7 +503,7 @@ func (e *Encoder) encodeStruct(st reflect.Value) error { ...@@ -503,7 +503,7 @@ func (e *Encoder) encodeStruct(st reflect.Value) error {
} }
} }
return e.emit(opSetitems) return e.emit(opDict)
} }
func reflectValueOf(v interface{}) reflect.Value { func reflectValueOf(v interface{}) reflect.Value {
......
...@@ -331,7 +331,16 @@ var tests = []TestEntry{ ...@@ -331,7 +331,16 @@ var tests = []TestEntry{
[]interface{}{map[interface{}]interface{}{"Foo": "Qux", "Bar": int64(4)}}, []interface{}{map[interface{}]interface{}{"Foo": "Qux", "Bar": int64(4)}},
// MARK + STRING + INT + DICT + LIST // MARK + STRING + INT + DICT + LIST
I("((S\"Foo\"\nS\"Qux\"\nS\"Bar\"\nI4\ndl.")), P0("((S\"Foo\"\nS\"Qux\"\nS\"Bar\"\nI4\ndl."),
// MARK + SHORT_BINSTRING + BININT1 + DICT + LIST
P12("((U\x03FooU\x03QuxU\x03BarK\x04dl."),
// MARK + BINUNICODE + BININT1 + DICT + LIST
P3("((X\x03\x00\x00\x00FooX\x03\x00\x00\x00QuxX\x03\x00\x00\x00BarK\x04dl."),
// MARK + SHORT_BINUNICODE + BININT1 + DICT + LIST
P4_("((\x8c\x03Foo\x8c\x03Qux\x8c\x03BarK\x04dl.")),
} }
// foo is a type to test how encoder handles Go structs. // foo is a type to test how encoder handles Go structs.
......
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