Skip to content

Commit

Permalink
Handle escaped strings
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyhb committed Jan 5, 2024
1 parent 65b33c4 commit 921f6fc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
20 changes: 9 additions & 11 deletions lift.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ type LiftedArgs interface {
// expressions to increase cache hit rates.
func liftLiterals(expr string) (string, LiftedArgs) {
// TODO: Lift numeric literals out of expressions.
// If this contains an escape sequence (eg. `\` or `\'`), skip the lifting
// of literals out of the expression.
if strings.Contains(expr, `\"`) || strings.Contains(expr, `\'`) {
return expr, nil
}

lp := liftParser{expr: expr}
return lp.lift()
}
Expand Down Expand Up @@ -103,23 +97,27 @@ func (l *liftParser) addLiftedVar(val argMapValue) {
func (l *liftParser) consumeString(quoteChar byte) argMapValue {
offset := l.idx
length := 0

for l.idx < len(l.expr) {
char := l.expr[l.idx]

// Grab the next char for evaluation.
l.idx++

if char == '\\' && l.peek() == quoteChar {
// If we're escaping the quote character, ignore it.
l.idx++
length++
l.idx += 2
length += 2
continue
}

if char == quoteChar {
// Skip over the end quote.
l.idx++
// Return the substring offset/length
return argMapValue{offset, length}
}

// Grab the next char for evaluation.
l.idx++

// Only now has the length of the inner quote increased.
length++
}
Expand Down
8 changes: 5 additions & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1086,9 +1086,11 @@ func TestParsedCELAST(t *testing.T) {

p := NewCachingParser(env, nil)

ast, iss, args := p.Parse("event.data.id == 'ok'")
ast, iss, args := p.Parse(`event.data.id == "ok\" please"`)
require.Nil(t, iss)
require.EqualValues(t, map[string]any{"a": "ok"}, args.Map())
require.NotNil(t, ast)
require.NotNil(t, args)
require.EqualValues(t, map[string]any{"a": `ok\" please`}, args.Map())

program, err := env.Program(
ast,
Expand All @@ -1100,7 +1102,7 @@ func TestParsedCELAST(t *testing.T) {
result, _, err := program.Eval(map[string]any{
"event": map[string]any{
"data": map[string]any{
"id": "ok",
"id": `ok\" please`,
},
},
"vars": args.Map(),
Expand Down

0 comments on commit 921f6fc

Please sign in to comment.