-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSHARP-3979: Support LinqExtensions.Inject in LINQ3.
- Loading branch information
Showing
10 changed files
with
261 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Filters/AstRawFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using MongoDB.Bson; | ||
using MongoDB.Driver.Core.Misc; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters | ||
{ | ||
internal sealed class AstRawFilter : AstFilter | ||
{ | ||
private readonly BsonDocument _filter; | ||
|
||
public AstRawFilter(BsonDocument filter) | ||
{ | ||
_filter = Ensure.IsNotNull(filter, nameof(filter)); | ||
} | ||
|
||
public override AstNodeType NodeType => AstNodeType.RawFilter; | ||
public BsonValue Filter => _filter; | ||
|
||
public override AstNode Accept(AstNodeVisitor visitor) | ||
{ | ||
return visitor.VisitRawFilter(this); | ||
} | ||
|
||
public override BsonValue Render() | ||
{ | ||
return _filter; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/LinqExtensionsMethod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System.Reflection; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Reflection | ||
{ | ||
internal static class LinqExtensionsMethod | ||
{ | ||
// private static fields | ||
private static readonly MethodInfo __inject; | ||
|
||
// static constructor | ||
static LinqExtensionsMethod() | ||
{ | ||
__inject = ReflectionInfo.Method((FilterDefinition<object> filter) => filter.Inject()); | ||
} | ||
|
||
// public properties | ||
public static MethodInfo Inject => __inject; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...slators/ExpressionToFilterTranslators/MethodTranslators/InjectMethodToFilterTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters; | ||
using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Reflection; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.MethodTranslators | ||
{ | ||
internal static class InjectMethodToFilterTranslator | ||
{ | ||
// public static methods | ||
public static AstFilter Translate(TranslationContext context, MethodCallExpression expression) | ||
{ | ||
var method = expression.Method; | ||
var arguments = expression.Arguments; | ||
|
||
if (method.Is(LinqExtensionsMethod.Inject)) | ||
{ | ||
var filterExpression = arguments[0]; | ||
var filterDefinition = filterExpression.GetConstantValue<object>(expression); | ||
var filterDefinitionType = filterDefinition.GetType(); // we KNOW it's a FilterDefinition<TDocument> because of the Inject method signature | ||
var documentType = filterDefinitionType.GetGenericArguments()[0]; | ||
var serializerRegistry = BsonSerializer.SerializerRegistry; | ||
var documentSerializer = serializerRegistry.GetSerializer(documentType); // TODO: is this the right serializer? | ||
var renderMethodArgumentTypes = new[] | ||
{ | ||
typeof(IBsonSerializer<>).MakeGenericType(documentType), | ||
typeof(IBsonSerializerRegistry), | ||
typeof(LinqProvider) | ||
}; | ||
var renderMethod = filterDefinitionType.GetMethod("Render", renderMethodArgumentTypes); | ||
var renderedFilter = (BsonDocument)renderMethod.Invoke(filterDefinition, new object[] { documentSerializer, serializerRegistry, LinqProvider.V3 }); | ||
return AstFilter.Raw(renderedFilter); | ||
} | ||
|
||
throw new ExpressionNotSupportedException(expression); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationTests/Jira/CSharp3979Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System.Linq; | ||
using MongoDB.Driver.Linq; | ||
using Xunit; | ||
|
||
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira | ||
{ | ||
public class CSharp3979Tests : Linq3IntegrationTest | ||
{ | ||
[Fact] | ||
public void Inject_should_work() | ||
{ | ||
var client = DriverTestConfiguration.Linq3Client; | ||
var database = client.GetDatabase("test"); | ||
var collection = database.GetCollection<C>("test"); | ||
|
||
var filter = Builders<C>.Filter.Eq(c => c.X, 1); | ||
var queryable = collection.AsQueryable().Where(x => filter.Inject()); | ||
|
||
var stages = Translate(collection, queryable); | ||
var expectedStages = new[] | ||
{ | ||
"{ $match : { X : 1 } }" | ||
}; | ||
AssertStages(stages, expectedStages); | ||
} | ||
|
||
[Fact] | ||
public void Inject_embedded_in_an_expression_should_work() | ||
{ | ||
var client = DriverTestConfiguration.Linq3Client; | ||
var database = client.GetDatabase("test"); | ||
var collection = database.GetCollection<C>("test"); | ||
|
||
var filter = Builders<C>.Filter.Lte(c => c.X, 10); | ||
var queryable = collection.AsQueryable().Where(x => x.X >= 1 && filter.Inject()); | ||
|
||
var stages = Translate(collection, queryable); | ||
var expectedStages = new[] | ||
{ | ||
"{ $match : { $and : [{ X : { $gte : 1 } }, { X : { $lte : 10 } }] } }" | ||
}; | ||
AssertStages(stages, expectedStages); | ||
} | ||
|
||
private class C | ||
{ | ||
public int Id { get; set; } | ||
public int X { get; set; } | ||
} | ||
} | ||
} |