Skip to content

CSHARP-5319: Remove AsDateTime and similar from BsonValue #1494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 62 additions & 36 deletions src/MongoDB.Bson/ObjectModel/BsonValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace MongoDB.Bson
Expand Down Expand Up @@ -53,6 +55,7 @@ public abstract class BsonValue : IComparable<BsonValue>, IConvertible, IEquatab
/// <summary>
/// Casts the BsonValue to a Boolean (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public bool AsBoolean
{
get { return ((BsonBoolean)this).Value; }
Expand All @@ -61,6 +64,7 @@ public bool AsBoolean
/// <summary>
/// Casts the BsonValue to a BsonArray (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonArray AsBsonArray
{
get { return (BsonArray)this; }
Expand All @@ -69,6 +73,7 @@ public BsonArray AsBsonArray
/// <summary>
/// Casts the BsonValue to a BsonBinaryData (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonBinaryData AsBsonBinaryData
{
get { return (BsonBinaryData)this; }
Expand All @@ -77,6 +82,7 @@ public BsonBinaryData AsBsonBinaryData
/// <summary>
/// Casts the BsonValue to a BsonDateTime (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing attribute on line 94.

public BsonDateTime AsBsonDateTime
{
get { return (BsonDateTime)this; }
Expand All @@ -85,6 +91,7 @@ public BsonDateTime AsBsonDateTime
/// <summary>
/// Casts the BsonValue to a BsonDocument (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonDocument AsBsonDocument
{
get { return (BsonDocument)this; }
Expand All @@ -93,6 +100,7 @@ public BsonDocument AsBsonDocument
/// <summary>
/// Casts the BsonValue to a BsonJavaScript (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonJavaScript AsBsonJavaScript
{
get { return (BsonJavaScript)this; }
Expand All @@ -101,6 +109,7 @@ public BsonJavaScript AsBsonJavaScript
/// <summary>
/// Casts the BsonValue to a BsonJavaScriptWithScope (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonJavaScriptWithScope AsBsonJavaScriptWithScope
{
get { return (BsonJavaScriptWithScope)this; }
Expand All @@ -109,6 +118,7 @@ public BsonJavaScriptWithScope AsBsonJavaScriptWithScope
/// <summary>
/// Casts the BsonValue to a BsonMaxKey (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonMaxKey AsBsonMaxKey
{
get { return (BsonMaxKey)this; }
Expand All @@ -117,6 +127,7 @@ public BsonMaxKey AsBsonMaxKey
/// <summary>
/// Casts the BsonValue to a BsonMinKey (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonMinKey AsBsonMinKey
{
get { return (BsonMinKey)this; }
Expand All @@ -125,6 +136,7 @@ public BsonMinKey AsBsonMinKey
/// <summary>
/// Casts the BsonValue to a BsonNull (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonNull AsBsonNull
{
get { return (BsonNull)this; }
Expand All @@ -133,6 +145,7 @@ public BsonNull AsBsonNull
/// <summary>
/// Casts the BsonValue to a BsonRegularExpression (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonRegularExpression AsBsonRegularExpression
{
get { return (BsonRegularExpression)this; }
Expand All @@ -141,6 +154,7 @@ public BsonRegularExpression AsBsonRegularExpression
/// <summary>
/// Casts the BsonValue to a BsonSymbol (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonSymbol AsBsonSymbol
{
get { return (BsonSymbol)this; }
Expand All @@ -149,6 +163,7 @@ public BsonSymbol AsBsonSymbol
/// <summary>
/// Casts the BsonValue to a BsonTimestamp (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonTimestamp AsBsonTimestamp
{
get { return (BsonTimestamp)this; }
Expand All @@ -157,6 +172,7 @@ public BsonTimestamp AsBsonTimestamp
/// <summary>
/// Casts the BsonValue to a BsonUndefined (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public BsonUndefined AsBsonUndefined
{
get { return (BsonUndefined)this; }
Expand All @@ -165,6 +181,7 @@ public BsonUndefined AsBsonUndefined
/// <summary>
/// Casts the BsonValue to a BsonValue (a way of upcasting subclasses of BsonValue to BsonValue at compile time).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing attribute on line 192.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 just added

public BsonValue AsBsonValue
{
get { return this; }
Expand All @@ -173,23 +190,16 @@ public BsonValue AsBsonValue
/// <summary>
/// Casts the BsonValue to a Byte[] (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public byte[] AsByteArray
{
get { return ((BsonBinaryData)this).Bytes; }
}

/// <summary>
/// Casts the BsonValue to a DateTime in UTC (throws an InvalidCastException if the cast is not valid).
/// </summary>
[Obsolete("Use ToUniversalTime instead.")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This obsoletion was proper and the member can be deleted.

The error message was wrong though. It should have been "Use AsUniversalTime" instead.

public DateTime AsDateTime
{
get { return AsUniversalTime; }
}

/// <summary>
/// Casts the BsonValue to a <see cref="decimal"/> (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public decimal AsDecimal
{
get { return (decimal)((BsonDecimal128)this).Value; }
Expand All @@ -198,6 +208,7 @@ public decimal AsDecimal
/// <summary>
/// Casts the BsonValue to a <see cref="Decimal128"/> (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Decimal128 AsDecimal128
{
get { return ((BsonDecimal128)this).Value; }
Expand All @@ -206,6 +217,7 @@ public Decimal128 AsDecimal128
/// <summary>
/// Casts the BsonValue to a Double (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public double AsDouble
{
get { return ((BsonDouble)this).Value; }
Expand All @@ -214,6 +226,7 @@ public double AsDouble
/// <summary>
/// Casts the BsonValue to a Guid (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Guid AsGuid
{
get { return ((BsonBinaryData)this).ToGuid(); }
Expand All @@ -222,48 +235,43 @@ public Guid AsGuid
/// <summary>
/// Casts the BsonValue to an Int32 (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public int AsInt32
{
get { return ((BsonInt32)this).Value; }
}

/// <summary>
/// Casts the BsonValue to a DateTime in the local timezone (throws an InvalidCastException if the cast is not valid).
/// </summary>
[Obsolete("Use ToLocalTime instead.")]
public DateTime AsLocalTime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was obsoleted by mistake and we should keep the property and remove the [Obsolete] attribute.

{
get { return ((BsonDateTime)this).ToLocalTime(); }
}

/// <summary>
/// Casts the BsonValue to a Int64 (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public long AsInt64
{
get { return ((BsonInt64)this).Value; }
}

/// <summary>
/// Casts the BsonValue to a Nullable{Boolean} (throws an InvalidCastException if the cast is not valid).
/// Casts the BsonValue to a DateTime in the local timezone (throws an InvalidCastException if the cast is not valid).
/// </summary>
public bool? AsNullableBoolean
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public DateTime AsLocalTime
{
get { return (BsonType == BsonType.Null) ? null : (bool?)AsBoolean; }
get { return ((BsonDateTime)this).ToLocalTime(); }
}

/// <summary>
/// Casts the BsonValue to a Nullable{DateTime} (throws an InvalidCastException if the cast is not valid).
/// Casts the BsonValue to a Nullable{Boolean} (throws an InvalidCastException if the cast is not valid).
/// </summary>
[Obsolete("Use ToNullableUniversalTime instead.")]
public DateTime? AsNullableDateTime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property should be replaced with two properties: AsNullableLocalTime and AsNullableUniversalTime.

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public bool? AsNullableBoolean
{
get { return (BsonType == BsonType.Null) ? null : (DateTime?)AsDateTime; }
get { return (BsonType == BsonType.Null) ? null : (bool?)AsBoolean; }
}

/// <summary>
/// Casts the BsonValue to a Nullable{Decimal} (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public decimal? AsNullableDecimal
{
get { return (BsonType == BsonType.Null) ? null : (decimal?)AsDecimal128; }
Expand All @@ -272,6 +280,7 @@ public decimal? AsNullableDecimal
/// <summary>
/// Casts the BsonValue to a Nullable{Decimal128} (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Decimal128? AsNullableDecimal128
{
get { return (BsonType == BsonType.Null) ? null : (Decimal128?)AsDecimal128; }
Expand All @@ -280,6 +289,7 @@ public Decimal128? AsNullableDecimal128
/// <summary>
/// Casts the BsonValue to a Nullable{Double} (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public double? AsNullableDouble
{
get { return (BsonType == BsonType.Null) ? null : (double?)AsDouble; }
Expand All @@ -288,6 +298,7 @@ public double? AsNullableDouble
/// <summary>
/// Casts the BsonValue to a Nullable{Guid} (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Guid? AsNullableGuid
{
get { return (BsonType == BsonType.Null) ? null : (Guid?)AsGuid; }
Expand All @@ -296,6 +307,7 @@ public Guid? AsNullableGuid
/// <summary>
/// Casts the BsonValue to a Nullable{Int32} (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public int? AsNullableInt32
{
get { return (BsonType == BsonType.Null) ? null : (int?)AsInt32; }
Expand All @@ -304,22 +316,43 @@ public int? AsNullableInt32
/// <summary>
/// Casts the BsonValue to a Nullable{Int64} (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public long? AsNullableInt64
{
get { return (BsonType == BsonType.Null) ? null : (long?)AsInt64; }
}

/// <summary>
/// Casts the BsonValue to a Nullable{DateTime} in the local timezone (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public DateTime? AsNullableLocalTime
{
get { return (BsonType == BsonType.Null) ? null : ((BsonDateTime)this).ToLocalTime(); }
}

/// <summary>
/// Casts the BsonValue to a Nullable{ObjectId} (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public ObjectId? AsNullableObjectId
{
get { return (BsonType == BsonType.Null) ? null : (ObjectId?)AsObjectId; }
}

/// <summary>
/// Casts the BsonValue to a Nullable{DateTime} in UTC (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public DateTime? AsNullableUniversalTime
{
get { return (BsonType == BsonType.Null) ? null : ((BsonDateTime)this).ToUniversalTime(); }
}

/// <summary>
/// Casts the BsonValue to an ObjectId (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public ObjectId AsObjectId
{
get { return ((BsonObjectId)this).Value; }
Expand All @@ -328,6 +361,7 @@ public ObjectId AsObjectId
/// <summary>
/// Casts the BsonValue to a Regex (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Regex AsRegex
{
get { return ((BsonRegularExpression)this).ToRegex(); }
Expand All @@ -336,6 +370,7 @@ public Regex AsRegex
/// <summary>
/// Casts the BsonValue to a String (throws an InvalidCastException if the cast is not valid).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public string AsString
{
get { return ((BsonString)this).Value; }
Expand All @@ -344,7 +379,7 @@ public string AsString
/// <summary>
/// Casts the BsonValue to a DateTime in UTC (throws an InvalidCastException if the cast is not valid).
/// </summary>
[Obsolete("Use ToUniversalTime instead.")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was obsoleted by mistake and we should keep the property and remove the [Obsolete] attribute.

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public DateTime AsUniversalTime
{
get { return ((BsonDateTime)this).ToUniversalTime(); }
Expand Down Expand Up @@ -467,15 +502,6 @@ public bool IsBsonUndefined
get { return BsonType == BsonType.Undefined; }
}

/// <summary>
/// Tests whether this BsonValue is a DateTime.
/// </summary>
[Obsolete("Use IsValidDateTime instead.")]
public bool IsDateTime
{
get { return IsValidDateTime; }
}

/// <summary>
/// Tests whether this BsonValue is a Decimal128.
/// </summary>
Expand Down Expand Up @@ -1248,7 +1274,7 @@ public bool Equals(BsonValue rhs)
public virtual bool ToBoolean()
{
// some subclasses override as appropriate
return true; // everything else is true
return true; // everything else is true
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public static AstFilterField Translate(TranslationContext context, MemberExpress
case "AsBsonUndefined": return AstFilter.Field(field.Path, BsonUndefinedSerializer.Instance);
case "AsBsonValue": return AstFilter.Field(field.Path, fieldSerializer);
case "AsByteArray": return AstFilter.Field(field.Path, ByteArraySerializer.Instance);
case "AsDateTime": return AstFilter.Field(field.Path, DateTimeSerializer.UtcInstance);
case "AsDecimal": return AstFilter.Field(field.Path, DecimalSerializer.Instance);
case "AsDecimal128": return AstFilter.Field(field.Path, Decimal128Serializer.Instance);
case "AsDouble": return AstFilter.Field(field.Path, DoubleSerializer.Instance);
Expand All @@ -71,14 +70,14 @@ public static AstFilterField Translate(TranslationContext context, MemberExpress
case "AsInt64": return AstFilter.Field(field.Path, Int64Serializer.Instance);
case "AsLocalTime": return AstFilter.Field(field.Path, DateTimeSerializer.LocalInstance);
case "AsNullableBoolean": return AstFilter.Field(field.Path, __nullableBooleanSerializer);
case "AsNullableDateTime": return AstFilter.Field(field.Path, __nullableDateTimeSerializer);
case "AsNullableDecimal": return AstFilter.Field(field.Path, __nullableDecimalSerializer);
case "AsNullableDecimal128": return AstFilter.Field(field.Path, __nullableDecimal128Serializer);
case "AsNullableDouble": return AstFilter.Field(field.Path, __nullableDoubleSerializer);
case "AsNullableGuid": return AstFilter.Field(field.Path, __nullableGuidSerializer);
case "AsNullableInt32": return AstFilter.Field(field.Path, __nullableInt32Serializer);
case "AsNullableInt64": return AstFilter.Field(field.Path, __nullableInt64Serializer);
case "AsNullableObjectId": return AstFilter.Field(field.Path, __nullableObjectIdSerializer);
case "AsNullableUniversalTime": return AstFilter.Field(field.Path, __nullableDateTimeSerializer);
case "AsObjectId": return AstFilter.Field(field.Path, ObjectIdSerializer.Instance);
case "AsRegex": return AstFilter.Field(field.Path, RegexSerializer.RegularExpressionInstance);
case "AsString": return AstFilter.Field(field.Path, StringSerializer.Instance);
Expand Down
Loading