Skip to content

Commit

Permalink
fix anyMap.copy
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Apr 14, 2018
1 parent c8fa548 commit 82de886
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
13 changes: 6 additions & 7 deletions msgpack2any.nim
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,12 @@ proc hasKey*(node: MsgAny, key: MsgAny): bool =
assert(node.kind == msgMap)
result = node.mapVal.hasKey(key)

proc contains*(node: MsgAny, key: MsgAny): bool =
assert(node.kind == msgMap)
node.mapVal.hasKey(key)

proc contains*(node: MsgAny, val: MsgAny): bool =
assert(node.kind == msgArray)
find(node.arrayVal, val) >= 0
assert(node.kind in {msgMap, msgArray})
if node.kind == msgMap:
result = node.mapVal.hasKey(val)
else:
result = find(node.arrayVal, val) >= 0

proc `[]=`*(obj: MsgAny, key: MsgAny, val: MsgAny) {.inline.} =
assert(obj.kind == msgMap)
Expand Down Expand Up @@ -255,7 +254,7 @@ proc copy*(n: MsgAny): MsgAny =
of msgMap:
result = anyMap(n.mapVal.len)
for k, v in n:
result[k] = v
result[k.copy] = v.copy

proc toAny*(s: Stream): MsgAny =
let pos = s.getPosition()
Expand Down
14 changes: 14 additions & 0 deletions tests/test_any.nim
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,17 @@ test "float number":
for i in vv:
let x = s.toAny()
check x.float64Val == i.float64

test "map copy and `in` operator":
var a = anyMap()
a[anyString("abc")] = anyInt(123)
var b = a.copy
a[anyString("abc")] = anyString("hello")
check a[anyString("abc")] == anyString("hello")
check b[anyString("abc")] == anyInt(123)

# `in` operator
check anyString("abc") in a
check anyString("abc") in b
var c = anyArray(anyString("apple"))
check anyString("apple") in c

0 comments on commit 82de886

Please sign in to comment.