Skip to content

Commit

Permalink
Simplify definition of cel.Source (#512)
Browse files Browse the repository at this point in the history
This uses type aliasing to avoid the construction of a distinct type
name to expose the common.Source interface in cel, simplifying the API
docs and making it possible in future to share function literals between
packages.
  • Loading branch information
kortschak committed Mar 20, 2022
1 parent a8e5866 commit 07655ec
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 5 additions & 7 deletions cel/env.go
Expand Up @@ -32,9 +32,7 @@ import (
)

// Source interface representing a user-provided expression.
type Source interface {
common.Source
}
type Source = common.Source

// Ast representing the checked or unchecked expression, its source, and related metadata such as
// source position information.
Expand Down Expand Up @@ -210,7 +208,7 @@ func (e *Env) Compile(txt string) (*Ast, *Issues) {
// issues discovered during Check.
//
// Note, for parse-only uses of CEL use Parse.
func (e *Env) CompileSource(src common.Source) (*Ast, *Issues) {
func (e *Env) CompileSource(src Source) (*Ast, *Issues) {
ast, iss := e.ParseSource(src)
if iss.Err() != nil {
return nil, iss
Expand Down Expand Up @@ -294,7 +292,7 @@ func (e *Env) HasFeature(flag int) bool {

// Parse parses the input expression value `txt` to a Ast and/or a set of Issues.
//
// This form of Parse creates a common.Source value for the input `txt` and forwards to the
// This form of Parse creates a Source value for the input `txt` and forwards to the
// ParseSource method.
func (e *Env) Parse(txt string) (*Ast, *Issues) {
src := common.NewTextSource(txt)
Expand All @@ -308,15 +306,15 @@ 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) {
func (e *Env) ParseSource(src Source) (*Ast, *Issues) {
res, errs := e.prsr.Parse(src)
if len(errs.GetErrors()) > 0 {
return nil, &Issues{errs: errs}
}
// Manually create the Ast to ensure that the text source information is propagated on
// subsequent calls to Check.
return &Ast{
source: Source(src),
source: src,
expr: res.GetExpr(),
info: res.GetSourceInfo()}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions cel/io.go
Expand Up @@ -44,7 +44,7 @@ func CheckedExprToAst(checkedExpr *exprpb.CheckedExpr) *Ast {
// through future calls.
//
// Prefer CheckedExprToAst if loading expressions from storage.
func CheckedExprToAstWithSource(checkedExpr *exprpb.CheckedExpr, src common.Source) *Ast {
func CheckedExprToAstWithSource(checkedExpr *exprpb.CheckedExpr, src Source) *Ast {
refMap := checkedExpr.GetReferenceMap()
if refMap == nil {
refMap = map[int64]*exprpb.Reference{}
Expand Down Expand Up @@ -96,7 +96,7 @@ func ParsedExprToAst(parsedExpr *exprpb.ParsedExpr) *Ast {
// expression, or if you need to separately check a subset of an expression.
//
// Prefer ParsedExprToAst if loading expressions from storage.
func ParsedExprToAstWithSource(parsedExpr *exprpb.ParsedExpr, src common.Source) *Ast {
func ParsedExprToAstWithSource(parsedExpr *exprpb.ParsedExpr, src Source) *Ast {
si := parsedExpr.GetSourceInfo()
if si == nil {
si = &exprpb.SourceInfo{}
Expand Down

0 comments on commit 07655ec

Please sign in to comment.