mashingan / anonimongo

Another Nim pure Mongo DB driver
MIT License
43 stars 5 forks source link

bson.encode() is not clear stream is called several times - is it correct? #15

Closed inv2004 closed 3 years ago

inv2004 commented 3 years ago

Example:

import anonimongo/core/bson
import strutils

proc main() =

  let bson0 = bson({"buf": [{"aaa": 1}, {"bbb": 2}]})
  let (s0, b0) = bson0.encode()
  echo toHex(b0)

  var bson1 = bson({"buf": []})
  bson1.mget("buf").add(bson({"aaa": 1}))
  let (_, _) = bson1.encode()    # if you remove the line - then b1 matches b0
  bson1.mget("buf").add(bson({"bbb": 2}))
  let (s1, b1) = bson1.encode()
  echo toHex(b1)

when isMainModule:
  main()
mashingan commented 3 years ago

Ah yes, I forgot to clear the stream after calling the mget . I relied on encoded to avoid re-encoding, but I forgot for this case. I'll fix later but if you know how to fix, PR is welcome. Please add a test suite in the tests/test_bson_test.nim too.