![]() |
VOOZH | about |
dotnet add package Cel --version 0.3.3
NuGet\Install-Package Cel -Version 0.3.3
<PackageReference Include="Cel" Version="0.3.3" />
<PackageVersion Include="Cel" Version="0.3.3" />Directory.Packages.props
<PackageReference Include="Cel" />Project file
paket add Cel --version 0.3.3
#r "nuget: Cel, 0.3.3"
#:package Cel@0.3.3
#addin nuget:?package=Cel&version=0.3.3Install as a Cake Addin
#tool nuget:?package=Cel&version=0.3.3Install as a Cake Tool
The Common Expression Language (CEL) implements common semantics for expression evaluation, enabling different applications to more easily interoperate.
This project is a native C# imlementation of the CEL Spec available Here
The Common Expression Language (CEL) is a simple expression language built on top of protocol buffer types. If you want to reference and use your own classes, they must be defined as a protocol buffer message and the descriptor registered in the CEL startup.
Suppose a type defined as follows (using protocol buffer syntax):
package mypackage;
message Account {
string account_number = 1;
string display_name = 2;
double overdraftLimit = 3;
double balance = 4;
...
}
message Transaction {
string transaction_number = 1;
double withdrawal = 2;
}
You will initialize the CEL environment with the file descriptors for your application. These are generated using the Google.Protobuf library. You can use Grpc.Tools to compile them automatically in your application.
The default namespace is optional. It is used to simplify some CEL expressions when creating or casting types.
// build a list of the file descriptors in your application.
var fileDescriptors = new[]
{
AccountReflection.Descriptor
};
var typeRegistry = Google.Protobuf.Reflection.TypeRegistry.FromFiles(fileDescriptors);
// the default namespace is optional.
var defaultNamespace = "mypackage";
var celEnvironment = new CelEnvironment(fileDescriptors, defaultNamespace);
You define your own CEL Expression. The example below will return a boolean.
// An expression compatible with CEL-spec
var celExpression = "account.balance >= transaction.withdrawal || (account.overdraftProtection && account.overdraftLimit >= transaction.withdrawal - account.balance)";
You also need to define your variables in your application.
// Example with an Account and Transaction type
var account = new Account() {
// define your own values here
...
};
var transaction = new Transction() {
// define your own values here
...
};
// define the variables that will be passed into the CEL application.
// the keys in the dictionary correspond to the variables defined in the CEL Expression.
var variables = new Dictionary<string, object?>();
variables.Add("account", account);
variables.Add("transaction", transaction);
// define the CEL Context. This compiles the expression into a delegate.
// you should cache this delegate because it is reusable and will avoid recompiling the expression.
var celProgramDelegate = celEnvironment.Compile(celExpression);
// now execute the expression
// you can invoke this function as many times as you want with different values for the variables.
var result = celProgramDelegate.Invoke(variables);
// the result is an object. You can cast it to whatever type you expect to be returned from the expression. In this example it is a boolean.
You can also define custom CEL functions.
private static object? MyFunction(object?[] value)
{
var doubleArg = (double) value[0];
//put your custom function logic here.
//in this example, we are expecting one argument as a double type
return "some return value that can be any type you want";
}
You register the custom CEL functions like this:
var celEnvironment = new CelEnvironment(fileDescriptors, defaultNamespace);
// the first argument is the function name that will be exposed to the cel Expression
// the second argument defines the types expected by the function
// the third argument is the pointer to the function that will be executed
celEnvironment.RegisterFunction("my_function", new[] { typeof(double) }, MyFunction);
// functions must be registered before you parse the cel expression.
You can use the functions in an expression like this:
var celExpression = "account.balance > my_function(account.overdraftLimit)";
Refer to the CEL-spec documentation for more examples.
Released under the .
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 2 NuGet packages that depend on Cel:
| Package | Downloads |
|---|---|
|
ProtoValidate
C# implementation of protovalidate. Supports ProtoValidate v1.0.0. |
|
|
NArk.Core
Core services for the Ark protocol .NET SDK. Includes spending, batch management, VTXO synchronization, sweeping, wallet infrastructure, and gRPC transport. |
This package is not used by any popular GitHub repositories.