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
55 changes: 44 additions & 11 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package checker
import (
"fmt"
"reflect"
"slices"
"strings"

"github.com/google/cel-go/common"
"github.com/google/cel-go/common/ast"
Expand Down Expand Up @@ -104,11 +106,15 @@ func (c *checker) check(e ast.Expr) {
func (c *checker) checkIdent(e ast.Expr) {
identName := e.AsIdent()
// Check to see if the identifier is declared.
if ident := c.env.LookupIdent(identName); ident != nil {
if ident := c.env.resolveSimpleIdent(identName); ident != nil {
name := strings.TrimPrefix(ident.Name(), ".")
if ident.requiresDisambiguation {
name = "." + name
}
c.setType(e, ident.Type())
c.setReference(e, ast.NewIdentReference(ident.Name(), ident.Value()))
c.setReference(e, ast.NewIdentReference(name, ident.Value()))
// Overwrite the identifier with its fully qualified name.
e.SetKindCase(c.NewIdent(e.ID(), ident.Name()))
e.SetKindCase(c.NewIdent(e.ID(), name))
return
}

Expand All @@ -119,18 +125,22 @@ func (c *checker) checkIdent(e ast.Expr) {
func (c *checker) checkSelect(e ast.Expr) {
sel := e.AsSelect()
// Before traversing down the tree, try to interpret as qualified name.
qname, found := containers.ToQualifiedName(e)
qualifiers, found := c.computeQualifiers(e)
if found {
ident := c.env.LookupIdent(qname)
ident := c.env.resolveQualifiedIdent(qualifiers...)
if ident != nil {
// We don't check for a TestOnly expression here since the `found` result is
// always going to be false for TestOnly expressions.

// Rewrite the node to be a variable reference to the resolved fully-qualified
// variable name.
name := ident.Name()
if ident.requiresDisambiguation {
name = "." + name
}
c.setType(e, ident.Type())
c.setReference(e, ast.NewIdentReference(ident.Name(), ident.Value()))
e.SetKindCase(c.NewIdent(e.ID(), ident.Name()))
c.setReference(e, ast.NewIdentReference(name, ident.Value()))
e.SetKindCase(c.NewIdent(e.ID(), name))
return
}
}
Expand All @@ -142,6 +152,29 @@ func (c *checker) checkSelect(e ast.Expr) {
c.setType(e, substitute(c.mappings, resultType, false))
}

// computeQualifiers computes the qualified names parts of a select expression.
func (c *checker) computeQualifiers(e ast.Expr) ([]string, bool) {
var qualifiers []string
for e.Kind() == ast.SelectKind {
sel := e.AsSelect()
// test only expressions are not considered for qualified name selection.
if sel.IsTestOnly() {
return qualifiers, false
}
// otherwise append the select field name to the qualifier list (reverse order)
qualifiers = append(qualifiers, sel.FieldName())
e = sel.Operand()
// If the next operand is an identifier, then append it, reverse the name sequence
// and return it to the caller.s
if e.Kind() == ast.IdentKind {
qualifiers = append(qualifiers, e.AsIdent())
slices.Reverse(qualifiers)
return qualifiers, true
}
}
return qualifiers, false
}

func (c *checker) checkOptSelect(e ast.Expr) {
// Collect metadata related to the opt select call packaged by the parser.
call := e.AsCall()
Expand Down Expand Up @@ -234,7 +267,7 @@ func (c *checker) checkCall(e ast.Expr) {
// Regular static call with simple name.
if !call.IsMemberFunction() {
// Check for the existence of the function.
fn := c.env.LookupFunction(fnName)
fn := c.env.lookupFunction(fnName)
if fn == nil {
c.errors.undeclaredReference(e.ID(), c.location(e), c.env.container.Name(), fnName)
c.setType(e, types.ErrorType)
Expand All @@ -256,7 +289,7 @@ func (c *checker) checkCall(e ast.Expr) {
qualifiedPrefix, maybeQualified := containers.ToQualifiedName(target)
if maybeQualified {
maybeQualifiedName := qualifiedPrefix + "." + fnName
fn := c.env.LookupFunction(maybeQualifiedName)
fn := c.env.lookupFunction(maybeQualifiedName)
if fn != nil {
// The function name is namespaced and so preserving the target operand would
// be an inaccurate representation of the desired evaluation behavior.
Expand All @@ -269,7 +302,7 @@ func (c *checker) checkCall(e ast.Expr) {

// Regular instance call.
c.check(target)
fn := c.env.LookupFunction(fnName)
fn := c.env.lookupFunction(fnName)
// Function found, attempt overload resolution.
if fn != nil {
c.resolveOverloadOrError(e, fn, target, args)
Expand Down Expand Up @@ -441,7 +474,7 @@ func (c *checker) checkCreateStruct(e ast.Expr) {
msgVal := e.AsStruct()
// Determine the type of the message.
resultType := types.ErrorType
ident := c.env.LookupIdent(msgVal.TypeName())
ident := c.env.resolveTypeIdent(msgVal.TypeName())
if ident == nil {
c.errors.undeclaredReference(
e.ID(), c.location(e), c.env.container.Name(), msgVal.TypeName())
Expand Down
156 changes: 153 additions & 3 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2229,9 +2229,9 @@ _&&_(_==_(list~type(list(dyn))^list,
decls.NewVariable("NotAMessage", types.NewNullableType(types.IntType)),
},
},
err: `ERROR: <input>:1:12: 'wrapper(int)' is not a type
| NotAMessage{}
| ...........^`,
err: `ERROR: <input>:1:12: undeclared reference to 'NotAMessage' (in container '')
| NotAMessage{}
| ...........^`,
},
{
in: `{}.map(c,[c,type(c)])`,
Expand Down Expand Up @@ -2262,6 +2262,156 @@ _&&_(_==_(list~type(list(dyn))^list,
@result~list(list(dyn))^@result)~list(list(dyn))`,
outType: types.NewListType(types.NewListType(types.DynType)),
},
{
in: `[{'z': 0}].exists(y, y.z == 0)`,
env: testEnv{
idents: []*decls.VariableDecl{
decls.NewVariable("cel.example.y", types.NewMapType(types.StringType, types.IntType)),
},
},
out: `__comprehension__(
// Variable
y,
// Target
[
{
"z"~string:0~int
}~map(string, int)
]~list(map(string, int)),
// Accumulator
@result,
// Init
false~bool,
// LoopCondition
@not_strictly_false(
!_(
@result~bool^@result
)~bool^logical_not
)~bool^not_strictly_false,
// LoopStep
_||_(
@result~bool^@result,
_==_(
y~map(string, int)^y.z~int,
0~int
)~bool^equals
)~bool^logical_or,
// Result
@result~bool^@result)~bool`,
outType: types.BoolType,
},
{
in: `[{'y': 0}].exists(x, x.y == 0)`,
env: testEnv{
idents: []*decls.VariableDecl{
decls.NewVariable("x", types.NewMapType(types.StringType, types.IntType)),
},
},
out: `__comprehension__(
// Variable
x,
// Target
[
{
"y"~string:0~int
}~map(string, int)
]~list(map(string, int)),
// Accumulator
@result,
// Init
false~bool,
// LoopCondition
@not_strictly_false(
!_(
@result~bool^@result
)~bool^logical_not
)~bool^not_strictly_false,
// LoopStep
_||_(
@result~bool^@result,
_==_(
x~map(string, int)^x.y~int,
0~int
)~bool^equals
)~bool^logical_or,
// Result
@result~bool^@result)~bool`,
outType: types.BoolType,
},
{
in: `[0].exists(x, x != .x)`,
env: testEnv{
idents: []*decls.VariableDecl{
decls.NewVariable("x", types.IntType),
},
},
out: `__comprehension__(
// Variable
x,
// Target
[
0~int
]~list(int),
// Accumulator
@result,
// Init
false~bool,
// LoopCondition
@not_strictly_false(
!_(
@result~bool^@result
)~bool^logical_not
)~bool^not_strictly_false,
// LoopStep
_||_(
@result~bool^@result,
_!=_(
x~int^x,
.x~int^.x
)~bool^not_equals
)~bool^logical_or,
// Result
@result~bool^@result)~bool`,
outType: types.BoolType,
},
{
in: `[{'z': 0}].exists(y, .y.z == y.z)`,
env: testEnv{
idents: []*decls.VariableDecl{
decls.NewVariable("y.z", types.IntType),
},
},
out: `__comprehension__(
// Variable
y,
// Target
[
{
"z"~string:0~int
}~map(string, int)
]~list(map(string, int)),
// Accumulator
@result,
// Init
false~bool,
// LoopCondition
@not_strictly_false(
!_(
@result~bool^@result
)~bool^logical_not
)~bool^not_strictly_false,
// LoopStep
_||_(
@result~bool^@result,
_==_(
.y.z~int^.y.z,
y~map(string, int)^y.z~int
)~bool^equals
)~bool^logical_or,
// Result
@result~bool^@result)~bool`,
outType: types.BoolType,
},
}
}

Expand Down
Loading