Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo_Adapter: type requests fixed to return all subtypes instead of exact type matches #167

Merged
merged 4 commits into from
Jan 10, 2023
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
40 changes: 37 additions & 3 deletions Mongo_Engine/Convert/MongoQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,76 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.Engine.Reflection;
using BH.oM.Base.Attributes;
using BH.oM.Data.Requests;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace BH.Engine.Adapters.Mongo
{
public static partial class Convert
{
/***************************************************/
/**** Public Interface ****/
/**** Public Interface ****/
/***************************************************/

[Description("Converts a given IRequest into Bson document.")]
[Input("query", "Request to be converted to a Bson document.")]
[Output("document", "Bson document that resulted from converting the input request.")]
public static BsonDocument IToMongoQuery(this IRequest query)
{
return ToMongoQuery(query as dynamic);
}


/***************************************************/
/**** Private Methods - Curves ****/
/**** Public Methods ****/
/***************************************************/

[Description("Converts a given CustomRequest into Bson document.")]
[Input("query", "Request to be converted to a Bson document.")]
[Output("document", "Bson document that resulted from converting the input request.")]
public static BsonDocument ToMongoQuery(this CustomRequest query)
{
return BsonDocument.Parse(query.Body);
}

/***************************************************/

[Description("Converts a given FilterRequest into Bson document.")]
[Input("query", "Request to be converted to a Bson document.")]
[Output("document", "Bson document that resulted from converting the input request.")]
public static BsonDocument ToMongoQuery(this FilterRequest query)
{
BsonDocument document = new BsonDocument();

// Define the match
if (query.Equalities == null)
query.Equalities = new Dictionary<string, object>();

BsonDocument equalities = query.Equalities.ToBsonDocument();

if (query.Type != null)
equalities["_t"] = query.Type.ToString();
{
BsonArray typeEqualities = new BsonArray();

// Add requested type if it is not an interface
if (!query.Type.IsInterface)
typeEqualities.Add(new BsonDocument { { "_t", query.Type.ToString() } });

// Add subtypes of the requested type
foreach (Type subtype in query.Type.Subtypes())
{
typeEqualities.Add(new BsonDocument { { "_t", subtype.ToString() } });
}

equalities.Add(new BsonElement("$or", typeEqualities));
}

if (query.Tag != "")
equalities["__Tag__"] = query.Tag;

Expand All @@ -71,6 +100,9 @@ public static BsonDocument ToMongoQuery(this FilterRequest query)

/***************************************************/

[Description("Converts a given IResultRequest into Bson document.")]
[Input("query", "Request to be converted to a Bson document.")]
[Output("document", "Bson document that resulted from converting the input request.")]
public static BsonDocument ToMongoQuery(this IResultRequest query)
{
BsonDocument document = new BsonDocument();
Expand All @@ -97,6 +129,8 @@ public static BsonDocument ToMongoQuery(this IResultRequest query)
}


/***************************************************/
/**** Fallback Methods ****/
/***************************************************/

private static BsonDocument ToMongoQuery(this IRequest query)
Expand Down
7 changes: 6 additions & 1 deletion Mongo_Engine/Mongo_Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<Description>https://github.com/BHoM/Mongo_Toolkit</Description>
<Description>https://github.com/BHoM/Mongo_Toolkit</Description>
<Version>5.0.0</Version>
<Authors>BHoM</Authors>
<Copyright>Copyright © https://github.com/BHoM</Copyright>
Expand Down Expand Up @@ -41,6 +41,11 @@
<HintPath>C:\ProgramData\BHoM\Assemblies\Data_oM.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Reflection_Engine">
<HintPath>C:\ProgramData\BHoM\Assemblies\Reflection_Engine.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="Serialiser_Engine">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\ProgramData\BHoM\Assemblies\Serialiser_Engine.dll</HintPath>
Expand Down