Bump cel-go to v0.10.0

This commit is contained in:
Joe Betz
2022-03-07 20:47:04 -05:00
parent f93be6584e
commit 2a6b85c395
66 changed files with 3332 additions and 817 deletions

View File

@@ -16,6 +16,7 @@ package cel
import (
"errors"
"fmt"
"sync"
"github.com/google/cel-go/checker"
@@ -89,13 +90,17 @@ type Env struct {
adapter ref.TypeAdapter
provider ref.TypeProvider
features map[int]bool
// program options tied to the environment.
progOpts []ProgramOption
// Internal parser representation
prsr *parser.Parser
// Internal checker representation
chk *checker.Env
chkErr error
once sync.Once
chk *checker.Env
chkErr error
chkOnce sync.Once
// Program options tied to the environment
progOpts []ProgramOption
}
// NewEnv creates a program environment configured with the standard library of CEL functions and
@@ -147,18 +152,22 @@ func (e *Env) Check(ast *Ast) (*Ast, *Issues) {
pe, _ := AstToParsedExpr(ast)
// Construct the internal checker env, erroring if there is an issue adding the declarations.
e.once.Do(func() {
ce := checker.NewEnv(e.Container, e.provider)
ce.EnableDynamicAggregateLiterals(true)
if e.HasFeature(FeatureDisableDynamicAggregateLiterals) {
ce.EnableDynamicAggregateLiterals(false)
}
err := ce.Add(e.declarations...)
e.chkOnce.Do(func() {
ce, err := checker.NewEnv(e.Container, e.provider,
checker.HomogeneousAggregateLiterals(
e.HasFeature(featureDisableDynamicAggregateLiterals)),
checker.CrossTypeNumericComparisons(
e.HasFeature(featureCrossTypeNumericComparisons)))
if err != nil {
e.chkErr = err
} else {
e.chk = ce
return
}
err = ce.Add(e.declarations...)
if err != nil {
e.chkErr = err
return
}
e.chk = ce
})
// The once call will ensure that this value is set or nil for all invocations.
if e.chkErr != nil {
@@ -207,11 +216,10 @@ func (e *Env) CompileSource(src common.Source) (*Ast, *Issues) {
return nil, iss
}
checked, iss2 := e.Check(ast)
iss = iss.Append(iss2)
if iss.Err() != nil {
return nil, iss
if iss2.Err() != nil {
return nil, iss2
}
return checked, iss
return checked, iss2
}
// Extend the current environment with additional options to produce a new Env.
@@ -280,8 +288,8 @@ func (e *Env) Extend(opts ...EnvOption) (*Env, error) {
// HasFeature checks whether the environment enables the given feature
// flag, as enumerated in options.go.
func (e *Env) HasFeature(flag int) bool {
_, has := e.features[flag]
return has
enabled, has := e.features[flag]
return has && enabled
}
// Parse parses the input expression value `txt` to a Ast and/or a set of Issues.
@@ -301,7 +309,7 @@ func (e *Env) Parse(txt string) (*Ast, *Issues) {
// It is possible to have both non-nil Ast and Issues values returned from this call; however,
// the mere presence of an Ast does not imply that it is valid for use.
func (e *Env) ParseSource(src common.Source) (*Ast, *Issues) {
res, errs := parser.ParseWithMacros(src, e.macros)
res, errs := e.prsr.Parse(src)
if len(errs.GetErrors()) > 0 {
return nil, &Issues{errs: errs}
}
@@ -325,11 +333,6 @@ func (e *Env) Program(ast *Ast, opts ...ProgramOption) (Program, error) {
return newProgram(e, ast, optSet)
}
// SetFeature sets the given feature flag, as enumerated in options.go.
func (e *Env) SetFeature(flag int) {
e.features[flag] = true
}
// TypeAdapter returns the `ref.TypeAdapter` configured for the environment.
func (e *Env) TypeAdapter() ref.TypeAdapter {
return e.adapter
@@ -402,6 +405,16 @@ func (e *Env) ResidualAst(a *Ast, details *EvalDetails) (*Ast, error) {
return checked, nil
}
// EstimateCost estimates the cost of a type checked CEL expression using the length estimates of input data and
// extension functions provided by estimator.
func (e *Env) EstimateCost(ast *Ast, estimator checker.CostEstimator) (checker.CostEstimate, error) {
checked, err := AstToCheckedExpr(ast)
if err != nil {
return checker.CostEstimate{}, fmt.Errorf("EsimateCost could not inspect Ast: %v", err)
}
return checker.Cost(checked, estimator), nil
}
// configure applies a series of EnvOptions to the current environment.
func (e *Env) configure(opts []EnvOption) (*Env, error) {
// Customized the environment using the provided EnvOption values. If an error is
@@ -413,6 +426,14 @@ func (e *Env) configure(opts []EnvOption) (*Env, error) {
return nil, err
}
}
prsrOpts := []parser.Option{parser.Macros(e.macros...)}
if e.HasFeature(featureEnableMacroCallTracking) {
prsrOpts = append(prsrOpts, parser.PopulateMacroCalls(true))
}
e.prsr, err = parser.NewParser(prsrOpts...)
if err != nil {
return nil, err
}
return e, nil
}
@@ -454,6 +475,9 @@ func (i *Issues) Append(other *Issues) *Issues {
if i == nil {
return other
}
if other == nil {
return i
}
return NewIssues(i.errs.Append(other.errs.GetErrors()))
}