Skip to content

Commit 53c8f40

Browse files
authored
VS-110: Update Analyzer to 2.21 driver (#50)
1 parent c28486a commit 53c8f40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+294
-279
lines changed

MongoDB.Analyzer.sln

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
2727
EndProjectSection
2828
EndProject
2929
Global
30-
GlobalSection(SharedMSBuildProjectFiles) = preSolution
31-
tests\MongoDB.Analyzer.Tests.Common.TestCases\MongoDB.Analyzer.Tests.Common.TestCases.projitems*{1845551f-deff-4082-b2db-3548a44f83af}*SharedItemsImports = 13
32-
tests\MongoDB.Analyzer.Tests.Common\MongoDB.Analyzer.Tests.Common.projitems*{41218684-a8e3-41ac-8f85-58b9b2f43b09}*SharedItemsImports = 5
33-
tests\MongoDB.Analyzer.Tests.Common\MongoDB.Analyzer.Tests.Common.projitems*{4f59be68-9ab5-4dcc-8049-04ad7bb36ab6}*SharedItemsImports = 13
34-
tests\MongoDB.Analyzer.Tests.Common.TestCases\MongoDB.Analyzer.Tests.Common.TestCases.projitems*{acb908a0-7f5c-44da-b9d2-ccfbcaeadd1d}*SharedItemsImports = 5
35-
tests\MongoDB.Analyzer.Tests.Common\MongoDB.Analyzer.Tests.Common.projitems*{acb908a0-7f5c-44da-b9d2-ccfbcaeadd1d}*SharedItemsImports = 5
36-
EndGlobalSection
3730
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3831
Debug|Any CPU = Debug|Any CPU
3932
Release|Any CPU = Release|Any CPU
@@ -55,8 +48,8 @@ Global
5548
{41218684-A8E3-41AC-8F85-58B9B2F43B09}.Debug|Any CPU.Build.0 = Debug|Any CPU
5649
{41218684-A8E3-41AC-8F85-58B9B2F43B09}.Release|Any CPU.ActiveCfg = Release|Any CPU
5750
{41218684-A8E3-41AC-8F85-58B9B2F43B09}.Release|Any CPU.Build.0 = Release|Any CPU
58-
{C9D49CAA-3F1F-4969-BDC1-0D17F9DB2B65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59-
{C9D49CAA-3F1F-4969-BDC1-0D17F9DB2B65}.Debug|Any CPU.Build.0 = Debug|Any CPU
51+
{C9D49CAA-3F1F-4969-BDC1-0D17F9DB2B65}.Debug|Any CPU.ActiveCfg = DRIVER_2_19|Any CPU
52+
{C9D49CAA-3F1F-4969-BDC1-0D17F9DB2B65}.Debug|Any CPU.Build.0 = DRIVER_2_19|Any CPU
6053
{C9D49CAA-3F1F-4969-BDC1-0D17F9DB2B65}.Release|Any CPU.ActiveCfg = Release|Any CPU
6154
{C9D49CAA-3F1F-4969-BDC1-0D17F9DB2B65}.Release|Any CPU.Build.0 = Release|Any CPU
6255
EndGlobalSection
@@ -75,4 +68,11 @@ Global
7568
GlobalSection(ExtensibilityGlobals) = postSolution
7669
SolutionGuid = {92FC99D4-1527-4D7F-AD28-B0949CD5BE72}
7770
EndGlobalSection
71+
GlobalSection(SharedMSBuildProjectFiles) = preSolution
72+
tests\MongoDB.Analyzer.Tests.Common.TestCases\MongoDB.Analyzer.Tests.Common.TestCases.projitems*{1845551f-deff-4082-b2db-3548a44f83af}*SharedItemsImports = 13
73+
tests\MongoDB.Analyzer.Tests.Common\MongoDB.Analyzer.Tests.Common.projitems*{41218684-a8e3-41ac-8f85-58b9b2f43b09}*SharedItemsImports = 5
74+
tests\MongoDB.Analyzer.Tests.Common\MongoDB.Analyzer.Tests.Common.projitems*{4f59be68-9ab5-4dcc-8049-04ad7bb36ab6}*SharedItemsImports = 13
75+
tests\MongoDB.Analyzer.Tests.Common.TestCases\MongoDB.Analyzer.Tests.Common.TestCases.projitems*{acb908a0-7f5c-44da-b9d2-ccfbcaeadd1d}*SharedItemsImports = 5
76+
tests\MongoDB.Analyzer.Tests.Common\MongoDB.Analyzer.Tests.Common.projitems*{acb908a0-7f5c-44da-b9d2-ccfbcaeadd1d}*SharedItemsImports = 5
77+
EndGlobalSection
7878
EndGlobal

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ public static string Render<T, E>(IFindFluent<T, E> fluentDefinition)
5959
{
6060
return fluentDefinition.ToString();
6161
}
62+
63+
#if DRIVER_2_21_OR_GREATER
64+
public static string Render<T>(MongoDB.Driver.Search.SearchDefinition<T> searchDefinition)
65+
{
66+
var renderedBuildersDefinition = searchDefinition.Render(new MongoDB.Driver.Search.SearchDefinitionRenderContext<T>(BsonSerializer.LookupSerializer<T>(), BsonSerializer.SerializerRegistry));
67+
return renderedBuildersDefinition.ToString();
68+
}
69+
#elif DRIVER_2_19_OR_GREATER
70+
public static string Render<T>(MongoDB.Driver.Search.SearchDefinition<T> searchDefinition)
71+
{
72+
var renderedBuildersDefinition = searchDefinition.Render(BsonSerializer.LookupSerializer<T>(), BsonSerializer.SerializerRegistry);
73+
return renderedBuildersDefinition.ToString();
74+
}
75+
#endif
6276
}
6377
}
6478

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

Lines changed: 0 additions & 28 deletions
This file was deleted.

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

Lines changed: 0 additions & 23 deletions
This file was deleted.

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

Lines changed: 0 additions & 25 deletions
This file was deleted.

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Linq;
1717
using MongoDB.Bson;
1818
using MongoDB.Bson.Serialization.Options;
19+
using MongoDB.Driver;
1920
using MongoDB.Driver.Linq;
2021

2122
namespace MongoDB.Analyzer.Helpers.Linq
@@ -34,13 +35,11 @@ private sealed class MqlGeneratorTemplateType
3435
public Tuple<int, int> Field { get; set; }
3536
}
3637

37-
private static readonly IQueryableProvider s_queryableProvider = new IQueryableProviderV2();
38-
3938
public static string GetDriverVersion() => typeof(IMongoQueryable<>).Assembly.GetName().Version.ToString(3);
4039

4140
public static string GetMQL(bool isV3)
4241
{
43-
var queryable = s_queryableProvider.GetQueryable<MqlGeneratorTemplateType>(isV3);
42+
var queryable = QueryableProvider.GetQueryable<MqlGeneratorTemplateType>(isV3);
4443
var queryableWithExpression = queryable.Where(t => t.Field.Item1 == 1);
4544

4645
queryableWithExpression.FirstOrDefault();

src/MongoDB.Analyzer.Helpers/Linq/IQueryableProviderV3.cs renamed to src/MongoDB.Analyzer.Helpers/Linq/QueryableProvider.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717

1818
namespace MongoDB.Analyzer.Helpers.Linq
1919
{
20-
public sealed class IQueryableProviderV3 : IQueryableProvider
20+
public static class QueryableProvider
2121
{
22-
public IMongoQueryable<TDocument> GetQueryable<TDocument>(bool isV3)
22+
23+
#if DRIVER_2_14_OR_GREATER
24+
public static IMongoQueryable<TDocument> GetQueryable<TDocument>(bool isV3)
2325
{
2426
var provider = isV3 ? LinqProviderAdapter.V3 : LinqProviderAdapter.V2;
2527
return provider.AsQueryable(new MongoCollectionMock<TDocument>(), null, new AggregateOptions());
2628
}
29+
#else
30+
public static IMongoQueryable<TDocument> GetQueryable<TDocument>(bool isV3) =>
31+
!isV3 ? (new MongoCollectionMock<TDocument>()).AsQueryable() : null;
32+
#endif
2733
}
2834
}

src/MongoDB.Analyzer.Helpers/MongoCollectionMock.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public sealed class MongoCollectionMock<TDocument> : MongoCollectionBase<TDocume
3434

3535
public override MongoCollectionSettings Settings => new MongoCollectionSettings();
3636

37+
#if DRIVER_2_21_OR_GREATER
38+
public override Driver.Search.IMongoSearchIndexManager SearchIndexes => throw new NotImplementedException();
39+
#endif
40+
3741
/// <inheritdoc />
3842
public override IAsyncCursor<TResult> Aggregate<TResult>(PipelineDefinition<TDocument, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default)
3943
{
@@ -305,25 +309,29 @@ public override async Task<TProjection> FindOneAndUpdateAsync<TProjection>(IClie
305309
}
306310

307311
/// <inheritdoc />
312+
[Obsolete]
308313
public override IAsyncCursor<TResult> MapReduce<TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions<TDocument, TResult> options = null, CancellationToken cancellationToken = default)
309314
{
310315
return new EmptyCursor<TResult>();
311316
}
312317

313318
/// <inheritdoc />
319+
[Obsolete]
314320
public override IAsyncCursor<TResult> MapReduce<TResult>(IClientSessionHandle session, BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions<TDocument, TResult> options = null, CancellationToken cancellationToken = default)
315321
{
316322
return new EmptyCursor<TResult>();
317323
}
318324

319325
/// <inheritdoc />
326+
[Obsolete]
320327
public override async Task<IAsyncCursor<TResult>> MapReduceAsync<TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions<TDocument, TResult> options = null, CancellationToken cancellationToken = default)
321328
{
322329
await Task.FromResult(1);
323330
return new EmptyCursor<TResult>();
324331
}
325332

326333
/// <inheritdoc />
334+
[Obsolete]
327335
public override async Task<IAsyncCursor<TResult>> MapReduceAsync<TResult>(IClientSessionHandle session, BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions<TDocument, TResult> options = null, CancellationToken cancellationToken = default)
328336
{
329337
await Task.FromResult(1);
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
43
<TargetFramework>netstandard2.0</TargetFramework>
54
<AssemblyName>DynamicProxyGenAssembly2</AssemblyName>
5+
<Configurations>DRIVER_2_12;DRIVER_2_14;DRIVER_2_19;DRIVER_2_21</Configurations>
6+
</PropertyGroup>
7+
8+
<PropertyGroup Condition="'$(Configuration)' == 'DRIVER_2_14'">
9+
<DefineConstants>DRIVER_2_14_OR_GREATER</DefineConstants>
610
</PropertyGroup>
711

12+
<PropertyGroup Condition="'$(Configuration)' == 'DRIVER_2_19'">
13+
<DefineConstants>DRIVER_2_14_OR_GREATER</DefineConstants>
14+
<DefineConstants>DRIVER_2_19_OR_GREATER</DefineConstants>
15+
</PropertyGroup>
16+
17+
<PropertyGroup Condition="'$(Configuration)' == 'DRIVER_2_21'">
18+
<DefineConstants>DRIVER_2_21_OR_GREATER</DefineConstants>
19+
</PropertyGroup>
20+
821
<ItemGroup>
9-
<PackageReference Include="MongoDB.Driver" Version="2.19.0" />
22+
<PackageReference Include="MongoDB.Driver" Version="2.12.4" Condition="'$(Configuration)' == 'DRIVER_2_12'" />
23+
<PackageReference Include="MongoDB.Driver" Version="2.14.0" Condition="'$(Configuration)' == 'DRIVER_2_14'" />
24+
<PackageReference Include="MongoDB.Driver" Version="2.19.0" Condition="'$(Configuration)' == 'DRIVER_2_19'" />
25+
<PackageReference Include="MongoDB.Driver" Version="2.21.0" Condition="'$(Configuration)' == 'DRIVER_2_21'" />
1026
</ItemGroup>
11-
1227
</Project>

src/MongoDB.Analyzer/Core/Builders/AnalysisCodeGenerator.cs

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

15+
using MongoDB.Analyzer.Core.HelperResources;
1516
using MongoDB.Analyzer.Core.Utilities;
1617
using static MongoDB.Analyzer.Core.HelperResources.ResourcesUtilities;
1718

1819
namespace MongoDB.Analyzer.Core.Builders;
1920

2021
internal static class AnalysisCodeGenerator
2122
{
22-
private static readonly SyntaxTree[] s_helpersSyntaxTrees;
23-
private static readonly SyntaxTree s_renderer_2_19_and_higher;
23+
private static readonly SyntaxTreesCache s_syntaxTreesCache;
24+
2425
private static readonly BuildersMqlGeneratorTemplateBuilder.SyntaxElements s_mqlGeneratorSyntaxElements;
25-
private static readonly ParseOptions s_parseOptions;
26+
private static readonly CSharpParseOptions s_parseOptions;
2627

2728
static AnalysisCodeGenerator()
2829
{
29-
s_helpersSyntaxTrees = GetCommonCodeResources(ResourceNames.Builders.Renderer);
30-
s_renderer_2_19_and_higher = GetCodeResource(ResourceNames.Builders.Renderer_2_19_and_higher);
31-
3230
var mqlGeneratorSyntaxTree = GetCodeResource(ResourceNames.Builders.MqlGenerator);
3331
s_mqlGeneratorSyntaxElements = BuildersMqlGeneratorTemplateBuilder.CreateSyntaxElements(mqlGeneratorSyntaxTree);
34-
s_parseOptions = mqlGeneratorSyntaxTree.Options;
32+
33+
s_parseOptions = (CSharpParseOptions)mqlGeneratorSyntaxTree.Options;
34+
s_syntaxTreesCache = new SyntaxTreesCache(s_parseOptions, ResourceNames.Builders.Renderer);
3535
}
3636

3737
public static CompilationResult Compile(MongoAnalysisContext context, ExpressionsAnalysis buildersExpressionAnalysis)
@@ -46,17 +46,13 @@ public static CompilationResult Compile(MongoAnalysisContext context, Expression
4646
var typesSyntaxTree = TypesGeneratorHelper.GenerateTypesSyntaxTree(AnalysisType.Builders, buildersExpressionAnalysis.TypesDeclarations, s_parseOptions);
4747
var mqlGeneratorSyntaxTree = GenerateMqlGeneratorSyntaxTree(buildersExpressionAnalysis);
4848

49-
var syntaxTrees = new List<SyntaxTree>(s_helpersSyntaxTrees)
49+
var helperSyntaxTrees = s_syntaxTreesCache.GetSyntaxTrees(referencesContainer.Version);
50+
var syntaxTrees = new List<SyntaxTree>(helperSyntaxTrees)
5051
{
5152
typesSyntaxTree,
5253
mqlGeneratorSyntaxTree
5354
};
5455

55-
if (referencesContainer.Version >= BuildersAnalysisConstants.Version_2_19_and_higher)
56-
{
57-
syntaxTrees.Add(s_renderer_2_19_and_higher);
58-
}
59-
6056
var generatorType = AnalysisCodeGeneratorUtilities.CompileAndGetGeneratorType(AnalysisType.Builders, context, referencesContainer, syntaxTrees);
6157
if (generatorType == null)
6258
{
@@ -85,5 +81,5 @@ private static SyntaxTree GenerateMqlGeneratorSyntaxTree(ExpressionsAnalysis bui
8581
}
8682

8783
return testCodeBuilder.GenerateSyntaxTree();
88-
}
84+
}
8985
}

0 commit comments

Comments
 (0)