I receive different types of object from network, packed as msgpack. Now i need a construct that creates objects of the given type, to work with them further. I'm struggeling to create a proc/template that does this in one call. My goal is to get something like this:
var msgObj = unpack(msgObjEnvelope)
## msgObj is now of type MsgPing, MsgLog etc.
My data structures looks like this:
MessageType* {.pure.} = enum
MsgLog, MsgControlReq, MsgControlRes, MsgPing, MsgPong, MsgUntrusted
MessageEnvelope* = object
messageType*: MessageType # the remote tells me what kind of message was received.
msg*: string # the object of MsgPing/MsgLog/... as msgpack
MsgBase* = ref object of RootObj
version*: byte
# more stuff
MsgPing* = object of MsgBase
# more stuff
MsgLog* = object of MsgBase
# more stuff
any idea how i can do this? I could maybe solve this with Object variants, but i do not like that i cannot create obj attributes with the same name:
Node = ref object
case kind: NodeKind
of nkInt:
foo: int
baa: int
of nkFloat:
foo: int # <- this would not work and i dont like this, so i would stick with derived objects if possible
something: int
Does that not fit your needs?