Git Product home page Git Product logo

cel-net's Introduction

Common Expression Language

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

Type declarations

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;
}

Environment Setup

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);   

CEL Expression Execution

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 Apache License.

cel-net's People

Contributors

lamarlugli avatar

Stargazers

 avatar Greg Engle avatar Ivan Josipovic avatar

Watchers

Steve. avatar  avatar Zdenek Ryska avatar

Forkers

evengard

cel-net's Issues

Lexing/Parsing errors should explicitly throw exceptions

Invalid expressions eg: "1+" or "join('a', 'b'" should throw an exception instead of proceeding with ANTLR's default recovery mechanisms.

Checking on celParser.NumberOfSyntaxErrors should be sufficient I believe. However it may not catch lexer issues (see antlr/antlr4#3849 for a discussion of parser/lexer defaults in antlr).

This should bring it inline with the behavior of cel-go, otherwise the behavior is determined by how ANTLR determines to recover from a bad parse - which may be surprising to the user.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.