Skip to content

Commit 994bc50

Browse files
VS-99: Optimize MQL Generator to avoid having 1 using statement for each Bson Type (#38)
1 parent bd30514 commit 994bc50

File tree

5 files changed

+53
-58
lines changed

5 files changed

+53
-58
lines changed

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,20 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using MongoDB.Driver;
1615
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;
21-
using BsonTimeSpanCustom123 = MongoDB.Bson.Serialization.Options.TimeSpanUnits;
16+
using MongoDB.Bson;
17+
using MongoDB.Bson.Serialization.Options;
18+
using MongoDB.Driver;
2219

2320
namespace MongoDB.Analyzer.Helpers.Builders
2421
{
2522
public static class MqlGenerator
2623
{
27-
#pragma warning disable CS0169 // The field is never used
24+
#pragma warning disable CS0169 // These fields are never used, they are needed to ensure that the relevant usings are not accidently removed
2825
#pragma warning disable IDE0051
29-
private static readonly BsonDocumentCustom123 s_dummyRef1;
30-
private static readonly BsonValueCustom123 s_dummyRef2;
31-
private static readonly BsonObjectIdCustom123 s_dummyRef3;
32-
private static readonly BsonTypeCustom123 s_dummyRef4;
33-
private static readonly BsonTimeSpanCustom123 s_dummyRef5;
34-
#pragma warning restore IDE0051 // The field is never used
26+
private static readonly BsonType s_dummyRef1;
27+
private static readonly TimeSpanUnits s_dummyRef2;
28+
#pragma warning restore IDE0051 // These fields are never used, they are needed to ensure that the relevant usings are not accidently removed
3529
#pragma warning restore CS0169
3630

3731
private sealed class MqlGeneratorTemplateType

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414

1515
using System;
1616
using System.Linq;
17+
using MongoDB.Bson;
18+
using MongoDB.Bson.Serialization.Options;
1719
using MongoDB.Driver.Linq;
18-
using BsonTypeCustom123 = MongoDB.Bson.BsonType;
19-
using BsonTimeSpanCustom123 = MongoDB.Bson.Serialization.Options.TimeSpanUnits;
2020

2121
namespace MongoDB.Analyzer.Helpers.Linq
2222
{
2323
public static class MqlGenerator
2424
{
25-
#pragma warning disable CS0169 // The field is never used
25+
#pragma warning disable CS0169 // These fields are never used, they are needed to ensure that the relevant usings are not accidently removed
2626
#pragma warning disable IDE0051
27-
private static readonly BsonTypeCustom123 s_dummyRef1;
28-
private static readonly BsonTimeSpanCustom123 s_dummyRef2;
29-
#pragma warning restore IDE0051 // The field is never used
27+
private static readonly BsonType s_dummyRef1;
28+
private static readonly TimeSpanUnits s_dummyRef2;
29+
#pragma warning restore IDE0051 // These fields are never used, they are needed to ensure that the relevant usings are not accidently removed
3030
#pragma warning restore CS0169
3131

3232
private sealed class MqlGeneratorTemplateType

src/MongoDB.Analyzer/Core/TypesGeneratorHelper.cs

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

3228

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

src/MongoDB.Analyzer/Core/TypesProcessor.cs

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

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

3021
private int _nextTypeId = 0;
@@ -73,17 +64,17 @@ public string ProcessTypeSymbol(ITypeSymbol typeSymbol)
7364
}
7465

7566
var fullTypeName = GetFullName(typeSymbol);
76-
if (s_knownBsonTypes.TryGetValue(fullTypeName, out var knowTypeName))
67+
if (typeSymbol.IsSupportedBsonType(fullTypeName))
7768
{
78-
return (knowTypeName, fullTypeName);
69+
return (typeSymbol.Name, fullTypeName);
7970
}
8071

8172
if (_processedTypes.TryGetValue(fullTypeName, out var result))
8273
{
8374
return (result.NewName, fullTypeName);
8475
}
8576

86-
if (typeSymbol.IsSupportedSystemType())
77+
if (typeSymbol.IsSupportedSystemType(fullTypeName))
8778
{
8879
return (fullTypeName, fullTypeName);
8980
}
@@ -236,7 +227,7 @@ private void GenerateFields(ITypeSymbol typeSymbol, List<MemberDeclarationSyntax
236227
var attributeLists = bsonAttributeList != null && bsonAttributeList.Attributes.AnySafe() ?
237228
SyntaxFactory.List<AttributeListSyntax>(SyntaxFactory.SingletonSeparatedList<AttributeListSyntax>(bsonAttributeList)) :
238229
SyntaxFactory.List<AttributeListSyntax>();
239-
230+
240231
var fieldDeclaration = SyntaxFactory.FieldDeclaration(
241232
attributeLists: attributeLists,
242233
modifiers: SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)),
@@ -349,7 +340,7 @@ private ExpressionSyntax GenerateExpressionFromBsonAttributeArgumentInfo(TypedCo
349340

350341
private ExpressionSyntax HandleEnumInBsonAttributeArgument(object value, ITypeSymbol typeSymbol) =>
351342
SyntaxFactoryUtilities.GetCastConstantExpression(ProcessTypeSymbol(typeSymbol), value);
352-
343+
353344
private LiteralExpressionSyntax HandlePrimitiveInBsonAttributeArgument(object value) =>
354345
SyntaxFactoryUtilities.GetConstantExpression(value);
355346

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

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,13 @@ namespace MongoDB.Analyzer.Core;
1717
internal static class SymbolExtensions
1818
{
1919
private const string AssemblyMongoDBDriver = "MongoDB.Driver";
20+
private const string NamespaceMongoDBBson = "MongoDB.Bson";
2021
private const string NamespaceMongoDBBsonAttributes = "MongoDB.Bson.Serialization.Attributes";
2122
private const string NamespaceMongoDBDriver = "MongoDB.Driver";
2223
private const string NamespaceMongoDBLinq = "MongoDB.Driver.Linq";
2324
private const string NamespaceSystem = "System";
2425
private const string NamespaceSystemLinq = "System.Linq";
2526

26-
private static readonly HashSet<string> s_supportedCollections = new()
27-
{
28-
"System.Collections.Generic.IEnumerable<T>",
29-
"System.Collections.Generic.IList<T>",
30-
"System.Collections.Generic.List<T>"
31-
};
32-
3327
private static readonly HashSet<string> s_supportedBsonAttributes = new()
3428
{
3529
"BsonConstructorAttribute",
@@ -50,6 +44,22 @@ internal static class SymbolExtensions
5044
"BsonTimeSpanOptionsAttribute"
5145
};
5246

47+
private static readonly HashSet<string> s_supportedBsonTypes = new()
48+
{
49+
"MongoDB.Bson.BsonDocument",
50+
"MongoDB.Bson.BsonObjectId",
51+
"MongoDB.Bson.BsonType",
52+
"MongoDB.Bson.BsonValue",
53+
"MongoDB.Bson.Serialization.Options.TimeSpanUnits"
54+
};
55+
56+
private static readonly HashSet<string> s_supportedCollections = new()
57+
{
58+
"System.Collections.Generic.IEnumerable<T>",
59+
"System.Collections.Generic.IList<T>",
60+
"System.Collections.Generic.List<T>"
61+
};
62+
5363
private static readonly HashSet<string> s_supportedSystemTypes = new()
5464
{
5565
"System.DateTimeKind",
@@ -150,24 +160,16 @@ public static bool IsIQueryable(this ITypeSymbol typeSymbol) =>
150160
public static bool IsMongoQueryable(this ITypeSymbol typeSymbol) =>
151161
typeSymbol?.Name == "MongoQueryable";
152162

153-
public static bool IsSupportedCollection(this ITypeSymbol typeSymbol) =>
154-
typeSymbol is INamedTypeSymbol namedTypeSymbol &&
155-
s_supportedCollections.Contains(namedTypeSymbol.ConstructedFrom?.ToDisplayString());
156-
157-
public static bool IsSupportedMongoCollectionType(this ITypeSymbol typeSymbol) =>
158-
typeSymbol.TypeKind == TypeKind.Class &&
159-
!typeSymbol.IsAnonymousType;
163+
public static bool IsString(this ITypeSymbol typeSymbol) =>
164+
typeSymbol?.SpecialType == SpecialType.System_String;
160165

161166
public static bool IsSupportedBsonAttribute(this ITypeSymbol typeSymbol) =>
162167
s_supportedBsonAttributes.Contains(typeSymbol?.Name) &&
163168
typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceMongoDBBsonAttributes;
164169

165-
public static bool IsString(this ITypeSymbol typeSymbol) =>
166-
typeSymbol?.SpecialType == SpecialType.System_String;
167-
168-
public static bool IsSupportedSystemType(this ITypeSymbol typeSymbol) =>
169-
(typeSymbol.SpecialType != SpecialType.None || s_supportedSystemTypes.Contains(typeSymbol.ToDisplayString())) &&
170-
typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceSystem;
170+
public static bool IsSupportedBsonType(this ITypeSymbol typeSymbol, string fullTypeName) =>
171+
s_supportedBsonTypes.Contains(fullTypeName) &&
172+
(typeSymbol?.ContainingNamespace?.ToDisplayString().StartsWith(NamespaceMongoDBBson) ?? false);
171173

172174
public static bool IsSupportedBuilderType(this ITypeSymbol typeSymbol) =>
173175
typeSymbol?.TypeKind switch
@@ -178,6 +180,18 @@ TypeKind.Enum or
178180
_ => false
179181
};
180182

183+
public static bool IsSupportedCollection(this ITypeSymbol typeSymbol) =>
184+
typeSymbol is INamedTypeSymbol namedTypeSymbol &&
185+
s_supportedCollections.Contains(namedTypeSymbol.ConstructedFrom?.ToDisplayString());
186+
187+
public static bool IsSupportedMongoCollectionType(this ITypeSymbol typeSymbol) =>
188+
typeSymbol.TypeKind == TypeKind.Class &&
189+
!typeSymbol.IsAnonymousType;
190+
191+
public static bool IsSupportedSystemType(this ITypeSymbol typeSymbol, string fullTypeName) =>
192+
(typeSymbol.SpecialType != SpecialType.None || s_supportedSystemTypes.Contains(fullTypeName)) &&
193+
typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceSystem;
194+
181195
public static bool IsSupportedIMongoCollection(this ITypeSymbol typeSymbol) =>
182196
typeSymbol.IsIMongoCollection() &&
183197
typeSymbol is INamedTypeSymbol namedType &&

0 commit comments

Comments
 (0)