Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cel/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ func AstToParsedExpr(a *Ast) (*exprpb.ParsedExpr, error) {
// Note, the conversion may not be an exact replica of the original expression, but will produce
// a string that is semantically equivalent and whose textual representation is stable.
func AstToString(a *Ast) (string, error) {
return parser.Unparse(a.NativeRep().Expr(), a.NativeRep().SourceInfo())
return ExprToString(a.NativeRep().Expr(), a.NativeRep().SourceInfo())
}

// ExprToString converts an AST Expr node back to a string using macro call tracking metadata from
// source info if any macros are encountered within the expression.
func ExprToString(e ast.Expr, info *ast.SourceInfo) (string, error) {
return parser.Unparse(e, info)
}

// RefValueToValue converts between ref.Val and google.api.expr.v1alpha1.Value.
Expand Down
48 changes: 48 additions & 0 deletions cel/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"google.golang.org/protobuf/proto"

"github.com/google/cel-go/checker/decls"
celast "github.com/google/cel-go/common/ast"
"github.com/google/cel-go/common/operators"
"github.com/google/cel-go/common/types"

proto3pb "github.com/google/cel-go/test/proto3pb"
Expand Down Expand Up @@ -145,6 +147,52 @@ func TestAstToString(t *testing.T) {
}
}

func TestExprToString(t *testing.T) {
stdEnv, err := NewEnv(EnableMacroCallTracking())
if err != nil {
t.Fatalf("NewEnv() failed: %v", err)
}
in := "[a, b].filter(i, (i > 0) ? (-i + 4) : i)"
ast, iss := stdEnv.Parse(in)
if iss.Err() != nil {
t.Fatalf("stdEnv.Parse(%q) failed: %v", in, iss.Err())
}
expr, err := ExprToString(ast.NativeRep().Expr(), ast.NativeRep().SourceInfo())
if err != nil {
t.Fatalf("ExprToString(ast) failed: %v", err)
}
if expr != in {
t.Errorf("got %v, wanted %v", expr, in)
}

// Test sub-expression unparsing.
navExpr := celast.NavigateAST(ast.NativeRep())
condExpr := celast.MatchDescendants(navExpr, celast.FunctionMatcher(operators.Conditional))[0]
want := `(i > 0) ? (-i + 4) : i`
expr, err = ExprToString(condExpr, ast.NativeRep().SourceInfo())
if err != nil {
t.Fatalf("ExprToString(ast) failed: %v", err)
}
if expr != want {
t.Errorf("got %v, wanted %v", expr, want)
}

// Also passes with a nil source info, but only because the sub-expr doesn't contain macro calls.
expr, err = ExprToString(condExpr, nil)
if err != nil {
t.Fatalf("ExprToString(ast) failed: %v", err)
}
if expr != want {
t.Errorf("got %v, wanted %v", expr, want)
}

// Fails do to missing macro information.
_, err = ExprToString(ast.NativeRep().Expr(), nil)
if err == nil {
t.Error("ExprToString() succeeded, wanted error")
}
}

func TestAstToStringNil(t *testing.T) {
expr, err := AstToString(nil)
if err == nil || !strings.Contains(err.Error(), "unsupported expr") {
Expand Down