jangko / msgpack4nim

MessagePack serializer/deserializer implementation for Nim / msgpack.org[Nim]
http://msgpack.org/
119 stars 21 forks source link

object variant unpack #4

Closed leemorgan-mobile closed 8 years ago

leemorgan-mobile commented 8 years ago
import msgpack
type
  NodeKind = enum # the different node types
    nkInt, #a leaf with an integer value
    nkFloat, #a leaf with a float value
    nkString, #a leaf with a string value
    nkAdd, #an addition
    nkSub, #a subtraction
    nkIf #an if statement
  Node = ref NodeObj
  NodeObj = object
    case kind: NodeKind # the kind field is the discriminator
    of nkInt: intVal: int
    of nkFloat: floatVal: float
    of nkString: strVal: string
    of nkAdd, nkSub:
      leftOp, rightOp: Node
    of nkIf:
      condition, thenPart, elsePart: Node
var
  aUnion = Node(kind:nkInt, intVal:22)
  anotherUnion = Node(kind:nkInt, intVal:22)
  s = pack(aUnion)

unpack(s, anotherUnion)
echo anotherUnion
...
type mismatch: got (Stream, NodeKind)
but expected one of: 
msgpack.unpack(data: string, val: var T)
msgpack.unpack(s: Stream, val: var T)
jangko commented 8 years ago

sorry for late reply. problem fixed. thanks for being patient with me