Commits

Liam Staskawicz committed e37a57a

* fix bson CodeWScope and test it

Comments (0)

Files changed (3)

fan/bson/BsonReader.fan

     name := readCStr(ins)
     totallen := ins.readS4
     Buf? val
-    switch (ins.read) // type
-    {
+    switch (ins.read) { // type
       case Bson.BIN_LENGTH:
         len := ins.readS4
         if (len + 4 != totallen)
   {
     name := readCStr(ins)
     totalsize := ins.readS4
+    
     code := ins.readChars(ins.readS4 - 1)
-    ins.skip(1) // eat the null terminator...worth validating that this is 0?
+    ins.skip(1) // eat null terminator
+    
     scope := Str:Obj?[:] { ordered = true }
     readObject(ins, scope)
     bson[name] = Code(code, scope)
-    return remaining - (name.size + 1 + totalsize)
+    return remaining - (name.size + 1 + totalsize + 4 + 4) // 4 - totalsize, 4 - strsize
   }
   
   static private Int readMinKey(InStream ins, Int remaining, Str:Obj? bson)

fan/bson/BsonWriter.fan

   {
     b.write(Bson.CODE_W_SCOPE)
     writeCStr(b.out, key)
-
-    writeObject(b, cws.scope)
-    b.writeI4(8 + cws.code.size + 1 + b.size)   // total size
-    b.writeI4(cws.code.size + 1)                // size of code str
-    writeCStr(b.out, cws.code)                  // code str
-    b.writeBuf(b.seek(0))                       // scope
+    
+    tmp := b.pos
+    b.writeI4(0) // placeholder for total len
+    b.writeI4(cws.code.size + 1)     // size of code string
+    b.writeChars(cws.code).write(0)  // code string
+    writeObject(b, cws.scope)        // scope as document
+    b.seek(tmp).writeI4(b.size - tmp).seek(b.size)
   }
   
   private static Void writeMinKey(Buf b, Str key)
   }
   
 }
-
-

test/BsonTest.fan

     list.add(1).add(2).add(3).add(4)
     map["testList"] = list
     Bson.write(b.out, map)
-    m := Bson.read(b.flip().in)
+    m := Bson.read(b.flip.in)
     verify(m == map, "${m} != ${map}")
   }
   
     e["double"] = ee
     map["testMap"] = e
     Bson.write(b.out, map)
-    m := Bson.read(b.flip().in)
-    verify(m == map, "${m} != ${map}")
+    m := Bson.read(b.flip.in)
+    verifyEq(m, map)
+  }
+  
+  Void testCodeWScope()
+  {
+    cleanSlate
+    c1 := Code("function(test, obj) {obj = 0;}", ["test":1])
+    map["testCode"] = c1
+    Bson.write(b.out, map)
+    m := Bson.read(b.flip.in)
+    Code c2 := m["testCode"]
+    verifyEq(c1.code, c2.code)
+    verifyEq(c1.scope["test"], c2.scope["test"])
   }
   
 }