Skip to content

Commit

Permalink
Fix BsonValue .ToString() to be more concise with JSON (just convert …
Browse files Browse the repository at this point in the history
…value to JSON value)
  • Loading branch information
mbdavid committed Sep 24, 2019
1 parent 7936b86 commit 4e3c8d7
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 29 deletions.
8 changes: 2 additions & 6 deletions LiteDB.Tests/Expressions/Expressions_Exec_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ public void Expressions_Scalar_Methods()
BsonValue S(string s, params BsonValue[] args)
{
return BsonExpression.Create(s, args).ExecuteScalar(doc);
}

;
};

doc = J("{}");

Expand Down Expand Up @@ -270,9 +268,7 @@ public void Expressions_Enumerable_Expr()
IEnumerable<BsonValue> A(string s, params BsonValue[] args)
{
return BsonExpression.Create(s, args).Execute(docs);
}

;
};

docs = new List<BsonDocument>()
{
Expand Down
10 changes: 5 additions & 5 deletions LiteDB.Tests/Utils/AssertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,31 @@ public static void ArrayEqual<T>(T[] first, T[] second, bool sort)
}
}

[DebuggerHidden]
//[DebuggerHidden]
public static void ExpectValue(this BsonValue value, BsonValue expect)
{
value.Should().Be(expect);
}

[DebuggerHidden]
//[DebuggerHidden]
public static void ExpectValue<T>(this T value, T expect)
{
value.Should().Be(expect);
}

[DebuggerHidden]
//[DebuggerHidden]
public static void ExpectArray(this BsonValue value, params BsonValue[] args)
{
value.Should().Be(new BsonArray(args));
}

[DebuggerHidden]
//[DebuggerHidden]
public static void ExpectJson(this BsonValue value, string expectJson)
{
value.Should().Be((JsonSerializer.Deserialize(expectJson)));
}

[DebuggerHidden]
//[DebuggerHidden]
public static void ExpectValues(this IEnumerable<BsonValue> values, params BsonValue[] expectValues)
{
values.ToArray().Should().Equal(expectValues);
Expand Down
5 changes: 0 additions & 5 deletions LiteDB/Document/BsonArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@ public override int CompareTo(BsonValue other)
return 1;
}

public override string ToString()
{
return JsonSerializer.Serialize(this);
}

private int _length;

internal override int GetBytesCount(bool recalc)
Expand Down
7 changes: 1 addition & 6 deletions LiteDB/Document/BsonDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override BsonValue this[string key]
}
}

#region CompareTo / ToString
#region CompareTo

public override int CompareTo(BsonValue other)
{
Expand Down Expand Up @@ -90,11 +90,6 @@ public override int CompareTo(BsonValue other)
return 1;
}

public override string ToString()
{
return JsonSerializer.Serialize(this);
}

#endregion

#region IDictionary
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Document/BsonValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public static implicit operator BsonValue(DateTime value)

public override string ToString()
{
return this.RawValue?.ToString() ?? "";
return JsonSerializer.Serialize(this);
}

#endregion
Expand Down
5 changes: 4 additions & 1 deletion LiteDB/Document/Expression/Methods/DataTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ public static BsonValue DECIMAL(BsonValue value, BsonValue culture)
/// </summary>
public static BsonValue STRING(BsonValue value)
{
return value.ToString();
return
value.IsNull ? "" :
value.IsString ? value.AsString :
value.ToString();
}

// ==> there is no convert to BsonDocument, must use { .. } syntax
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/Document/Expression/Methods/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static BsonValue JOIN(IEnumerable<BsonValue> values, BsonValue separator)
{
return string.Join(
separator.AsString,
values.Select(x => x.ToString()).ToArray()
values.Select(x => STRING(x).AsString).ToArray()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static BsonValue ADD(BsonValue left, BsonValue right)
// if any sides are string, concat casting both to string
else if (left.IsString || right.IsString)
{
return left.ToString() + right.ToString();
return BsonExpressionMethods.STRING(left).AsString + BsonExpressionMethods.STRING(right).AsString;
}
// if any side are DateTime and another is number, add days in date
else if (left.IsDateTime && right.IsNumber)
Expand Down
10 changes: 8 additions & 2 deletions LiteDB/Engine/Query/IndexQuery/IndexLike.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ private IEnumerable<IndexNode> ExecuteStartsWith(IndexService indexer, Collectio
// if current node are edges exit while
if (node.Key.IsMinValue || node.Key.IsMaxValue) break;

var valueString = node.Key.IsString ? node.Key.AsString : node.Key.ToString();
var valueString =
node.Key.IsString ? node.Key.AsString :
node.Key.IsNull ? "" :
node.Key.ToString();

if (_equals ?
valueString.Equals(_startsWith, StringComparison.OrdinalIgnoreCase) :
Expand Down Expand Up @@ -89,7 +92,10 @@ private IEnumerable<IndexNode> ExecuteStartsWith(IndexService indexer, Collectio
// if current node are edges exit while
if (node.Key.IsMinValue || node.Key.IsMaxValue) break;

var valueString = node.Key.IsString ? node.Key.AsString : node.Key.ToString();
var valueString =
node.Key.IsString ? node.Key.AsString :
node.Key.IsNull ? "" :
node.Key.ToString();

if (_equals ?
valueString.Equals(_pattern, StringComparison.OrdinalIgnoreCase) :
Expand Down
2 changes: 1 addition & 1 deletion LiteDB/LiteDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<NeutralLanguage>en-US</NeutralLanguage>
<Title>LiteDB</Title>
<PackageId>LiteDB</PackageId>
<PackageVersion>5.0.0-alpha</PackageVersion>
<PackageVersion>5.0.0-alpha2</PackageVersion>
<PackageTags>database nosql embedded</PackageTags>
<PackageIconUrl>http://www.litedb.org/img/logo_64x64.png</PackageIconUrl>
<PackageProjectUrl>https://www.litedb.org</PackageProjectUrl>
Expand Down

0 comments on commit 4e3c8d7

Please sign in to comment.