Skip to content

Commit b6ef4d2

Browse files
committed
Added common method BsonType --> string and removed unnecessary files.
1 parent eca0ace commit b6ef4d2

File tree

7 files changed

+64
-85
lines changed

7 files changed

+64
-85
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
18+
namespace MongoDB.Bson.Serialization
19+
{
20+
/// <summary>
21+
/// A static class containing extension methods for <see cref="BsonType"/>.
22+
/// </summary>
23+
public static class BsonTypeExtensions
24+
{
25+
/// <summary>
26+
/// Maps a <see cref="BsonType"/> to its corresponding string representation.
27+
/// </summary>
28+
/// <param name="type">The input type to map.</param>
29+
public static string ToStringRepresentation(this BsonType type)
30+
{
31+
return type switch
32+
{
33+
BsonType.Array => "array",
34+
BsonType.Binary => "binData",
35+
BsonType.Boolean => "bool",
36+
BsonType.DateTime => "date",
37+
BsonType.Decimal128 => "decimal",
38+
BsonType.Document => "object",
39+
BsonType.Double => "double",
40+
BsonType.Int32 => "int",
41+
BsonType.Int64 => "long",
42+
BsonType.JavaScript => "javascript",
43+
BsonType.JavaScriptWithScope => "javascriptWithScope",
44+
BsonType.MaxKey => "maxKey",
45+
BsonType.MinKey => "minKey",
46+
BsonType.Null => "null",
47+
BsonType.ObjectId => "objectId",
48+
BsonType.RegularExpression => "regex",
49+
BsonType.String => "string",
50+
BsonType.Symbol => "symbol",
51+
BsonType.Timestamp => "timestamp",
52+
BsonType.Undefined => "undefined",
53+
_ => throw new ArgumentException($"Unexpected BSON type: {type}.", nameof(type))
54+
};
55+
}
56+
}
57+
}

src/MongoDB.Driver.Encryption/CsfleSchemaBuilder.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private static BsonDocument CreateEncryptDocument(
281281

282282
if (bsonTypes != null)
283283
{
284-
var convertedBsonTypes = bsonTypes.Select(MapBsonTypeToString).ToList();
284+
var convertedBsonTypes = bsonTypes.Select(type => type.ToStringRepresentation()).ToList();
285285

286286
if (convertedBsonTypes.Count == 0)
287287
{
@@ -331,30 +331,6 @@ private void AddToProperties(string field, BsonDocument document)
331331
properties[field] = document;
332332
}
333333

334-
private static string MapBsonTypeToString(BsonType type)
335-
{
336-
return type switch
337-
{
338-
BsonType.Array => "array",
339-
BsonType.Binary => "binData",
340-
BsonType.Boolean => "bool",
341-
BsonType.DateTime => "date",
342-
BsonType.Decimal128 => "decimal",
343-
BsonType.Document => "object",
344-
BsonType.Double => "double",
345-
BsonType.Int32 => "int",
346-
BsonType.Int64 => "long",
347-
BsonType.JavaScript => "javascript",
348-
BsonType.JavaScriptWithScope => "javascriptWithScope",
349-
BsonType.ObjectId => "objectId",
350-
BsonType.RegularExpression => "regex",
351-
BsonType.String => "string",
352-
BsonType.Symbol => "symbol",
353-
BsonType.Timestamp => "timestamp",
354-
_ => throw new ArgumentException($"Unsupported BSON type: {type}.", nameof(type))
355-
};
356-
}
357-
358334
private static string MapCsfleEncryptionAlgorithmToString(EncryptionAlgorithm algorithm)
359335
{
360336
return algorithm switch

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/AstEnumExtensions.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,13 @@
1515

1616
using System;
1717
using MongoDB.Bson;
18+
using MongoDB.Bson.Serialization;
1819

1920
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast
2021
{
2122
internal static class AstEnumExtensions
2223
{
23-
public static string Render(this BsonType type)
24-
{
25-
return type switch
26-
{
27-
BsonType.Array => "array",
28-
BsonType.Binary => "binData",
29-
BsonType.Boolean => "bool",
30-
BsonType.DateTime => "date",
31-
BsonType.Decimal128 => "decimal",
32-
BsonType.Document => "object",
33-
BsonType.Double => "double",
34-
BsonType.Int32 => "int",
35-
BsonType.Int64 => "long",
36-
BsonType.JavaScript => "javascript",
37-
BsonType.JavaScriptWithScope => "javascriptWithScope",
38-
BsonType.MaxKey => "maxKey",
39-
BsonType.MinKey => "minKey",
40-
BsonType.Null => "null",
41-
BsonType.ObjectId => "objectId",
42-
BsonType.RegularExpression => "regex",
43-
BsonType.String => "string",
44-
BsonType.Symbol => "symbol",
45-
BsonType.Timestamp => "timestamp",
46-
BsonType.Undefined => "undefined",
47-
_ => throw new ArgumentException($"Unexpected BSON type: {type}.", nameof(type))
48-
};
49-
}
24+
public static string Render(this BsonType type) => type.ToStringRepresentation();
5025

5126
public static string Render(this ByteOrder byteOrder)
5227
{

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Filters/AstTypeFilterOperation.cs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Collections.Generic;
1818
using System.Linq;
1919
using MongoDB.Bson;
20+
using MongoDB.Bson.Serialization;
2021
using MongoDB.Driver.Core.Misc;
2122
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors;
2223
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
@@ -51,39 +52,11 @@ public override BsonValue Render()
5152
if (_types.Count == 1)
5253
{
5354
var type = _types[0];
54-
return new BsonDocument("$type", MapBsonTypeToString(type));
55+
return new BsonDocument("$type", type.ToStringRepresentation());
5556
}
5657
else
5758
{
58-
return new BsonDocument("$type", new BsonArray(_types.Select(type => MapBsonTypeToString(type))));
59-
}
60-
}
61-
62-
private string MapBsonTypeToString(BsonType type)
63-
{
64-
switch (type)
65-
{
66-
case BsonType.Array: return "array";
67-
case BsonType.Binary: return "binData";
68-
case BsonType.Boolean: return "bool";
69-
case BsonType.DateTime: return "date";
70-
case BsonType.Decimal128: return "decimal";
71-
case BsonType.Document: return "object";
72-
case BsonType.Double: return "double";
73-
case BsonType.Int32: return "int";
74-
case BsonType.Int64: return "long";
75-
case BsonType.JavaScript: return "javascript";
76-
case BsonType.JavaScriptWithScope: return "javascriptWithScope";
77-
case BsonType.MaxKey: return "maxKey";
78-
case BsonType.MinKey: return "minKey";
79-
case BsonType.Null: return "null";
80-
case BsonType.ObjectId: return "objectId";
81-
case BsonType.RegularExpression: return "regex";
82-
case BsonType.String: return "string";
83-
case BsonType.Symbol: return "symbol";
84-
case BsonType.Timestamp: return "timestamp";
85-
case BsonType.Undefined: return "undefined";
86-
default: throw new ArgumentException($"Unexpected BSON type: {type}.", nameof(type));
59+
return new BsonDocument("$type", new BsonArray(_types.Select(type => type.ToStringRepresentation())));
8760
}
8861
}
8962
}

tests/MongoDB.Driver.Tests/Encryption/CsfleSchemaBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public void CsfleSchemaBuilder_with_multiple_types_works_as_expected()
156156
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
157157
});
158158

159-
schemaBuilder.Encrypt<TestClass>(_collectionNamespace, builder =>
159+
schemaBuilder.Encrypt<TestClass>(testCollectionNamespace, builder =>
160160
{
161161
builder.Property(t => t.TestString, BsonType.String);
162162
});

tests/MongoDB.Driver.Tests/Encryption/MongoEncryptionCreateCollectionExceptionTests.cs

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/MongoDB.Driver.Tests/Encryption/MongoEncryptionExceptionTests.cs

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)