google / cel-spec

Common Expression Language -- specification and binary representation
https://cel.dev
Apache License 2.0
2.79k stars 222 forks source link

Discrepancy in checked.proto docs vs content #170

Open jsannemo opened 3 years ago

jsannemo commented 3 years ago

The documenation for the FunctionDecl and Overload messages in https://github.com/googleapis/googleapis/blob/master/google/api/expr/v1alpha1/checked.proto includes

and may optionally specify a function definition in terms of CEL expressions.

but as far as I can see no such field is present in the proto. Is this field planned to be added?

TristonianJones commented 3 years ago

Thanks for the catch. We do not intend to support function definitions within the AST or syntax. This comment is an artifact of CEL's early development when we tried to merge three different languages into a common core, and later realized that the function definitions in CEL didn't need to be part of the core proto.

An application of CEL could certainly do this if they wanted to, but it would be separate from the core proto.

    // FunctionSource defines a signature whose parameters are treated as an extension
    // of the variables available in the CEL environment as well as a source expression
    // string.
    message FunctionSource {
       google.api.expr.v1alpha1.Decl signature = 1;
       google.type.Expr source = 2;
    }

    // Function is a compiled FunctionSource along with the signature which may be used
    // to compose this function with other CEL expressions.
    message Function {
        google.api.expr.v1alpha1.Decl signature = 1;
        google.api.expr.v1alpha1.CheckedExpr definition = 2;
    }

The https://github.com/google/cel-policy-templates-go project is probably closest to implementing such a feature within it's Environment feature: https://github.com/google/cel-policy-templates-go/blob/master/policy/model/env.go. You'll have to squint a little to see it, but the YAML source definition of an environment is compiled into a set of CEL Decl values and it doesn't take too much imagination to realize that you could provide constant values and function definitions as part of the YAML source and bind a CEL function for use by other CEL expressions.

I'll leave the ticket open until we can fix the documentation.