Giter VIP home page Giter VIP logo

isgasho / cel-go Goto Github PK

View Code? Open in Web Editor NEW

This project forked from google/cel-go

0.0 2.0 0.0 2.6 MB

The Go implementation of the Common Expression Language (CEL). CEL is a non-Turing complete language designed to be portable and fast. It is well suited to embedded applications expression evaluation with familiar syntax and features, protocol buffer support, and not needing the sandboxing needed for a runtime like JavaScript or Lua.

License: Apache License 2.0

Python 3.36% Shell 0.12% Go 95.95% ANTLR 0.58%

cel-go's Introduction

Common Expression Language - Go Toolchain

Build Status Go Report Card

This repo contains the Go toolchain for the Common Expression Language (CEL). CEL is a non-Turing complete language designed to be portable and fast. It is best suited to applications where sandboxing a full-fledged language like JavaScript or Lua would be too resource intensive, but side-effect free dynamic computations are strongly desired.

Want to learn more?

Want to integrate CEL?

  • See GoDoc to learn how to integrate CEL into services written in Go. GoDoc
  • See the CEL C++ toolchain (under development) for information about how to integrate CEL evaluation into other environments.

Want to contribute?

How does CEL work?

Write an expression in the CEL syntax, then parse, check, and interpret.

Step Description
Parse Parses a string expression to a protocol buffer representation.
Check Type-checks a parsed expression against a given environment.
Interpret Evaluates parsed expressions against a set of inputs.

Type-checking an expression in an optional, but strongly encouraged step that can be used to reject some expressions as semantically invalid using static analysis. Additionally, the type-check produces some additional metadata related to function overload resolution and object field selection which may be used by the interpret step to speed up execution.

Example

The following example shows the parse, check, and intepretation of a simple program. After the parse and type-check steps, the service checks whether there are any errors that need to be reported.

import(
    "fmt"
    "log"

    "github.com/google/cel-go/cel"
    "github.com/google/cel-go/checker/decls"
)

func main() {
    // Variables used within this expression environment.
    decls := cel.Declarations(
        decls.NewIdent("i", decls.String, nil),
        decls.NewIdent("you", decls.String, nil))
    env, err := cel.NewEnv(decls)
    if err != nil {
        log.Fatalf("environment creation error: %s\n", err)
    }

    // Parse and type-check the expression.
    //
    // Detailed information about which issues/errors were encountered may be
    // found within the `iss`, but a non-nil `iss.Err()` value will contain a
    // detailed error string in human-readable form.
    p, iss := env.Parse(`"Hello " + you + "! I'm " + i + "."`)
    if iss != nil && iss.Err() != nil {
        log.Fatalln(iss.Err())
    }
    c, iss := env.Check(p)
    if iss != nil && iss.Err() != nil {
        log.Fatalln(iss.Err())
    }

    // Create the program, and evaluate it against some input.
    prg, err := env.Program(c)
    if err != nil {
        log.Fatalf("program creation error: %s\n", err)
    }

    // Note, the second result, the EvalDetails will be non-nil when using
    // either cel.OptTrackState or cel.OptExhaustiveEval (not shown). The
    // details value holds information about the evaluation, rather than the
    // output of the evaluation itself.
    out, _, err := prg.Eval(cel.Vars(map[string]interface{}{
        "i": "CEL",
        "you": "world"}))
    if err != nil {
        log.Fatalf("runtime error: %s\n", err)
    }

    // Hello world! I'm CEL.
    fmt.Println(out)
}

More examples like these can be found within the unit tests which can be run using Bazel:

bazel test ...

License

Released under the Apache License.

Disclaimer: This is not an official Google product.

cel-go's People

Contributors

ayas avatar eobrain avatar gabrielsz avatar hnovikov avatar jimlarson avatar jiyunyao avatar kpacha avatar lopezator avatar madi8229 avatar mrsaints avatar sefk avatar tristonianjones avatar ultrasaurus avatar

Watchers

 avatar  avatar

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.