Commit d3cae883 authored by Levin Zimmermann's avatar Levin Zimmermann

proto: Fix Error -> ErrorCode is int on the wire and not extension type

parent d4317253
......@@ -1095,35 +1095,6 @@ func (d *decoderM) expectEnum(assignto string, enumType int) {
}
func (d *decoderM) genBasic(assignto string, typ *types.Basic, userType types.Type) {
// zodb.Tid and zodb.Oid are encoded as [8]bin or Nil
if userType == zodbTid || userType == zodbOid {
d.emit("if data[%v] == byte(msgpack.Nil) {", d.n)
d.emit("%s = %s(%v)", assignto, typeName(userType), INVALID_ID)
d.emit("data = data[%v:]", d.n + 1)
d.emit("} else {")
d.expectBin8Fix(assignto, 8)
d.emit("%s= %s(binary.BigEndian.Uint64(data[%v:]))", assignto, typeName(userType), d.n)
d.n += 8
d.overflow.Add(8)
d.resetPos()
d.emit("}")
return
}
// enums are encoded as `fixext1 enumType fixint<value>`
if enum, ok := enumRegistry[userType]; ok {
d.expectEnum(assignto, enum)
d.emit("{")
d.emit("v := data[%v]", d.n); d.n++
d.emit("if !(0 <= v && v <= 0x7f) {") // mposfixint
d.emit(" return 0, mdecodeEnumValueErr(%q, v)", d.pathName(assignto))
d.emit("}")
d.emit("%s= %s(v)", assignto, typeName(userType))
d.emit("}")
d.overflow.Add(1)
return
}
// v represents basic decoded value casted to user type if needed
v := "v"
if userType.Underlying() != userType {
......@@ -1185,6 +1156,40 @@ func (d *decoderM) genBasic(assignto string, typ *types.Basic, userType types.Ty
d.emit("}")
}
// zodb.Tid and zodb.Oid are encoded as [8]bin or Nil
if userType == zodbTid || userType == zodbOid {
d.emit("if data[%v] == byte(msgpack.Nil) {", d.n)
d.emit("%s = %s(%v)", assignto, typeName(userType), INVALID_ID)
d.emit("data = data[%v:]", d.n + 1)
d.emit("} else {")
d.expectBin8Fix(assignto, 8)
d.emit("%s= %s(binary.BigEndian.Uint64(data[%v:]))", assignto, typeName(userType), d.n)
d.n += 8
d.overflow.Add(8)
d.resetPos()
d.emit("}")
return
}
if typeName(userType) == "ErrorCode" {
mgetint("", 32, "")
return
}
// enums are encoded as `fixext1 enumType fixint<value>`
if enum, ok := enumRegistry[userType]; ok {
d.expectEnum(assignto, enum)
d.emit("{")
d.emit("v := data[%v]", d.n); d.n++
d.emit("if !(0 <= v && v <= 0x7f) {") // mposfixint
d.emit(" return 0, mdecodeEnumValueErr(%q, v)", d.pathName(assignto))
d.emit("}")
d.emit("%s= %s(v)", assignto, typeName(userType))
d.emit("}")
d.overflow.Add(1)
return
}
// IdTime can be nil ('None' in py), in this case we use
// infinite -1, see
// https://lab.nexedi.com/kirr/neo/-/blob/1ad088c8/go/neo/proto/proto.go#L352-357
......
......@@ -85,7 +85,7 @@ func (p *Error) neoMsgEncodeM(data []byte) {
func (p *Error) neoMsgDecodeM(data []byte) (int, error) {
var nread uint64
if len(data) < 4 {
if len(data) < 1 {
goto overflow
}
if op, opOk := msgpack.Op(data[0]), msgpack.FixArray_4|2; op != opOk {
......@@ -98,20 +98,15 @@ func (p *Error) neoMsgDecodeM(data []byte) (int, error) {
}
data = tail
}
if op := msgpack.Op(data[0]); op != msgpack.FixExt1 {
return 0, mdecodeOpErr("Error.Code", op, msgpack.FixExt1)
}
if enumType := data[1]; enumType != 2 {
return 0, mdecodeEnumTypeErr("Error.Code", enumType, 2)
}
{
v := data[2]
if !(0 <= v && v <= 0x7f) {
return 0, mdecodeEnumValueErr("Error.Code", v)
v, tail, err := msgp.ReadInt32Bytes(data)
if err != nil {
return 0, mdecodeErr("Error.Code", err)
}
p.Code = ErrorCode(v)
nread += uint64(len(data) - len(tail))
data = tail
}
data = data[3:]
{
b, tail, err := msgp.ReadBytesZC(data)
if err != nil {
......@@ -121,7 +116,7 @@ func (p *Error) neoMsgDecodeM(data []byte) (int, error) {
nread += uint64(len(data) - len(tail))
data = tail
}
return 4 + int(nread), nil
return 1 + int(nread), nil
overflow:
return 0, ErrDecodeOverflow
......
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