using Ejdb.BSON;
using Ejdb.Utils;
using System.Reflection;
+using System.Linq;
namespace Ejdb.BSON {
}
public override string ToString() {
- return string.Format("[{0}: {1}]", GetType().Name, _fieldslist);
+ return string.Format("[{0}: {1}]", GetType().Name,
+ string.Join(", ", from bv in _fieldslist select bv.ToString()));
}
//.//////////////////////////////////////////////////////////////////
// Private staff
public class BSONIterator : IDisposable, IEnumerable<BSONType> {
ExtBinaryReader _input;
+ bool _closeOnDispose = true;
+ bool _disposed;
int _doclen;
BSONType _ctype = BSONType.UNKNOWN;
string _entryKey;
public bool Disposed {
get {
- return (_input == null);
+ return _disposed;
}
}
}
public void Dispose() {
- if (_input != null) {
+ _disposed = true;
+ if (_closeOnDispose && _input != null) {
_input.Close();
_input = null;
}
return GetEnumerator();
}
+ public IEnumerable<BSONValue> Values() {
+ while (Next() != BSONType.EOO) {
+ yield return FetchCurrentValue();
+ }
+
+ }
+
public BSONType Next() {
CheckDisposed();
if (_ctype == BSONType.EOO) {
{
BSONDocument doc = (_ctype == BSONType.OBJECT ? new BSONDocument() : new BSONArray());
BSONIterator sit = new BSONIterator(this._input, _entryLen + 4);
+ sit._closeOnDispose = false;
while (sit.Next() != BSONType.EOO) {
doc.Add(sit.FetchCurrentValue());
}
}
public void WriteCString(string val) {
- Write(_encoding.GetBytes(val));
+ if (val.Length > 0) {
+ Write(_encoding.GetBytes(val));
+ }
Write((byte) 0x00);
}
}
}
[Test]
- public void testIterate2() {
+ public void TestIterate2() {
var doc = new BSONDocument();
doc["a"] = "av";
doc["b"] = BSONDocument.ValueOf(new{cc = 1});
}
++c;
}
+ bool thrown = false;
Assert.IsTrue(it.Disposed);
+ try {
+ it.Next();
+ } catch (ObjectDisposedException) {
+ thrown = true;
+ }
+ Assert.IsTrue(thrown);
+ c = 0;
+ it = new BSONIterator(doc);
+ foreach (var bv in it.Values()) {
+ if (c == 0) {
+ Assert.AreEqual("a", bv.Key);
+ Assert.AreEqual("av", bv.Value);
+ }
+ if (c == 1) {
+ Assert.AreEqual("b", bv.Key);
+ BSONDocument sdoc = bv.Value as BSONDocument;
+ Assert.IsNotNull(sdoc);
+ foreach (var bv2 in new BSONIterator(sdoc).Values()) {
+ Assert.AreEqual("cc", bv2.Key);
+ Assert.AreEqual(1, bv2.Value);
+ Assert.AreEqual(BSONType.INT, bv2.BSONType);
+ }
+ }
+ if (c == 2) {
+ Assert.AreEqual(BSONType.OID, bv.BSONType);
+ Assert.IsInstanceOfType(typeof(BSONOid), bv.Value);
+ var oid = bv.Value as BSONOid;
+ Assert.AreEqual("51b9f3af98195c4600000000", oid.ToString());
+ }
+ c++;
+ }
+ }
-
-
+ [Test]
+ public void TestIterateRE() {
+ var doc = new BSONDocument();
+ doc["a"] = new BSONRegexp("b", "c");
+ doc["d"] = 1;
+ doc["e"] = BSONDocument.ValueOf(new {f = new BSONRegexp("g", "")});
+ doc["h"] = 2;
+ //28-00-00-00
+ //0B-61-00-62-00-63-00
+ //10-64-00-01-00-00-00
+ //03-65-00-0B-00-00-00
+ //0B-66-00-67-00-00-00
+ //10-68-00-02-00-00-00-00
+ var cs = "";
+ foreach (var bt in new BSONIterator(doc)) {
+ cs += bt.ToString();
+ }
+ Assert.AreEqual("REGEXINTOBJECTINT", cs);
+ cs = "";
+ foreach (var bv in new BSONIterator(doc).Values()) {
+ if (bv.Key == "a") {
+ cs += ((BSONRegexp) bv.Value).Re;
+ cs += ((BSONRegexp) bv.Value).Opts;
+ } else {
+ cs += bv.Value;
+ }
+ }
+ Assert.AreEqual("bc1[BSONDocument: [BSONValue: BSONType=REGEX, Key=f, Value=[BSONRegexp: re=g, opts=]]]2", cs);
}
}
}
<MonoDevelop.Ide.Workbench ActiveDocument="Ejdb.Tests/TestBSON.cs">
<Files>
<File FileName="Ejdb.BSON/BSONArray.cs" Line="12" Column="61" />
- <File FileName="Ejdb.BSON/InvalidBSONDataException.cs" Line="30" Column="1" />
<File FileName="Ejdb.JSON/JSONReader.cs" Line="33" Column="4" />
- <File FileName="Ejdb.JSON/JSONElement.cs" Line="26" Column="1" />
- <File FileName="Ejdb.BSON/BSONDocument.cs" Line="141" Column="15" />
- <File FileName="Ejdb.Tests/TestBSON.cs" Line="183" Column="18" />
- <File FileName="Ejdb.BSON/BSONIterator.cs" Line="234" Column="9" />
- <File FileName="Ejdb.BSON/BSONType.cs" Line="29" Column="14" />
+ <File FileName="Ejdb.JSON/JSONElement.cs" Line="1" Column="1" />
+ <File FileName="Ejdb.BSON/BSONDocument.cs" Line="322" Column="1" />
+ <File FileName="Ejdb.Tests/TestBSON.cs" Line="133" Column="31" />
+ <File FileName="Ejdb.BSON/BSONIterator.cs" Line="308" Column="37" />
+ <File FileName="Ejdb.BSON/BSONType.cs" Line="39" Column="20" />
+ <File FileName="Ejdb.BSON/BSONOid.cs" Line="1" Column="1" />
+ <File FileName="Ejdb.IO/ExtBinaryWriter.cs" Line="54" Column="5" />
</Files>
<Pads>
<Pad Id="ProjectPad">
<Value>_input.BaseStream.Position</Value>
</State>
</Pad>
+ <Pad Id="ConnectionManagerPad">
+ <State selected="True" />
+ </Pad>
<Pad Id="MonoDevelop.NUnit.TestPad">
- <State expanded="True" selected="True">
- <Node name="nejdb" expanded="True">
+ <State expanded="True">
+ <Node name="nejdb" expanded="True" selected="True">
<Node name="Ejdb" expanded="True">
<Node name="Tests" expanded="True">
<Node name="TestBSON" expanded="True" />
</Node>
</State>
</Pad>
- <Pad Id="ConnectionManagerPad">
- <State selected="True" />
- </Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>