From 3210aa2a170d7fb3dbb15637bc2deb093bc6b095 Mon Sep 17 00:00:00 2001 From: Ravi-Raghavan Date: Thu, 13 Jul 2023 14:28:43 -0400 Subject: [PATCH 1/6] VS-99: Optimize MQL Generator to avoid having 1 Using Statement Per Bson Type --- .../Builders/MqlGenerator.cs | 16 ++++-------- .../Linq/MqlGenerator.cs | 8 +++--- .../Core/TypesGeneratorHelper.cs | 6 +---- src/MongoDB.Analyzer/Core/TypesProcessor.cs | 17 +++---------- .../Core/Utilities/SymbolExtensions.cs | 25 ++++++++++++++++++- 5 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs b/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs index ff51c8c0..2afece0f 100644 --- a/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs +++ b/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs @@ -12,13 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -using MongoDB.Driver; using System.Linq; -using BsonDocumentCustom123 = MongoDB.Bson.BsonDocument; -using BsonValueCustom123 = MongoDB.Bson.BsonValue; -using BsonObjectIdCustom123 = MongoDB.Bson.BsonObjectId; -using BsonTypeCustom123 = MongoDB.Bson.BsonType; -using BsonTimeSpanCustom123 = MongoDB.Bson.Serialization.Options.TimeSpanUnits; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Options; +using MongoDB.Driver; namespace MongoDB.Analyzer.Helpers.Builders { @@ -26,11 +23,8 @@ public static class MqlGenerator { #pragma warning disable CS0169 // The field is never used #pragma warning disable IDE0051 - private static readonly BsonDocumentCustom123 s_dummyRef1; - private static readonly BsonValueCustom123 s_dummyRef2; - private static readonly BsonObjectIdCustom123 s_dummyRef3; - private static readonly BsonTypeCustom123 s_dummyRef4; - private static readonly BsonTimeSpanCustom123 s_dummyRef5; + private static readonly BsonType s_dummyRef1; + private static readonly TimeSpanUnits s_dummyRef2; #pragma warning restore IDE0051 // The field is never used #pragma warning restore CS0169 diff --git a/src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs b/src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs index f03bde0f..7ea9b23f 100644 --- a/src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs +++ b/src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs @@ -14,9 +14,9 @@ using System; using System.Linq; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Options; using MongoDB.Driver.Linq; -using BsonTypeCustom123 = MongoDB.Bson.BsonType; -using BsonTimeSpanCustom123 = MongoDB.Bson.Serialization.Options.TimeSpanUnits; namespace MongoDB.Analyzer.Helpers.Linq { @@ -24,8 +24,8 @@ public static class MqlGenerator { #pragma warning disable CS0169 // The field is never used #pragma warning disable IDE0051 - private static readonly BsonTypeCustom123 s_dummyRef1; - private static readonly BsonTimeSpanCustom123 s_dummyRef2; + private static readonly BsonType s_dummyRef1; + private static readonly TimeSpanUnits s_dummyRef2; #pragma warning restore IDE0051 // The field is never used #pragma warning restore CS0169 diff --git a/src/MongoDB.Analyzer/Core/TypesGeneratorHelper.cs b/src/MongoDB.Analyzer/Core/TypesGeneratorHelper.cs index e0fbf611..c0c59e1d 100644 --- a/src/MongoDB.Analyzer/Core/TypesGeneratorHelper.cs +++ b/src/MongoDB.Analyzer/Core/TypesGeneratorHelper.cs @@ -23,11 +23,7 @@ internal static class TypesGeneratorHelper Using("System"), Using("MongoDB.Bson"), Using("MongoDB.Bson.Serialization.Attributes"), - Using("BsonTimeSpanCustom123", "MongoDB.Bson.Serialization.Options.TimeSpanUnits"), - Using("BsonTypeCustom123", "MongoDB.Bson.BsonType"), - Using("BsonDocumentCustom123", "MongoDB.Bson.BsonDocument"), - Using("BsonValueCustom123", "MongoDB.Bson.BsonValue"), - Using("BsonObjectIdCustom123", "MongoDB.Bson.BsonObjectId")); + Using("MongoDB.Bson.Serialization.Options")); private static readonly NamespaceDeclarationSyntax s_namespaceDeclarationSyntaxBuilders = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(MqlGeneratorSyntaxElements.Builders.MqlGeneratorNamespace)); diff --git a/src/MongoDB.Analyzer/Core/TypesProcessor.cs b/src/MongoDB.Analyzer/Core/TypesProcessor.cs index d2bcb0fb..37fa1fbe 100644 --- a/src/MongoDB.Analyzer/Core/TypesProcessor.cs +++ b/src/MongoDB.Analyzer/Core/TypesProcessor.cs @@ -16,15 +16,6 @@ namespace MongoDB.Analyzer.Core; internal sealed class TypesProcessor { - private static readonly Dictionary s_knownBsonTypes = new() - { - { "MongoDB.Bson.BsonDocument", "BsonDocumentCustom123" }, - { "MongoDB.Bson.BsonValue", "BsonValueCustom123" }, - { "MongoDB.Bson.BsonObjectId", "BsonObjectIdCustom123" }, - { "MongoDB.Bson.BsonType", "BsonTypeCustom123" }, - { "MongoDB.Bson.Serialization.Options.TimeSpanUnits", "BsonTimeSpanCustom123" } - }; - private readonly Dictionary _processedTypes; private int _nextTypeId = 0; @@ -73,9 +64,9 @@ public string ProcessTypeSymbol(ITypeSymbol typeSymbol) } var fullTypeName = GetFullName(typeSymbol); - if (s_knownBsonTypes.TryGetValue(fullTypeName, out var knowTypeName)) + if (typeSymbol.IsSupportedBsonType() || typeSymbol.IsSupportedBsonSerializationOption()) { - return (knowTypeName, fullTypeName); + return (typeSymbol.Name, fullTypeName); } if (_processedTypes.TryGetValue(fullTypeName, out var result)) @@ -236,7 +227,7 @@ private void GenerateFields(ITypeSymbol typeSymbol, List(SyntaxFactory.SingletonSeparatedList(bsonAttributeList)) : SyntaxFactory.List(); - + var fieldDeclaration = SyntaxFactory.FieldDeclaration( attributeLists: attributeLists, modifiers: SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)), @@ -349,7 +340,7 @@ private ExpressionSyntax GenerateExpressionFromBsonAttributeArgumentInfo(TypedCo private ExpressionSyntax HandleEnumInBsonAttributeArgument(object value, ITypeSymbol typeSymbol) => SyntaxFactoryUtilities.GetCastConstantExpression(ProcessTypeSymbol(typeSymbol), value); - + private LiteralExpressionSyntax HandlePrimitiveInBsonAttributeArgument(object value) => SyntaxFactoryUtilities.GetConstantExpression(value); diff --git a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs index 74cd5db2..2a6cd6de 100644 --- a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs +++ b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs @@ -16,13 +16,23 @@ namespace MongoDB.Analyzer.Core; internal static class SymbolExtensions { - private const string AssemblyMongoDBDriver = "MongoDB.Driver"; + private const string NamespaceMongoDBBson = "MongoDB.Bson"; private const string NamespaceMongoDBBsonAttributes = "MongoDB.Bson.Serialization.Attributes"; + private const string NamespaceMongoDBBsonSerializationOptions = "MongoDB.Bson.Serialization.Options"; + private const string AssemblyMongoDBDriver = "MongoDB.Driver"; private const string NamespaceMongoDBDriver = "MongoDB.Driver"; private const string NamespaceMongoDBLinq = "MongoDB.Driver.Linq"; private const string NamespaceSystem = "System"; private const string NamespaceSystemLinq = "System.Linq"; + private static readonly HashSet s_supportedBsonTypes = new() + { + "MongoDB.Bson.BsonDocument", + "MongoDB.Bson.BsonValue", + "MongoDB.Bson.BsonObjectId", + "MongoDB.Bson.BsonType" + }; + private static readonly HashSet s_supportedCollections = new() { "System.Collections.Generic.IEnumerable", @@ -50,6 +60,11 @@ internal static class SymbolExtensions "BsonTimeSpanOptionsAttribute" }; + private static readonly HashSet s_supportedBsonSerializationOptions = new() + { + "MongoDB.Bson.Serialization.Options.TimeSpanUnits" + }; + private static readonly HashSet s_supportedSystemTypes = new() { "System.DateTimeKind", @@ -162,6 +177,14 @@ public static bool IsSupportedBsonAttribute(this ITypeSymbol typeSymbol) => s_supportedBsonAttributes.Contains(typeSymbol?.Name) && typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceMongoDBBsonAttributes; + public static bool IsSupportedBsonSerializationOption(this ITypeSymbol typeSymbol) => + s_supportedBsonSerializationOptions.Contains(typeSymbol.ToDisplayString()) && + typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceMongoDBBsonSerializationOptions; + + public static bool IsSupportedBsonType(this ITypeSymbol typeSymbol) => + typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceMongoDBBson && + s_supportedBsonTypes.Contains(typeSymbol.ToDisplayString()); + public static bool IsString(this ITypeSymbol typeSymbol) => typeSymbol?.SpecialType == SpecialType.System_String; From 4466258831ce67e0cbc494fe545668d001e1464a Mon Sep 17 00:00:00 2001 From: Ravi-Raghavan Date: Thu, 13 Jul 2023 15:04:13 -0400 Subject: [PATCH 2/6] Sorted in Alphabetical Order --- .../Core/Utilities/SymbolExtensions.cs | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs index 2a6cd6de..97d3b5eb 100644 --- a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs +++ b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs @@ -16,30 +16,15 @@ namespace MongoDB.Analyzer.Core; internal static class SymbolExtensions { + private const string AssemblyMongoDBDriver = "MongoDB.Driver"; private const string NamespaceMongoDBBson = "MongoDB.Bson"; private const string NamespaceMongoDBBsonAttributes = "MongoDB.Bson.Serialization.Attributes"; private const string NamespaceMongoDBBsonSerializationOptions = "MongoDB.Bson.Serialization.Options"; - private const string AssemblyMongoDBDriver = "MongoDB.Driver"; private const string NamespaceMongoDBDriver = "MongoDB.Driver"; private const string NamespaceMongoDBLinq = "MongoDB.Driver.Linq"; private const string NamespaceSystem = "System"; private const string NamespaceSystemLinq = "System.Linq"; - private static readonly HashSet s_supportedBsonTypes = new() - { - "MongoDB.Bson.BsonDocument", - "MongoDB.Bson.BsonValue", - "MongoDB.Bson.BsonObjectId", - "MongoDB.Bson.BsonType" - }; - - private static readonly HashSet s_supportedCollections = new() - { - "System.Collections.Generic.IEnumerable", - "System.Collections.Generic.IList", - "System.Collections.Generic.List" - }; - private static readonly HashSet s_supportedBsonAttributes = new() { "BsonConstructorAttribute", @@ -65,6 +50,21 @@ internal static class SymbolExtensions "MongoDB.Bson.Serialization.Options.TimeSpanUnits" }; + private static readonly HashSet s_supportedBsonTypes = new() + { + "MongoDB.Bson.BsonDocument", + "MongoDB.Bson.BsonValue", + "MongoDB.Bson.BsonObjectId", + "MongoDB.Bson.BsonType" + }; + + private static readonly HashSet s_supportedCollections = new() + { + "System.Collections.Generic.IEnumerable", + "System.Collections.Generic.IList", + "System.Collections.Generic.List" + }; + private static readonly HashSet s_supportedSystemTypes = new() { "System.DateTimeKind", @@ -165,13 +165,8 @@ public static bool IsIQueryable(this ITypeSymbol typeSymbol) => public static bool IsMongoQueryable(this ITypeSymbol typeSymbol) => typeSymbol?.Name == "MongoQueryable"; - public static bool IsSupportedCollection(this ITypeSymbol typeSymbol) => - typeSymbol is INamedTypeSymbol namedTypeSymbol && - s_supportedCollections.Contains(namedTypeSymbol.ConstructedFrom?.ToDisplayString()); - - public static bool IsSupportedMongoCollectionType(this ITypeSymbol typeSymbol) => - typeSymbol.TypeKind == TypeKind.Class && - !typeSymbol.IsAnonymousType; + public static bool IsString(this ITypeSymbol typeSymbol) => + typeSymbol?.SpecialType == SpecialType.System_String; public static bool IsSupportedBsonAttribute(this ITypeSymbol typeSymbol) => s_supportedBsonAttributes.Contains(typeSymbol?.Name) && @@ -185,13 +180,6 @@ public static bool IsSupportedBsonType(this ITypeSymbol typeSymbol) => typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceMongoDBBson && s_supportedBsonTypes.Contains(typeSymbol.ToDisplayString()); - public static bool IsString(this ITypeSymbol typeSymbol) => - typeSymbol?.SpecialType == SpecialType.System_String; - - public static bool IsSupportedSystemType(this ITypeSymbol typeSymbol) => - (typeSymbol.SpecialType != SpecialType.None || s_supportedSystemTypes.Contains(typeSymbol.ToDisplayString())) && - typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceSystem; - public static bool IsSupportedBuilderType(this ITypeSymbol typeSymbol) => typeSymbol?.TypeKind switch { @@ -201,6 +189,18 @@ TypeKind.Enum or _ => false }; + public static bool IsSupportedCollection(this ITypeSymbol typeSymbol) => + typeSymbol is INamedTypeSymbol namedTypeSymbol && + s_supportedCollections.Contains(namedTypeSymbol.ConstructedFrom?.ToDisplayString()); + + public static bool IsSupportedMongoCollectionType(this ITypeSymbol typeSymbol) => + typeSymbol.TypeKind == TypeKind.Class && + !typeSymbol.IsAnonymousType; + + public static bool IsSupportedSystemType(this ITypeSymbol typeSymbol) => + (typeSymbol.SpecialType != SpecialType.None || s_supportedSystemTypes.Contains(typeSymbol.ToDisplayString())) && + typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceSystem; + public static bool IsSupportedIMongoCollection(this ITypeSymbol typeSymbol) => typeSymbol.IsIMongoCollection() && typeSymbol is INamedTypeSymbol namedType && From d520b600a138f575a289f79885408843b937abc4 Mon Sep 17 00:00:00 2001 From: Ravi-Raghavan Date: Thu, 13 Jul 2023 16:39:52 -0400 Subject: [PATCH 3/6] Made suggested PR Changes --- src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs | 4 ++-- src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs | 4 ++-- src/MongoDB.Analyzer/Core/TypesProcessor.cs | 2 +- .../Core/Utilities/SymbolExtensions.cs | 10 +++------- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs b/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs index 2afece0f..38a6d860 100644 --- a/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs +++ b/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs @@ -21,11 +21,11 @@ namespace MongoDB.Analyzer.Helpers.Builders { public static class MqlGenerator { -#pragma warning disable CS0169 // The field is never used +#pragma warning disable CS0169 // These fields are never used, they are needed to ensure that the relevant usings are not accidently removed #pragma warning disable IDE0051 private static readonly BsonType s_dummyRef1; private static readonly TimeSpanUnits s_dummyRef2; -#pragma warning restore IDE0051 // The field is never used +#pragma warning restore IDE0051 // These fields are never used, they are needed to ensure that the relevant usings are not accidently removed #pragma warning restore CS0169 private sealed class MqlGeneratorTemplateType diff --git a/src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs b/src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs index 7ea9b23f..8330ccce 100644 --- a/src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs +++ b/src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs @@ -22,11 +22,11 @@ namespace MongoDB.Analyzer.Helpers.Linq { public static class MqlGenerator { -#pragma warning disable CS0169 // The field is never used +#pragma warning disable CS0169 // These fields are never used, they are needed to ensure that the relevant usings are not accidently removed #pragma warning disable IDE0051 private static readonly BsonType s_dummyRef1; private static readonly TimeSpanUnits s_dummyRef2; -#pragma warning restore IDE0051 // The field is never used +#pragma warning restore IDE0051 // These fields are never used, they are needed to ensure that the relevant usings are not accidently removed #pragma warning restore CS0169 private sealed class MqlGeneratorTemplateType diff --git a/src/MongoDB.Analyzer/Core/TypesProcessor.cs b/src/MongoDB.Analyzer/Core/TypesProcessor.cs index 37fa1fbe..74e03a22 100644 --- a/src/MongoDB.Analyzer/Core/TypesProcessor.cs +++ b/src/MongoDB.Analyzer/Core/TypesProcessor.cs @@ -64,7 +64,7 @@ public string ProcessTypeSymbol(ITypeSymbol typeSymbol) } var fullTypeName = GetFullName(typeSymbol); - if (typeSymbol.IsSupportedBsonType() || typeSymbol.IsSupportedBsonSerializationOption()) + if (typeSymbol.IsSupportedBsonType(fullTypeName)) { return (typeSymbol.Name, fullTypeName); } diff --git a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs index 97d3b5eb..7aca9fc3 100644 --- a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs +++ b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs @@ -172,13 +172,9 @@ public static bool IsSupportedBsonAttribute(this ITypeSymbol typeSymbol) => s_supportedBsonAttributes.Contains(typeSymbol?.Name) && typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceMongoDBBsonAttributes; - public static bool IsSupportedBsonSerializationOption(this ITypeSymbol typeSymbol) => - s_supportedBsonSerializationOptions.Contains(typeSymbol.ToDisplayString()) && - typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceMongoDBBsonSerializationOptions; - - public static bool IsSupportedBsonType(this ITypeSymbol typeSymbol) => - typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceMongoDBBson && - s_supportedBsonTypes.Contains(typeSymbol.ToDisplayString()); + public static bool IsSupportedBsonType(this ITypeSymbol typeSymbol, string fullTypeName = default) => + typeSymbol?.ContainingNamespace?.ToDisplayString().StartsWith(NamespaceMongoDBBson) ?? false && + (s_supportedBsonTypes.Contains(fullTypeName) || s_supportedBsonSerializationOptions.Contains(fullTypeName)); public static bool IsSupportedBuilderType(this ITypeSymbol typeSymbol) => typeSymbol?.TypeKind switch From 03fb9a140980fe93774e3ffb8eaff57b6f4c0dea Mon Sep 17 00:00:00 2001 From: Ravi-Raghavan Date: Thu, 13 Jul 2023 16:40:48 -0400 Subject: [PATCH 4/6] Made suggested PR Changes --- src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs index 7aca9fc3..de79389c 100644 --- a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs +++ b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs @@ -19,7 +19,6 @@ internal static class SymbolExtensions private const string AssemblyMongoDBDriver = "MongoDB.Driver"; private const string NamespaceMongoDBBson = "MongoDB.Bson"; private const string NamespaceMongoDBBsonAttributes = "MongoDB.Bson.Serialization.Attributes"; - private const string NamespaceMongoDBBsonSerializationOptions = "MongoDB.Bson.Serialization.Options"; private const string NamespaceMongoDBDriver = "MongoDB.Driver"; private const string NamespaceMongoDBLinq = "MongoDB.Driver.Linq"; private const string NamespaceSystem = "System"; From f3af08d606d2503a9295c4ce1b95900d5b43466b Mon Sep 17 00:00:00 2001 From: Ravi-Raghavan Date: Thu, 13 Jul 2023 20:57:39 -0400 Subject: [PATCH 5/6] Made suggested PR Changes --- src/MongoDB.Analyzer/Core/TypesProcessor.cs | 2 +- .../Core/Utilities/SymbolExtensions.cs | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/MongoDB.Analyzer/Core/TypesProcessor.cs b/src/MongoDB.Analyzer/Core/TypesProcessor.cs index 74e03a22..8bca58cb 100644 --- a/src/MongoDB.Analyzer/Core/TypesProcessor.cs +++ b/src/MongoDB.Analyzer/Core/TypesProcessor.cs @@ -74,7 +74,7 @@ public string ProcessTypeSymbol(ITypeSymbol typeSymbol) return (result.NewName, fullTypeName); } - if (typeSymbol.IsSupportedSystemType()) + if (typeSymbol.IsSupportedSystemType(fullTypeName)) { return (fullTypeName, fullTypeName); } diff --git a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs index de79389c..2d9459d4 100644 --- a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs +++ b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs @@ -44,17 +44,13 @@ internal static class SymbolExtensions "BsonTimeSpanOptionsAttribute" }; - private static readonly HashSet s_supportedBsonSerializationOptions = new() - { - "MongoDB.Bson.Serialization.Options.TimeSpanUnits" - }; - private static readonly HashSet s_supportedBsonTypes = new() { "MongoDB.Bson.BsonDocument", "MongoDB.Bson.BsonValue", "MongoDB.Bson.BsonObjectId", - "MongoDB.Bson.BsonType" + "MongoDB.Bson.BsonType", + "MongoDB.Bson.Serialization.Options.TimeSpanUnits" }; private static readonly HashSet s_supportedCollections = new() @@ -171,9 +167,9 @@ public static bool IsSupportedBsonAttribute(this ITypeSymbol typeSymbol) => s_supportedBsonAttributes.Contains(typeSymbol?.Name) && typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceMongoDBBsonAttributes; - public static bool IsSupportedBsonType(this ITypeSymbol typeSymbol, string fullTypeName = default) => - typeSymbol?.ContainingNamespace?.ToDisplayString().StartsWith(NamespaceMongoDBBson) ?? false && - (s_supportedBsonTypes.Contains(fullTypeName) || s_supportedBsonSerializationOptions.Contains(fullTypeName)); + public static bool IsSupportedBsonType(this ITypeSymbol typeSymbol, string fullTypeName) => + s_supportedBsonTypes.Contains(fullTypeName) && + (typeSymbol?.ContainingNamespace?.ToDisplayString().StartsWith(NamespaceMongoDBBson) ?? false); public static bool IsSupportedBuilderType(this ITypeSymbol typeSymbol) => typeSymbol?.TypeKind switch @@ -192,8 +188,8 @@ public static bool IsSupportedMongoCollectionType(this ITypeSymbol typeSymbol) = typeSymbol.TypeKind == TypeKind.Class && !typeSymbol.IsAnonymousType; - public static bool IsSupportedSystemType(this ITypeSymbol typeSymbol) => - (typeSymbol.SpecialType != SpecialType.None || s_supportedSystemTypes.Contains(typeSymbol.ToDisplayString())) && + public static bool IsSupportedSystemType(this ITypeSymbol typeSymbol, string fullTypeName) => + (typeSymbol.SpecialType != SpecialType.None || s_supportedSystemTypes.Contains(fullTypeName)) && typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceSystem; public static bool IsSupportedIMongoCollection(this ITypeSymbol typeSymbol) => From 5fb488d66b048ed9bcf84c7bdabc450ffada81f9 Mon Sep 17 00:00:00 2001 From: Ravi-Raghavan Date: Thu, 13 Jul 2023 21:53:13 -0400 Subject: [PATCH 6/6] Made suggested PR Changes --- src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs index 2d9459d4..611f2695 100644 --- a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs +++ b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs @@ -47,9 +47,9 @@ internal static class SymbolExtensions private static readonly HashSet s_supportedBsonTypes = new() { "MongoDB.Bson.BsonDocument", - "MongoDB.Bson.BsonValue", "MongoDB.Bson.BsonObjectId", "MongoDB.Bson.BsonType", + "MongoDB.Bson.BsonValue", "MongoDB.Bson.Serialization.Options.TimeSpanUnits" };