Skip to content

Commit 3c4c80d

Browse files
papaferstam
andauthored
CSHARP-5319: Remove AsDateTime and similar from BsonValue (#1494)
Co-authored-by: rstam <[email protected]>
1 parent 2420e1e commit 3c4c80d

File tree

4 files changed

+131
-67
lines changed

4 files changed

+131
-67
lines changed

src/MongoDB.Bson/ObjectModel/BsonValue.cs

+62-36
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18+
using System.ComponentModel;
19+
using System.Diagnostics;
1820
using System.Text.RegularExpressions;
1921

2022
namespace MongoDB.Bson
@@ -53,6 +55,7 @@ public abstract class BsonValue : IComparable<BsonValue>, IConvertible, IEquatab
5355
/// <summary>
5456
/// Casts the BsonValue to a Boolean (throws an InvalidCastException if the cast is not valid).
5557
/// </summary>
58+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
5659
public bool AsBoolean
5760
{
5861
get { return ((BsonBoolean)this).Value; }
@@ -61,6 +64,7 @@ public bool AsBoolean
6164
/// <summary>
6265
/// Casts the BsonValue to a BsonArray (throws an InvalidCastException if the cast is not valid).
6366
/// </summary>
67+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
6468
public BsonArray AsBsonArray
6569
{
6670
get { return (BsonArray)this; }
@@ -69,6 +73,7 @@ public BsonArray AsBsonArray
6973
/// <summary>
7074
/// Casts the BsonValue to a BsonBinaryData (throws an InvalidCastException if the cast is not valid).
7175
/// </summary>
76+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
7277
public BsonBinaryData AsBsonBinaryData
7378
{
7479
get { return (BsonBinaryData)this; }
@@ -77,6 +82,7 @@ public BsonBinaryData AsBsonBinaryData
7782
/// <summary>
7883
/// Casts the BsonValue to a BsonDateTime (throws an InvalidCastException if the cast is not valid).
7984
/// </summary>
85+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
8086
public BsonDateTime AsBsonDateTime
8187
{
8288
get { return (BsonDateTime)this; }
@@ -85,6 +91,7 @@ public BsonDateTime AsBsonDateTime
8591
/// <summary>
8692
/// Casts the BsonValue to a BsonDocument (throws an InvalidCastException if the cast is not valid).
8793
/// </summary>
94+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
8895
public BsonDocument AsBsonDocument
8996
{
9097
get { return (BsonDocument)this; }
@@ -93,6 +100,7 @@ public BsonDocument AsBsonDocument
93100
/// <summary>
94101
/// Casts the BsonValue to a BsonJavaScript (throws an InvalidCastException if the cast is not valid).
95102
/// </summary>
103+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
96104
public BsonJavaScript AsBsonJavaScript
97105
{
98106
get { return (BsonJavaScript)this; }
@@ -101,6 +109,7 @@ public BsonJavaScript AsBsonJavaScript
101109
/// <summary>
102110
/// Casts the BsonValue to a BsonJavaScriptWithScope (throws an InvalidCastException if the cast is not valid).
103111
/// </summary>
112+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
104113
public BsonJavaScriptWithScope AsBsonJavaScriptWithScope
105114
{
106115
get { return (BsonJavaScriptWithScope)this; }
@@ -109,6 +118,7 @@ public BsonJavaScriptWithScope AsBsonJavaScriptWithScope
109118
/// <summary>
110119
/// Casts the BsonValue to a BsonMaxKey (throws an InvalidCastException if the cast is not valid).
111120
/// </summary>
121+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
112122
public BsonMaxKey AsBsonMaxKey
113123
{
114124
get { return (BsonMaxKey)this; }
@@ -117,6 +127,7 @@ public BsonMaxKey AsBsonMaxKey
117127
/// <summary>
118128
/// Casts the BsonValue to a BsonMinKey (throws an InvalidCastException if the cast is not valid).
119129
/// </summary>
130+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
120131
public BsonMinKey AsBsonMinKey
121132
{
122133
get { return (BsonMinKey)this; }
@@ -125,6 +136,7 @@ public BsonMinKey AsBsonMinKey
125136
/// <summary>
126137
/// Casts the BsonValue to a BsonNull (throws an InvalidCastException if the cast is not valid).
127138
/// </summary>
139+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
128140
public BsonNull AsBsonNull
129141
{
130142
get { return (BsonNull)this; }
@@ -133,6 +145,7 @@ public BsonNull AsBsonNull
133145
/// <summary>
134146
/// Casts the BsonValue to a BsonRegularExpression (throws an InvalidCastException if the cast is not valid).
135147
/// </summary>
148+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
136149
public BsonRegularExpression AsBsonRegularExpression
137150
{
138151
get { return (BsonRegularExpression)this; }
@@ -141,6 +154,7 @@ public BsonRegularExpression AsBsonRegularExpression
141154
/// <summary>
142155
/// Casts the BsonValue to a BsonSymbol (throws an InvalidCastException if the cast is not valid).
143156
/// </summary>
157+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
144158
public BsonSymbol AsBsonSymbol
145159
{
146160
get { return (BsonSymbol)this; }
@@ -149,6 +163,7 @@ public BsonSymbol AsBsonSymbol
149163
/// <summary>
150164
/// Casts the BsonValue to a BsonTimestamp (throws an InvalidCastException if the cast is not valid).
151165
/// </summary>
166+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
152167
public BsonTimestamp AsBsonTimestamp
153168
{
154169
get { return (BsonTimestamp)this; }
@@ -157,6 +172,7 @@ public BsonTimestamp AsBsonTimestamp
157172
/// <summary>
158173
/// Casts the BsonValue to a BsonUndefined (throws an InvalidCastException if the cast is not valid).
159174
/// </summary>
175+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
160176
public BsonUndefined AsBsonUndefined
161177
{
162178
get { return (BsonUndefined)this; }
@@ -165,6 +181,7 @@ public BsonUndefined AsBsonUndefined
165181
/// <summary>
166182
/// Casts the BsonValue to a BsonValue (a way of upcasting subclasses of BsonValue to BsonValue at compile time).
167183
/// </summary>
184+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
168185
public BsonValue AsBsonValue
169186
{
170187
get { return this; }
@@ -173,23 +190,16 @@ public BsonValue AsBsonValue
173190
/// <summary>
174191
/// Casts the BsonValue to a Byte[] (throws an InvalidCastException if the cast is not valid).
175192
/// </summary>
193+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
176194
public byte[] AsByteArray
177195
{
178196
get { return ((BsonBinaryData)this).Bytes; }
179197
}
180198

181-
/// <summary>
182-
/// Casts the BsonValue to a DateTime in UTC (throws an InvalidCastException if the cast is not valid).
183-
/// </summary>
184-
[Obsolete("Use ToUniversalTime instead.")]
185-
public DateTime AsDateTime
186-
{
187-
get { return AsUniversalTime; }
188-
}
189-
190199
/// <summary>
191200
/// Casts the BsonValue to a <see cref="decimal"/> (throws an InvalidCastException if the cast is not valid).
192201
/// </summary>
202+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
193203
public decimal AsDecimal
194204
{
195205
get { return (decimal)((BsonDecimal128)this).Value; }
@@ -198,6 +208,7 @@ public decimal AsDecimal
198208
/// <summary>
199209
/// Casts the BsonValue to a <see cref="Decimal128"/> (throws an InvalidCastException if the cast is not valid).
200210
/// </summary>
211+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
201212
public Decimal128 AsDecimal128
202213
{
203214
get { return ((BsonDecimal128)this).Value; }
@@ -206,6 +217,7 @@ public Decimal128 AsDecimal128
206217
/// <summary>
207218
/// Casts the BsonValue to a Double (throws an InvalidCastException if the cast is not valid).
208219
/// </summary>
220+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
209221
public double AsDouble
210222
{
211223
get { return ((BsonDouble)this).Value; }
@@ -214,6 +226,7 @@ public double AsDouble
214226
/// <summary>
215227
/// Casts the BsonValue to a Guid (throws an InvalidCastException if the cast is not valid).
216228
/// </summary>
229+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
217230
public Guid AsGuid
218231
{
219232
get { return ((BsonBinaryData)this).ToGuid(); }
@@ -222,48 +235,43 @@ public Guid AsGuid
222235
/// <summary>
223236
/// Casts the BsonValue to an Int32 (throws an InvalidCastException if the cast is not valid).
224237
/// </summary>
238+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
225239
public int AsInt32
226240
{
227241
get { return ((BsonInt32)this).Value; }
228242
}
229243

230-
/// <summary>
231-
/// Casts the BsonValue to a DateTime in the local timezone (throws an InvalidCastException if the cast is not valid).
232-
/// </summary>
233-
[Obsolete("Use ToLocalTime instead.")]
234-
public DateTime AsLocalTime
235-
{
236-
get { return ((BsonDateTime)this).ToLocalTime(); }
237-
}
238-
239244
/// <summary>
240245
/// Casts the BsonValue to a Int64 (throws an InvalidCastException if the cast is not valid).
241246
/// </summary>
247+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
242248
public long AsInt64
243249
{
244250
get { return ((BsonInt64)this).Value; }
245251
}
246252

247253
/// <summary>
248-
/// Casts the BsonValue to a Nullable{Boolean} (throws an InvalidCastException if the cast is not valid).
254+
/// Casts the BsonValue to a DateTime in the local timezone (throws an InvalidCastException if the cast is not valid).
249255
/// </summary>
250-
public bool? AsNullableBoolean
256+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
257+
public DateTime AsLocalTime
251258
{
252-
get { return (BsonType == BsonType.Null) ? null : (bool?)AsBoolean; }
259+
get { return ((BsonDateTime)this).ToLocalTime(); }
253260
}
254261

255262
/// <summary>
256-
/// Casts the BsonValue to a Nullable{DateTime} (throws an InvalidCastException if the cast is not valid).
263+
/// Casts the BsonValue to a Nullable{Boolean} (throws an InvalidCastException if the cast is not valid).
257264
/// </summary>
258-
[Obsolete("Use ToNullableUniversalTime instead.")]
259-
public DateTime? AsNullableDateTime
265+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
266+
public bool? AsNullableBoolean
260267
{
261-
get { return (BsonType == BsonType.Null) ? null : (DateTime?)AsDateTime; }
268+
get { return (BsonType == BsonType.Null) ? null : (bool?)AsBoolean; }
262269
}
263270

264271
/// <summary>
265272
/// Casts the BsonValue to a Nullable{Decimal} (throws an InvalidCastException if the cast is not valid).
266273
/// </summary>
274+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
267275
public decimal? AsNullableDecimal
268276
{
269277
get { return (BsonType == BsonType.Null) ? null : (decimal?)AsDecimal128; }
@@ -272,6 +280,7 @@ public decimal? AsNullableDecimal
272280
/// <summary>
273281
/// Casts the BsonValue to a Nullable{Decimal128} (throws an InvalidCastException if the cast is not valid).
274282
/// </summary>
283+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
275284
public Decimal128? AsNullableDecimal128
276285
{
277286
get { return (BsonType == BsonType.Null) ? null : (Decimal128?)AsDecimal128; }
@@ -280,6 +289,7 @@ public Decimal128? AsNullableDecimal128
280289
/// <summary>
281290
/// Casts the BsonValue to a Nullable{Double} (throws an InvalidCastException if the cast is not valid).
282291
/// </summary>
292+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
283293
public double? AsNullableDouble
284294
{
285295
get { return (BsonType == BsonType.Null) ? null : (double?)AsDouble; }
@@ -288,6 +298,7 @@ public double? AsNullableDouble
288298
/// <summary>
289299
/// Casts the BsonValue to a Nullable{Guid} (throws an InvalidCastException if the cast is not valid).
290300
/// </summary>
301+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
291302
public Guid? AsNullableGuid
292303
{
293304
get { return (BsonType == BsonType.Null) ? null : (Guid?)AsGuid; }
@@ -296,6 +307,7 @@ public Guid? AsNullableGuid
296307
/// <summary>
297308
/// Casts the BsonValue to a Nullable{Int32} (throws an InvalidCastException if the cast is not valid).
298309
/// </summary>
310+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
299311
public int? AsNullableInt32
300312
{
301313
get { return (BsonType == BsonType.Null) ? null : (int?)AsInt32; }
@@ -304,22 +316,43 @@ public int? AsNullableInt32
304316
/// <summary>
305317
/// Casts the BsonValue to a Nullable{Int64} (throws an InvalidCastException if the cast is not valid).
306318
/// </summary>
319+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
307320
public long? AsNullableInt64
308321
{
309322
get { return (BsonType == BsonType.Null) ? null : (long?)AsInt64; }
310323
}
311324

325+
/// <summary>
326+
/// Casts the BsonValue to a Nullable{DateTime} in the local timezone (throws an InvalidCastException if the cast is not valid).
327+
/// </summary>
328+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
329+
public DateTime? AsNullableLocalTime
330+
{
331+
get { return (BsonType == BsonType.Null) ? null : ((BsonDateTime)this).ToLocalTime(); }
332+
}
333+
312334
/// <summary>
313335
/// Casts the BsonValue to a Nullable{ObjectId} (throws an InvalidCastException if the cast is not valid).
314336
/// </summary>
337+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
315338
public ObjectId? AsNullableObjectId
316339
{
317340
get { return (BsonType == BsonType.Null) ? null : (ObjectId?)AsObjectId; }
318341
}
319342

343+
/// <summary>
344+
/// Casts the BsonValue to a Nullable{DateTime} in UTC (throws an InvalidCastException if the cast is not valid).
345+
/// </summary>
346+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
347+
public DateTime? AsNullableUniversalTime
348+
{
349+
get { return (BsonType == BsonType.Null) ? null : ((BsonDateTime)this).ToUniversalTime(); }
350+
}
351+
320352
/// <summary>
321353
/// Casts the BsonValue to an ObjectId (throws an InvalidCastException if the cast is not valid).
322354
/// </summary>
355+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
323356
public ObjectId AsObjectId
324357
{
325358
get { return ((BsonObjectId)this).Value; }
@@ -328,6 +361,7 @@ public ObjectId AsObjectId
328361
/// <summary>
329362
/// Casts the BsonValue to a Regex (throws an InvalidCastException if the cast is not valid).
330363
/// </summary>
364+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
331365
public Regex AsRegex
332366
{
333367
get { return ((BsonRegularExpression)this).ToRegex(); }
@@ -336,6 +370,7 @@ public Regex AsRegex
336370
/// <summary>
337371
/// Casts the BsonValue to a String (throws an InvalidCastException if the cast is not valid).
338372
/// </summary>
373+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
339374
public string AsString
340375
{
341376
get { return ((BsonString)this).Value; }
@@ -344,7 +379,7 @@ public string AsString
344379
/// <summary>
345380
/// Casts the BsonValue to a DateTime in UTC (throws an InvalidCastException if the cast is not valid).
346381
/// </summary>
347-
[Obsolete("Use ToUniversalTime instead.")]
382+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
348383
public DateTime AsUniversalTime
349384
{
350385
get { return ((BsonDateTime)this).ToUniversalTime(); }
@@ -467,15 +502,6 @@ public bool IsBsonUndefined
467502
get { return BsonType == BsonType.Undefined; }
468503
}
469504

470-
/// <summary>
471-
/// Tests whether this BsonValue is a DateTime.
472-
/// </summary>
473-
[Obsolete("Use IsValidDateTime instead.")]
474-
public bool IsDateTime
475-
{
476-
get { return IsValidDateTime; }
477-
}
478-
479505
/// <summary>
480506
/// Tests whether this BsonValue is a Decimal128.
481507
/// </summary>
@@ -1248,7 +1274,7 @@ public bool Equals(BsonValue rhs)
12481274
public virtual bool ToBoolean()
12491275
{
12501276
// some subclasses override as appropriate
1251-
return true; // everything else is true
1277+
return true; // everything else is true
12521278
}
12531279

12541280
/// <summary>

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ToFilterFieldTranslators/MemberExpressionToFilterFieldTranslator.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public static AstFilterField Translate(TranslationContext context, MemberExpress
6262
case "AsBsonUndefined": return AstFilter.Field(field.Path, BsonUndefinedSerializer.Instance);
6363
case "AsBsonValue": return AstFilter.Field(field.Path, fieldSerializer);
6464
case "AsByteArray": return AstFilter.Field(field.Path, ByteArraySerializer.Instance);
65-
case "AsDateTime": return AstFilter.Field(field.Path, DateTimeSerializer.UtcInstance);
6665
case "AsDecimal": return AstFilter.Field(field.Path, DecimalSerializer.Instance);
6766
case "AsDecimal128": return AstFilter.Field(field.Path, Decimal128Serializer.Instance);
6867
case "AsDouble": return AstFilter.Field(field.Path, DoubleSerializer.Instance);
@@ -71,14 +70,14 @@ public static AstFilterField Translate(TranslationContext context, MemberExpress
7170
case "AsInt64": return AstFilter.Field(field.Path, Int64Serializer.Instance);
7271
case "AsLocalTime": return AstFilter.Field(field.Path, DateTimeSerializer.LocalInstance);
7372
case "AsNullableBoolean": return AstFilter.Field(field.Path, __nullableBooleanSerializer);
74-
case "AsNullableDateTime": return AstFilter.Field(field.Path, __nullableDateTimeSerializer);
7573
case "AsNullableDecimal": return AstFilter.Field(field.Path, __nullableDecimalSerializer);
7674
case "AsNullableDecimal128": return AstFilter.Field(field.Path, __nullableDecimal128Serializer);
7775
case "AsNullableDouble": return AstFilter.Field(field.Path, __nullableDoubleSerializer);
7876
case "AsNullableGuid": return AstFilter.Field(field.Path, __nullableGuidSerializer);
7977
case "AsNullableInt32": return AstFilter.Field(field.Path, __nullableInt32Serializer);
8078
case "AsNullableInt64": return AstFilter.Field(field.Path, __nullableInt64Serializer);
8179
case "AsNullableObjectId": return AstFilter.Field(field.Path, __nullableObjectIdSerializer);
80+
case "AsNullableUniversalTime": return AstFilter.Field(field.Path, __nullableDateTimeSerializer);
8281
case "AsObjectId": return AstFilter.Field(field.Path, ObjectIdSerializer.Instance);
8382
case "AsRegex": return AstFilter.Field(field.Path, RegexSerializer.RegularExpressionInstance);
8483
case "AsString": return AstFilter.Field(field.Path, StringSerializer.Instance);

0 commit comments

Comments
 (0)