Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: Add syntax for escaped field selectors. #1002

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
46 changes: 46 additions & 0 deletions cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,52 @@ func TestParserRecursionLimit(t *testing.T) {
}
}

func TestQuotedFields(t *testing.T) {
testCases := []struct {
expr string
errorSubstr string
out ref.Val
}{
{
expr: "{'$key': 64}.`$key`",
out: types.Int(64),
},
{
expr: "{'$key': 64}.`$key2`",
errorSubstr: "no such key: $key2",
},
{
expr: "has({'$key': 64}.`$key`)",
out: types.True,
},
{
expr: "has({'$key': 64}.`$key2`)",
out: types.False,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.expr, func(t *testing.T) {
env := testEnv(t, ParserRecursionLimit(10))
out, err := interpret(t, env,
tc.expr, map[string]any{})

if tc.errorSubstr != "" {
if err == nil || !strings.Contains(err.Error(), tc.errorSubstr) {
t.Fatalf("prg.Eval() wanted error containing '%s' got %v", tc.errorSubstr, err)
}
}

if tc.out != nil {
if tc.out != out {
t.Errorf("prg.Eval() wanted %v got %v", tc.out, out)
}
}
})

}
}

func TestDynamicDispatch(t *testing.T) {
env := testEnv(t,
HomogeneousAggregateLiterals(),
Expand Down
18 changes: 18 additions & 0 deletions ext/protos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@ func TestProtos(t *testing.T) {
{expr: `proto.getExt(ExampleType{}, google.expr.proto2.test.nested_example) == ExampleType{}`},
{expr: `proto.getExt(ExampleType{}, google.expr.proto2.test.ExtendedExampleType.extended_examples) == []`},
{expr: `proto.getExt(ExampleType{}, google.expr.proto2.test.ExtendedExampleType.enum_ext) == GlobalEnum.GOO`},
{expr: "ExampleType{`in`: 64}.`in` == 64"},
// TODO(): can't assign extension fields, only read. Unclear if needed.
// ExampleType{`google.expr.proto2.test.int32_ext`: 42}.`google.expr.proto2.test.int32_ext` == 42
{expr: `proto.getExt(msg, google.expr.proto2.test.int32_ext) == 42`},
{expr: "msg.`google.expr.proto2.test.int32_ext` == 42"},
{expr: `proto.getExt(msg, google.expr.proto2.test.int32_wrapper_ext) == 21`},
{expr: "msg.`google.expr.proto2.test.int32_wrapper_ext` == 21"},
{expr: `proto.hasExt(msg, google.expr.proto2.test.nested_example)`},
{expr: "has(msg.`google.expr.proto2.test.nested_example`)"},
{expr: `proto.getExt(msg, google.expr.proto2.test.nested_example) == ExampleType{name: 'nested'}`},
{expr: "msg.`google.expr.proto2.test.nested_example` == ExampleType{name: 'nested'}"},
{expr: `proto.getExt(msg, google.expr.proto2.test.ExtendedExampleType.extended_examples) == ['example1', 'example2']`},
{expr: "msg.`google.expr.proto2.test.ExtendedExampleType.extended_examples` == ['example1', 'example2']"},
{expr: `proto.getExt(msg, google.expr.proto2.test.ExtendedExampleType.enum_ext) == GlobalEnum.GAZ`},
{expr: "msg.`google.expr.proto2.test.ExtendedExampleType.enum_ext` == GlobalEnum.GAZ"},
}

env := testProtosEnv(t)
Expand Down Expand Up @@ -174,6 +183,15 @@ func TestProtosParseErrors(t *testing.T) {
| proto.getExt(ExtendedExampleType{}, has(google.expr.proto2.test.int32_ext))
| .......................................^`,
},
{
expr: `ExampleType{}.in`,
err: `ERROR: <input>:1:15: Syntax error: no viable alternative at input '.in'
| ExampleType{}.in
| ..............^
ERROR: <input>:1:17: Syntax error: mismatched input '<EOF>' expecting {'[', '{', '(', '.', '-', '!', 'true', 'false', 'null', NUM_FLOAT, NUM_INT, NUM_UINT, STRING, BYTES, IDENTIFIER}
| ExampleType{}.in
| ................^`,
},
}
env := testProtosEnv(t)
for i, tst := range protosTests {
Expand Down
13 changes: 10 additions & 3 deletions parser/gen/CEL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ unary

member
: primary # PrimaryExpr
| member op='.' (opt='?')? id=IDENTIFIER # Select
| member op='.' (opt='?')? id=escapeIdent # Select
| member op='.' id=IDENTIFIER open='(' args=exprList? ')' # MemberCall
| member op='[' (opt='?')? index=expr ']' # Index
;

primary
: leadingDot='.'? id=IDENTIFIER (op='(' args=exprList? ')')? # IdentOrGlobalCall
: leadingDot='.'? id=IDENTIFIER # Ident
| leadingDot='.'? id=IDENTIFIER (op='(' args=exprList? ')') # GlobalCall
| '(' e=expr ')' # Nested
| op='[' elems=listInit? ','? ']' # CreateList
| op='{' entries=mapInitializerList? ','? '}' # CreateStruct
Expand All @@ -80,13 +81,18 @@ fieldInitializerList
;

optField
: (opt='?')? IDENTIFIER
: (opt='?')? escapeIdent
;

mapInitializerList
: keys+=optExpr cols+=':' values+=expr (',' keys+=optExpr cols+=':' values+=expr)*
;

escapeIdent
: id=IDENTIFIER # SimpleIdentifier
| id=ESC_IDENTIFIER # EscapedIdentifier
;

optExpr
: (opt='?')? e=expr
;
Expand Down Expand Up @@ -198,3 +204,4 @@ STRING
BYTES : ('b' | 'B') STRING;

IDENTIFIER : (LETTER | '_') ( LETTER | DIGIT | '_')*;
ESC_IDENTIFIER : '`' (BACKSLASH '`' | BACKSLASH BACKSLASH | ~('\r'|'\n'|'\\'|'`'))* '`';
5 changes: 4 additions & 1 deletion parser/gen/CEL.interp

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions parser/gen/CEL.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ NUM_UINT=33
STRING=34
BYTES=35
IDENTIFIER=36
ESC_IDENTIFIER=37
'=='=1
'!='=2
'in'=3
Expand Down
5 changes: 4 additions & 1 deletion parser/gen/CELLexer.interp

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions parser/gen/CELLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ NUM_UINT=33
STRING=34
BYTES=35
IDENTIFIER=36
ESC_IDENTIFIER=37
'=='=1
'!='=2
'in'=3
Expand Down
28 changes: 23 additions & 5 deletions parser/gen/cel_base_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions parser/gen/cel_base_visitor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading