diff --git a/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs b/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs index ff51c8c0..38a6d860 100644 --- a/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs +++ b/src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs @@ -12,26 +12,20 @@ // 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 { 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 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; -#pragma warning restore IDE0051 // The field is never used + private static readonly BsonType s_dummyRef1; + private static readonly TimeSpanUnits s_dummyRef2; +#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 f03bde0f..8330ccce 100644 --- a/src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs +++ b/src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs @@ -14,19 +14,19 @@ 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 { 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 BsonTypeCustom123 s_dummyRef1; - private static readonly BsonTimeSpanCustom123 s_dummyRef2; -#pragma warning restore IDE0051 // The field is never used + private static readonly BsonType s_dummyRef1; + private static readonly TimeSpanUnits s_dummyRef2; +#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/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..8bca58cb 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(fullTypeName)) { - return (knowTypeName, fullTypeName); + return (typeSymbol.Name, fullTypeName); } if (_processedTypes.TryGetValue(fullTypeName, out var result)) @@ -83,7 +74,7 @@ public string ProcessTypeSymbol(ITypeSymbol typeSymbol) return (result.NewName, fullTypeName); } - if (typeSymbol.IsSupportedSystemType()) + if (typeSymbol.IsSupportedSystemType(fullTypeName)) { return (fullTypeName, fullTypeName); } @@ -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..611f2695 100644 --- a/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs +++ b/src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs @@ -17,19 +17,13 @@ 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 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_supportedCollections = new() - { - "System.Collections.Generic.IEnumerable", - "System.Collections.Generic.IList", - "System.Collections.Generic.List" - }; - private static readonly HashSet s_supportedBsonAttributes = new() { "BsonConstructorAttribute", @@ -50,6 +44,22 @@ internal static class SymbolExtensions "BsonTimeSpanOptionsAttribute" }; + private static readonly HashSet s_supportedBsonTypes = new() + { + "MongoDB.Bson.BsonDocument", + "MongoDB.Bson.BsonObjectId", + "MongoDB.Bson.BsonType", + "MongoDB.Bson.BsonValue", + "MongoDB.Bson.Serialization.Options.TimeSpanUnits" + }; + + 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", @@ -150,24 +160,16 @@ 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) && typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceMongoDBBsonAttributes; - 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 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 @@ -178,6 +180,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, string fullTypeName) => + (typeSymbol.SpecialType != SpecialType.None || s_supportedSystemTypes.Contains(fullTypeName)) && + typeSymbol?.ContainingNamespace?.ToDisplayString() == NamespaceSystem; + public static bool IsSupportedIMongoCollection(this ITypeSymbol typeSymbol) => typeSymbol.IsIMongoCollection() && typeSymbol is INamedTypeSymbol namedType &&