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
- Host: GitHub
- URL: https://github.com/rayokota/cel.net
- Owner: rayokota
- License: apache-2.0
- Created: 2022-08-25T02:40:01.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-09-10T21:05:58.000Z (over 1 year ago)
- Last Synced: 2025-02-24T02:22:09.221Z (over 1 year ago)
- Topics: cel, common, common-expression-language, csharp, expression, expression-language, language
- Language: Starlark
- Homepage:
- Size: 12.1 MB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cel.NET - Common Expression Language for .NET
[![Build Status][github-actions-shield]][github-actions-link] [](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.