Gaip.Net.Mongo 0.2.1

dotnet add package Gaip.Net.Mongo --version 0.2.1
                    
NuGet\Install-Package Gaip.Net.Mongo -Version 0.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Gaip.Net.Mongo" Version="0.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Gaip.Net.Mongo" Version="0.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Gaip.Net.Mongo" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Gaip.Net.Mongo --version 0.2.1
                    
#r "nuget: Gaip.Net.Mongo, 0.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Gaip.Net.Mongo@0.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Gaip.Net.Mongo&version=0.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Gaip.Net.Mongo&version=0.2.1
                    
Install as a Cake Tool

Google API Improvement Proposal (GAIP) Utilities for C#

This project is an attempt to create a simple to use library that implements Google's API Improvement Proposals. Currently, only AIP-160 is being implemented, but features like sorting could follow. The project is still very fresh, so no stability is guaranteed outside of what is tested in the provided unit tests.

Packages

Currently, the following packages are available:

Package Prerelease Stable
Gaip.Net.Core NuGet Badge NuGet Badge
Gaip.Net.Mongo NuGet Badge NuGet Badge
Gaip.Net.Linq NuGet Badge NuGet Badge

Usage

This library is mainly built around performing queries in Mongo. For example:

var filterDefinition = FilterBuilder
    .FromString("foo=\"bar\" OR foo!=\"baz\"")
    .UseAdapter(new MongoFilterAdapter<object>())
    .Build();

IMongoCollection<SomeThing> myCollection = ... // However you want to instantiate your collection

myCollection.Find(filterDefinition);

The resulting query would then look something like this:

{ $or : [ { foo : "bar" }, { foo : { $ne: "baz" } } ] }

Or if you are using alternative names:

public class MyClass{
    [BsonId]
    public string Foo { get; set; }
    
    [BsonElement("baz")]
    public string Bar { get; set; }
}

var filterDefinition = FilterBuilder
    .FromString("Foo=\"abc\" AND Bar=\"def\"")
    .UseAdapter(new MongoFilterAdapter<MyClass>())
    .Build();
    
myCollection.Find(filterDefinition);

Result:

{ $and : [ { _id : "abc" }, { baz : "def" } ] }

Check the unit tests for more examples.

LINQ

The library also provides an adapter for generating expressions that can be used in .Where() calls. This is useful for filtering collections using LINQ. For example:

var list = new List<MyClass> 
{
    new () { Id = 1, foo = "bar" },
    new () { Id = 2, foo = "baz" },
    new () { Id = 3, foo = "fizzbuzz" }
};

var filter = FilterBuilder
    .FromString("foo=\"bar\" OR foo!=\"baz\"")
    .UseAdapter(new LinqFilterAdapter<MyClass>())
    .Build();
    
list.Where(filter).Select(x => x.Id).ToList(); // [1, 3]

Blacklisting (or whitelisting)

In order to prevent unsafe queries, you can use the blacklisting or whitelisting functionality, such as below:

var filterResult = FilterBuilder.FromString("bar=\"baz\"")
    .UseAdapter(new MongoFilterAdapter<MyClass>())
    .UseWhitelist(x => x.Foo)
    .Build();

filterResult.IsQueryAllowed == false;
myCollection.Find(filterResult.Value); // Throws InvalidOperationException

var filterResult = FilterBuilder.FromString("foo=\"baz\"")
    .UseAdapter(new MongoFilterAdapter<MyClass>())
    .UseBlacklist(x => x.Foo)
    .Build();
    
filterResult.IsQueryAllowed == false;
myCollection.Find(filterResult.Value); // Throws InvalidOperationException

Extending functionality

At the moment, only queries that are compatible with the Mongo C# driver are supported. You are of course free to extend this library to support other databases. The easiest way to do this is to implement the IFilterAdapter interface. See also the MongoFilterAdapter class.

Development notes

This project depends on Antlr4, and grammar files are specified in src/Gaip.Net.Core/Grammar. The easiest way to work with this is to use an Antlr4 plugin for your IDE. For example, the Rider plugin. You can configure the plugin to export the generated Antlr classes to src/Gaip.Net.Core/Antlr4 so they will be ignored by Git.

You can also generate the required files with the following command:

antlr4 -Dlanguage=CSharp ./src/Gaip.Net.Core/Grammar/Filter.g4 -o ./src/Gaip.Net.Core/Antlr4 -visitor

Also, in the base of the project there is a script to do this for you:

./build_grammar.sh

See also

License.md

Other Notes

You can find the grammar files in src/Gaip.Net.Core/Grammar. You should configure your ANTLR config to put files in src/Gaip.Net.Core/.... I put it in a src/Gaip.Net.Core/Antlr4 directory for convenience.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.1 19,497 4/13/2022
0.2.1-rc.104 178 6/17/2022
0.2.1-rc.102 170 6/17/2022
0.2.1-rc.100 168 6/17/2022
0.2.1-rc.97 176 6/3/2022
0.2.1-rc.94 193 4/13/2022
0.1.1-rc.91 192 4/13/2022
0.1.1-rc.85 192 3/28/2022
0.1.1-rc.82 189 3/24/2022
0.1.1-rc.79 184 3/24/2022
0.1.0 580 3/24/2022
0.1.0-rc.77 192 3/24/2022
0.1.0-prerelease0001 246 3/24/2022
0.0.1-alpha.56 185 3/23/2022
0.0.1-alpha.48 190 3/22/2022
0.0.1-alpha.44 191 3/16/2022
0.0.1-alpha.40 187 3/16/2022
0.0.1-alpha.38 188 3/16/2022
0.0.1-alpha.33 190 3/15/2022
0.0.1-alpha 236 3/14/2022