Skip to content

Commit 3338f33

Browse files
committed
VS-99: Optimize MQL Generator to avoid having 1 using statement for each supported Bson Type
1 parent 216e053 commit 3338f33

File tree

5 files changed

+21
-30
lines changed

5 files changed

+21
-30
lines changed

src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,12 @@
1414

1515
using MongoDB.Driver;
1616
using System.Linq;
17-
using BsonDocumentCustom123 = MongoDB.Bson.BsonDocument;
18-
using BsonValueCustom123 = MongoDB.Bson.BsonValue;
19-
using BsonObjectIdCustom123 = MongoDB.Bson.BsonObjectId;
20-
using BsonTypeCustom123 = MongoDB.Bson.BsonType;
17+
using MongoDB.Bson;
2118

2219
namespace MongoDB.Analyzer.Helpers.Builders
2320
{
2421
public static class MqlGenerator
2522
{
26-
#pragma warning disable CS0169 // The field is never used
27-
#pragma warning disable IDE0051
28-
private static readonly BsonDocumentCustom123 s_dummyRef1;
29-
private static readonly BsonValueCustom123 s_dummyRef2;
30-
private static readonly BsonObjectIdCustom123 s_dummyRef3;
31-
private static readonly BsonTypeCustom123 s_dummyRef4;
32-
#pragma warning restore IDE0051 // The field is never used
33-
#pragma warning restore CS0169
34-
3523
private sealed class MqlGeneratorTemplateType
3624
{
3725
public int Field { get; set; }

src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using System.Linq;
1717
using MongoDB.Driver.Linq;
18+
using MongoDB.Bson;
1819

1920
namespace MongoDB.Analyzer.Helpers.Linq
2021
{

src/MongoDB.Analyzer/Core/TypesGeneratorHelper.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ internal static class TypesGeneratorHelper
2222
.AddUsings(
2323
Using("System"),
2424
Using("MongoDB.Bson"),
25-
Using("MongoDB.Bson.Serialization.Attributes"),
26-
Using("BsonTypeCustom123", "MongoDB.Bson.BsonType"),
27-
Using("BsonDocumentCustom123", "MongoDB.Bson.BsonDocument"),
28-
Using("BsonValueCustom123", "MongoDB.Bson.BsonValue"),
29-
Using("BsonObjectIdCustom123", "MongoDB.Bson.BsonObjectId"));
25+
Using("MongoDB.Bson.Serialization.Attributes"));
3026

3127

3228
private static readonly NamespaceDeclarationSyntax s_namespaceDeclarationSyntaxBuilders = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(MqlGeneratorSyntaxElements.Builders.MqlGeneratorNamespace));

src/MongoDB.Analyzer/Core/TypesProcessor.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ namespace MongoDB.Analyzer.Core;
1616

1717
internal sealed class TypesProcessor
1818
{
19-
private static readonly Dictionary<string, string> s_knownTypes = new()
20-
{
21-
{ "MongoDB.Bson.BsonDocument", "BsonDocumentCustom123" },
22-
{ "MongoDB.Bson.BsonValue", "BsonValueCustom123" },
23-
{ "MongoDB.Bson.BsonObjectId", "BsonObjectIdCustom123" },
24-
{ "MongoDB.Bson.BsonType", "BsonTypeCustom123" }
25-
};
26-
2719
private readonly Dictionary<string, (string NewName, MemberDeclarationSyntax NewDeclaration)> _processedTypes;
2820

2921
private int _nextTypeId = 0;
@@ -43,9 +35,9 @@ public string GetTypeSymbolToGeneratedTypeMapping(ITypeSymbol typeSymbol)
4335
}
4436

4537
var fullTypeName = GetFullName(typeSymbol);
46-
if (s_knownTypes.TryGetValue(fullTypeName, out var knowTypeName))
38+
if (typeSymbol.IsSupportedBsonType())
4739
{
48-
return knowTypeName;
40+
return typeSymbol.Name;
4941
}
5042

5143
if (_processedTypes.TryGetValue(fullTypeName, out var result))
@@ -64,9 +56,9 @@ public string ProcessTypeSymbol(ITypeSymbol typeSymbol)
6456
}
6557

6658
var fullTypeName = GetFullName(typeSymbol);
67-
if (s_knownTypes.TryGetValue(fullTypeName, out var knowTypeName))
59+
if (typeSymbol.IsSupportedBsonType())
6860
{
69-
return knowTypeName;
61+
return typeSymbol.Name;
7062
}
7163

7264
if (_processedTypes.TryGetValue(fullTypeName, out var pair))

src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@ namespace MongoDB.Analyzer.Core;
1717
internal static class SymbolExtensions
1818
{
1919
private const string AssemblyMongoDBDriver = "MongoDB.Driver";
20+
private const string NamespaceMongoDBBson = "MongoDB.Bson";
21+
2022
private const string NamespaceMongoDBDriver = "MongoDB.Driver";
2123
private const string NamespaceMongoDBLinq = "MongoDB.Driver.Linq";
2224
private const string NamespaceSystemLinq = "System.Linq";
2325

26+
private static readonly HashSet<string> s_knownBsonTypes = new()
27+
{
28+
"MongoDB.Bson.BsonDocument",
29+
"MongoDB.Bson.BsonValue",
30+
"MongoDB.Bson.BsonObjectId",
31+
"MongoDB.Bson.BsonType"
32+
};
33+
2434
private static readonly HashSet<string> s_supportedCollections = new()
2535
{
2636
"System.Collections.Generic.List<T>",
@@ -128,6 +138,10 @@ public static bool IsSupportedMongoCollectionType(this ITypeSymbol typeSymbol) =
128138
typeSymbol.TypeKind == TypeKind.Class &&
129139
!typeSymbol.IsAnonymousType;
130140

141+
public static bool IsSupportedBsonType(this ITypeSymbol typeSymbol) =>
142+
typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceMongoDBBson &&
143+
s_knownBsonTypes.Contains(typeSymbol.ToDisplayString());
144+
131145
public static bool IsString(this ITypeSymbol typeSymbol) =>
132146
typeSymbol?.SpecialType == SpecialType.System_String;
133147

0 commit comments

Comments
 (0)