public static bool Compare(BsonValue a, BsonValue b) { if (a.BsonType == BsonType.Document && b.BsonType == BsonType.Document) { return(CompareDocuments((BsonDocument)a, (BsonDocument)b)); } else if (a.BsonType == BsonType.Array && b.BsonType == BsonType.Array) { return(CompareArrays((BsonArray)a, (BsonArray)b)); } else if (a.BsonType == b.BsonType) { return(a.Equals(b)); } else if (IsNumber(a) && IsNumber(b)) { return(a.ToDouble() == b.ToDouble()); } else if (CouldBeBoolean(a) && CouldBeBoolean(b)) { return(a.ToBoolean() == b.ToBoolean()); } else { return(false); } }
public static bool Compare(BsonValue a, BsonValue b) { if (a.BsonType == BsonType.Document && b.BsonType == BsonType.Document) { return CompareDocuments((BsonDocument)a, (BsonDocument)b); } else if (a.BsonType == BsonType.Array && b.BsonType == BsonType.Array) { return CompareArrays((BsonArray)a, (BsonArray)b); } else if (a.BsonType == b.BsonType) { return a.Equals(b); } else if (IsNumber(a) && IsNumber(b)) { return a.ToDouble() == b.ToDouble(); } else if (CouldBeBoolean(a) && CouldBeBoolean(b)) { return a.ToBoolean() == b.ToBoolean(); } else { return false; } }
private static object Convert(BsonValue value, Type type) { switch (type.Name) { case "String": return(value.ToString()); case "Int32": return(value.ToInt32()); case "Int64": return(value.ToInt64()); case "Decimal": return((decimal)value.ToDouble()); case "Double": return(value.ToDouble()); case "DateTime": return(value.ToLocalTime()); case "Boolean": return(value.ToBoolean()); case "Nullable`1": var nullType = type.GetProperty("Value").PropertyType; switch (nullType.Name) { case "DateTime": return(value.ToNullableLocalTime()); default: return(Convert(value, nullType)); } } return(value); }
static Expression SizeExpression(Expression field, BsonValue args) { double size; if (args.IsNumeric) { size = args.ToDouble(); if (size == args.ToInt64()) { return(Expression.Call(GetMethod("Size"), Data, field, Expression.Constant(size, typeof(double)))); } } throw new ArgumentException("Invalid $size argument."); }
private static bool CouldBeBoolean(BsonValue value) { switch (value.BsonType) { case BsonType.Boolean: return(true); case BsonType.Double: case BsonType.Int32: case BsonType.Int64: var numericValue = value.ToDouble(); return(numericValue == 0.0 || numericValue == 1.0); default: return(false); } }
private MetricValue bsonValueToMetric(BsonValue value, String pluginName, String instanceName, String name, String typeName) { var metric = new MetricValue { HostName = _hostName, PluginName = pluginName, PluginInstanceName = instanceName, TypeName = typeName, TypeInstanceName = name, Values = new double[1] { value.ToDouble() }, FriendlyNames = new string[1] { "" }, Timestamp = Util.toEpoch(DateTime.UtcNow) }; return(metric); }
private object GetDataTypeValueFromBsonValue(BsonValue value, DataType dt) { if (dt == DataType.DT_I8 | dt == DataType.DT_I4) { if (value.IsString) { Int64 parsedInt = -1; if (!Int64.TryParse(value.ToString(), out parsedInt)) { bool pbCancel = true; ComponentMetaData.FireError(0, "MongoDataSource", "Cannot parse string value to integer: " + value.ToString(), "", 0, out pbCancel); } return(parsedInt); } else { return(value.ToInt64()); } } else if (dt == DataType.DT_BOOL) { return(value.ToBoolean()); } else if (dt == DataType.DT_R8 | dt == DataType.DT_R4) { return(value.ToDouble()); } else if (dt == DataType.DT_DATE | dt == DataType.DT_DBTIMESTAMPOFFSET | dt == DataType.DT_DBTIMESTAMP) { return(DateTime.Parse(value.ToString())); } else { if (dt != DataType.DT_STR && !value.IsObjectId && !value.IsString && !value.IsBsonSymbol) { ComponentMetaData.FireWarning(0, "MongoDataSource", "Converting " + value.BsonType + " to string, though datatype was " + dt, String.Empty, 0); } return(value.ToString()); } }
private object GetDataTypeValueFromBsonValue(BsonValue value, DataType dt) { if (dt == DataType.DT_I8 | dt == DataType.DT_I4) { if (value.IsString) { Int64 parsedInt = -1; if (!Int64.TryParse(value.ToString(), out parsedInt)) { bool pbCancel = true; ComponentMetaData.FireError(0, "MongoDataSource", "Cannot parse string value to integer: " + value.ToString(), "", 0, out pbCancel); } return parsedInt; } else { return value.ToInt64(); } } else if (dt == DataType.DT_BOOL) { return value.ToBoolean(); } else if (dt == DataType.DT_R8 | dt == DataType.DT_R4) { return value.ToDouble(); } else if (dt == DataType.DT_DATE | dt == DataType.DT_DBTIMESTAMPOFFSET | dt == DataType.DT_DBTIMESTAMP) { return DateTime.Parse(value.ToString()); } else { if (dt != DataType.DT_STR && !value.IsObjectId && !value.IsString && !value.IsBsonSymbol) ComponentMetaData.FireWarning(0, "MongoDataSource", "Converting " + value.BsonType + " to string, though datatype was " + dt, String.Empty, 0); return value.ToString(); } }
private void AssertValuesMatch(BsonValue actual, BsonValue expected, bool isRoot, bool isRecursiveCall = true) { if (expected.IsBsonDocument && expected.AsBsonDocument.ElementCount == 1 && expected.AsBsonDocument.GetElement(0).Name.StartsWith("$$")) { var specialOperatorDocument = expected.AsBsonDocument; var operatorName = specialOperatorDocument.GetElement(0).Name; var operatorValue = specialOperatorDocument[0]; switch (operatorName) { case "$$exists": actual.Should().NotBeNull(); break; case "$$type": AssertExpectedType(actual, operatorValue); break; case "$$matchesHexBytes": AssertValuesMatch(actual, operatorValue, true); break; case "$$unsetOrMatches": if (actual != null) { AssertValuesMatch(actual, operatorValue, true); } break; default: throw new FormatException($"Unrecognized root level special operator: '{operatorName}'."); } return; } if (expected.IsBsonDocument) { actual.BsonType.Should().Be(BsonType.Document); var expectedDocument = expected.AsBsonDocument; var actualDocument = actual.AsBsonDocument; foreach (var expectedElement in expectedDocument) { var expectedName = expectedElement.Name; var expectedValue = expectedElement.Value; if (expectedValue.IsBsonDocument && expectedValue.AsBsonDocument.ElementCount == 1 && expectedValue.AsBsonDocument.GetElement(0).Name.StartsWith("$$")) { var specialOperatorDocument = expectedValue.AsBsonDocument; var operatorName = specialOperatorDocument.GetElement(0).Name; var operatorValue = specialOperatorDocument[0]; switch (operatorName) { case "$$exists": if (operatorValue.AsBoolean) { actualDocument.Names.Should().Contain(expectedName); } else { actualDocument.Names.Should().NotContain(expectedName); } continue; case "$$type": actualDocument.Names.Should().Contain(expectedName); AssertExpectedType(actualDocument[expectedName], operatorValue); continue; case "$$matchesEntity": var resultId = operatorValue.AsString; expectedValue = _entityMap.GetResult(resultId); break; case "$$matchesHexBytes": expectedValue = operatorValue; break; case "$$unsetOrMatches": if (!actualDocument.Contains(expectedName)) { continue; } expectedValue = operatorValue; break; case "$$sessionLsid": var sessionId = operatorValue.AsString; expectedValue = _entityMap.GetSessionId(sessionId); break; default: throw new FormatException($"Unrecognized special operator: '{operatorName}'."); } } actualDocument.Names.Should().Contain(expectedName); AssertValuesMatch(actualDocument[expectedName], expectedValue, isRoot: false); } if (!isRoot) { actualDocument.Names.Should().BeSubsetOf(expectedDocument.Names); } } else if (expected.IsBsonArray) { actual.BsonType.Should().Be(BsonType.Array); actual.AsBsonArray.Values.Should().HaveSameCount(expected.AsBsonArray.Values); var expectedArray = expected.AsBsonArray; var actualArray = actual.AsBsonArray; for (int i = 0; i < expectedArray.Count; i++) { AssertValuesMatch(actualArray[i], expectedArray[i], isRoot: isRoot && !isRecursiveCall); } } else if (expected.IsNumeric) { actual.IsNumeric.Should().BeTrue(); actual.ToDouble().Should().Be(expected.ToDouble()); } else { actual.BsonType.Should().Be(expected.BsonType); actual.Should().Be(expected); } }
/// <summary> /// Converts a <see cref="BsonValue" /> into a <see cref="UInt64" /> /// </summary> /// <param name="value">The <see cref="BsonValue" /></param> /// <returns>The corresponding <see cref="UInt64" /></returns> public static ulong ToUlong(this BsonValue value) { return(Convert.ToUInt64(value.ToDouble())); }
private static bool CouldBeBoolean(BsonValue value) { switch (value.BsonType) { case BsonType.Boolean: return true; case BsonType.Double: case BsonType.Int32: case BsonType.Int64: var numericValue = value.ToDouble(); return numericValue == 0.0 || numericValue == 1.0; default: return false; } }
private MongoNode AddBsonValueNode(string key, BsonValue v, MongoNode parent) { MongoNode node = new MongoNode { Key = key }; if (v.IsBsonNull) { node = AddTreeNode(key, null, parent); } else if (v.IsBoolean) { node = AddTreeNode(key, v.ToBoolean(), parent); } else if (v.IsInt32) { node = AddTreeNode(key, v.ToInt32(), parent); } else if (v.IsInt32 || v.IsInt64) { node = AddTreeNode(key, v.ToInt64(), parent); } else if (v.IsDouble) { node = AddTreeNode(key, v.ToDouble(), parent); } else if (v.IsDecimal128 || v.IsNumeric) { node = AddTreeNode(key, v.ToDecimal(), parent); } else if (v.IsObjectId) { node = AddTreeNode(key, v.AsObjectId, parent); } else if (v.IsString || v.IsGuid) { node = AddTreeNode(key, v.ToString(), parent); } else if (v.IsValidDateTime || v.IsBsonDateTime) { node = AddTreeNode(key, v.ToLocalTime(), parent); } else if (v.IsString) { node = AddTreeNode(key, v.ToString(), parent); } else if (v.IsValidDateTime) { node = AddTreeNode(key, v.ToLocalTime(), parent); } else if (v.IsBsonArray) { var array = v.AsBsonArray; node.Value = $"[{array.Count}]"; node.Type = "Array"; node.ImagePath = ResourcesBase + @"/Images/arr.png"; var i = 0; foreach (var item in array) { AddBsonValueNode($"[{i}]", item, node); i++; } } else { node = AddTreeNode(key, v.ToString(), parent); } return(node); }