|
13 | 13 | * limitations under the License.
|
14 | 14 | */
|
15 | 15 |
|
| 16 | +using System; |
16 | 17 | using MongoDB.Bson;
|
17 | 18 | using MongoDB.Bson.Serialization;
|
18 | 19 | using MongoDB.Driver.Core.Misc;
|
@@ -55,6 +56,31 @@ public static implicit operator SearchDefinition<TDocument>(string json) =>
|
55 | 56 | json != null ? new JsonSearchDefinition<TDocument>(json) : null;
|
56 | 57 | }
|
57 | 58 |
|
| 59 | + /// <summary> |
| 60 | + /// Extensions for SearchDefinition |
| 61 | + /// </summary> |
| 62 | + public static class SearchDefinitionExtensions |
| 63 | + { |
| 64 | + /// <summary> |
| 65 | + /// Sets the use of default serialization for the specified <see cref="SearchDefinition{TDocument}"/>. |
| 66 | + /// When set to true, the default serializers will be used to serialize the values of certain Atlas Search operators, such as "Equals", "In" and "Range". This will become the default behaviour in version 4.0 of the library. |
| 67 | + /// If not enabled, then a default conversion will be used. |
| 68 | + /// </summary> |
| 69 | + /// <typeparam name="TDocument">The type of the document.</typeparam> |
| 70 | + /// <param name="searchDefinition">The search definition instance.</param> |
| 71 | + /// <param name="useDefaultSerialization">Whether to use the default serialization or not.</param> |
| 72 | + /// <returns>The same <see cref="SearchDefinition{TDocument}"/> instance with default serialization enabled.</returns> |
| 73 | + /// <exception cref="InvalidOperationException">Thrown if <paramref name="searchDefinition"/> is not of a valid type/>.</exception> |
| 74 | + public static SearchDefinition<TDocument> WithDefaultSerialization<TDocument>(this SearchDefinition<TDocument> searchDefinition, bool useDefaultSerialization) |
| 75 | + { |
| 76 | + if (searchDefinition is not OperatorSearchDefinition<TDocument> op) |
| 77 | + throw new InvalidOperationException("Default serialization cannot be used with the current SearchDefinition type"); |
| 78 | + |
| 79 | + op.SetUseDefaultSerialization(useDefaultSerialization); |
| 80 | + return searchDefinition; |
| 81 | + } |
| 82 | + } |
| 83 | + |
58 | 84 | /// <summary>
|
59 | 85 | /// A search definition based on a BSON document.
|
60 | 86 | /// </summary>
|
@@ -134,6 +160,8 @@ private protected enum OperatorType
|
134 | 160 | protected readonly SearchPathDefinition<TDocument> _path;
|
135 | 161 | protected readonly SearchScoreDefinition<TDocument> _score;
|
136 | 162 |
|
| 163 | + protected bool _useDefaultSerialization = false; |
| 164 | + |
137 | 165 | private protected OperatorSearchDefinition(OperatorType operatorType)
|
138 | 166 | : this(operatorType, null)
|
139 | 167 | {
|
@@ -172,7 +200,36 @@ public override BsonDocument Render(RenderArgs<TDocument> args)
|
172 | 200 | return new(_operatorType.ToCamelCase(), renderedArgs);
|
173 | 201 | }
|
174 | 202 |
|
175 |
| - private protected virtual BsonDocument RenderArguments(RenderArgs<TDocument> args, |
| 203 | + private protected virtual BsonDocument RenderArguments( |
| 204 | + RenderArgs<TDocument> args, |
176 | 205 | IBsonSerializer fieldSerializer) => new();
|
| 206 | + |
| 207 | + internal void SetUseDefaultSerialization(bool useDefaultSerialization) |
| 208 | + { |
| 209 | + _useDefaultSerialization = useDefaultSerialization; |
| 210 | + } |
| 211 | + |
| 212 | + protected static BsonValue ToBsonValue<T>(T value) => |
| 213 | + value switch |
| 214 | + { |
| 215 | + bool v => (BsonBoolean)v, |
| 216 | + sbyte v => (BsonInt32)v, |
| 217 | + byte v => (BsonInt32)v, |
| 218 | + short v => (BsonInt32)v, |
| 219 | + ushort v => (BsonInt32)v, |
| 220 | + int v => (BsonInt32)v, |
| 221 | + uint v => (BsonInt64)v, |
| 222 | + long v => (BsonInt64)v, |
| 223 | + float v => (BsonDouble)v, |
| 224 | + double v => (BsonDouble)v, |
| 225 | + decimal v => (BsonDecimal128)v, |
| 226 | + DateTime v => (BsonDateTime)v, |
| 227 | + DateTimeOffset v => (BsonDateTime)v.UtcDateTime, |
| 228 | + ObjectId v => (BsonObjectId)v, |
| 229 | + Guid v => new BsonBinaryData(v, GuidRepresentation.Standard), |
| 230 | + string v => (BsonString)v, |
| 231 | + null => BsonNull.Value, |
| 232 | + _ => throw new InvalidCastException() |
| 233 | + }; |
177 | 234 | }
|
178 | 235 | }
|
0 commit comments