Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions src/MongoDB.Analyzer.Helpers/Builders/MqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/MongoDB.Analyzer.Helpers/Linq/MqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/MongoDB.Analyzer/Core/TypesGeneratorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
19 changes: 5 additions & 14 deletions src/MongoDB.Analyzer/Core/TypesProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ namespace MongoDB.Analyzer.Core;

internal sealed class TypesProcessor
{
private static readonly Dictionary<string, string> 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<string, (string NewName, MemberDeclarationSyntax NewDeclaration)> _processedTypes;

private int _nextTypeId = 0;
Expand Down Expand Up @@ -73,17 +64,17 @@ 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))
{
return (result.NewName, fullTypeName);
}

if (typeSymbol.IsSupportedSystemType())
if (typeSymbol.IsSupportedSystemType(fullTypeName))
{
return (fullTypeName, fullTypeName);
}
Expand Down Expand Up @@ -236,7 +227,7 @@ private void GenerateFields(ITypeSymbol typeSymbol, List<MemberDeclarationSyntax
var attributeLists = bsonAttributeList != null && bsonAttributeList.Attributes.AnySafe() ?
SyntaxFactory.List<AttributeListSyntax>(SyntaxFactory.SingletonSeparatedList<AttributeListSyntax>(bsonAttributeList)) :
SyntaxFactory.List<AttributeListSyntax>();

var fieldDeclaration = SyntaxFactory.FieldDeclaration(
attributeLists: attributeLists,
modifiers: SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)),
Expand Down Expand Up @@ -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);

Expand Down
54 changes: 34 additions & 20 deletions src/MongoDB.Analyzer/Core/Utilities/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> s_supportedCollections = new()
{
"System.Collections.Generic.IEnumerable<T>",
"System.Collections.Generic.IList<T>",
"System.Collections.Generic.List<T>"
};

private static readonly HashSet<string> s_supportedBsonAttributes = new()
{
"BsonConstructorAttribute",
Expand All @@ -50,6 +44,22 @@ internal static class SymbolExtensions
"BsonTimeSpanOptionsAttribute"
};

private static readonly HashSet<string> s_supportedBsonTypes = new()
{
"MongoDB.Bson.BsonDocument",
"MongoDB.Bson.BsonObjectId",
"MongoDB.Bson.BsonType",
"MongoDB.Bson.BsonValue",
"MongoDB.Bson.Serialization.Options.TimeSpanUnits"
};

private static readonly HashSet<string> s_supportedCollections = new()
{
"System.Collections.Generic.IEnumerable<T>",
"System.Collections.Generic.IList<T>",
"System.Collections.Generic.List<T>"
};

private static readonly HashSet<string> s_supportedSystemTypes = new()
{
"System.DateTimeKind",
Expand Down Expand Up @@ -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
Expand All @@ -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 &&
Expand Down