An open API service indexing awesome lists of open source software.

https://github.com/rayokota/cel.net

Common Expression Language for .NET
https://github.com/rayokota/cel.net

cel common common-expression-language csharp expression expression-language language

Last synced: about 1 year ago
JSON representation

Common Expression Language for .NET

Awesome Lists containing this project

README

          

# Cel.NET - Common Expression Language for .NET

[![Build Status][github-actions-shield]][github-actions-link] [![NuGet Package](https://img.shields.io/nuget/v/Cel.NET.svg)](https://www.nuget.org/packages/Cel.NET)

[github-actions-shield]: https://github.com/rayokota/cel.net/actions/workflows/build.yml/badge.svg?branch=master
[github-actions-link]: https://github.com/rayokota/cel.net/actions

This is a C# port of the [Common Expression Language (CEL)](https://opensource.google/projects/cel), based on [CEL-Java](https://github.com/projectnessie/cel-java).

The CEL specification can be found [here](https://github.com/google/cel-spec).

## Getting started

A very simple start:

```csharp
using Cel.Checker;
using Cel.Tools;

public class MyClass
{
public void MyScriptUsage()
{
// Build the script factory
ScriptHost scriptHost = ScriptHost.NewBuilder().Build();

// create the script, will be parsed and checked
Script script = scriptHost.BuildScript("x + ' ' + y")
.WithDeclarations(
// Variable declarations - we need `x` and `y` in this example
Decls.NewVar("x", Decls.String),
Decls.NewVar("y", Decls.String))
.Build();

IDictionary arguments = new Dictionary();
arguments.Add("x", "hello");
arguments.Add("y", "world");

string result = script.Execute(arguments);

Console.WriteLine(result); // Prints "hello world"
}
}
```

## Protobuf and Json.NET and plain C# objects

Protobuf (via `Google.Protobuf`) objects and schema is supported out of the box.

It is also possible to use plain C# and Json.NET objects as arguments by using the
`Cel.Common.Types.Json.JsonRegistry`.

Code sample similar to the one above. It takes a user-provided object type `MyInput`.

```csharp
using Cel.Checker;
using Cel.Tools;

public class MyClass
{
public bool EvalWithJsonObject(MyInput input, string checkName)
{
// Build the script factory
ScriptHost scriptHost = ScriptHost.NewBuilder()
.Registry(JsonRegistry.NewRegistry())
.Build();

// Create the script, will be parsed and checked.
// It checks whether the property `Name` in the "Json-ized" class `MyInput` is
// equal to the value of `checkName`.
Script script = scriptHost.BuildScript("inp.Name == checkName")
// Variable declarations - we need `inp` + `checkName` in this example
.WithDeclarations(
Decls.NewVar("inp", Decls.NewObjectType(typeof(MyInput).FullName)),
Decls.NewVar("checkName", Decls.String))
// Register our Json-NET object input type
.WithTypes(typeof(MyInput))
.Build();

IDictionary arguments = new Dictionary();
arguments.Add("inp", input);
arguments.Add("checkName", checkName);

bool result = script.Execute(arguments);

return result;
}

}
```

Note that the Json.NET field-names are used as property names in Cel.NET. It is not necessary to
annotate "plain C#" classes with Json.NET attributes.