You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Observed:
Object variants using standard idiom (with case) packs correctly, but fails to unpack.
Expected:
Object variants with zero-initialized variables should unpack correctly
Steps to Replicate:
import streams, msgpack4nim
type MsgType* = enum
MSG_FOO,
MSG_BAR
type Foo* = object
name: string
type Bar* = object
price: int
type FooBarMsg* = object
case msgType*: MsgType
of MSG_FOO:
foo*: Foo
of MSG_BAR:
bar*: Bar
when isMainModule:
var fooOriginal = Foo(name: "Baz")
var barOriginal = Bar(price: 11)
var outboundMsgFoo = FooBarMsg(
msgType: MSG_FOO,
foo: fooOriginal
)
var outboundMsgBar = FooBarMsg(
msgType: MSG_BAR,
bar: barOriginal
)
var sFoo = MsgStream.init()
sFoo.pack(outboundMsgFoo)
var sBar = MsgStream.init()
sBar.pack(outboundMsgBar)
var incomingFoo: FooBarMsg
var sFooReception = MsgStream.init(sFoo.data)
try:
sFooReception.unpack(incomingFoo)
except:
echo "unpack error for foo"
var incomingBar: FooBarMsg
var sBarReception = MsgStream.init(sBar.data)
try:
sBarReception.unpack(incomingBar)
except:
echo "unpack error for bar"
The text was updated successfully, but these errors were encountered:
thank you for reporting this. case object/object variants is always an interesting subject for serializer library. it is tricky to do it properly automatically.
work-around: you can provide a custom deserializer for your special object, e.g:
procunpack_type*[ByteStream](s: ByteStream, x: varFooBarMsg) =let numFields = s.unpack_array() # or unpack_map if you use OBJ_TO_MAP versionlet kind =MsgType(s.unpack_imp_int64())
x =FooBarMsg(kind: kind)
case kind:
ofMSG_FOO : s.unpack(x.foo)
ofMSG_BAR : s.unpack(x.bar)
EDIT: you can use stringify to debug the msgpack binary data.
@jangko thank you very much for the workaround suggestion. That works.
I agree that object variant handling is tough to implement correctly and generally, so please feel free to close the issue if you feel that it makes sense to do so.
Version:
0.2.9
Commit:
9c4bc6e66d5d193692be0e7c5605166dc61756dc
Observed:
Object variants using standard idiom (with
case
) packs correctly, but fails to unpack.Expected:
Object variants with zero-initialized variables should unpack correctly
Steps to Replicate:
The text was updated successfully, but these errors were encountered: