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

fix: Modify PEG rule to support escape chracter #774

Merged
merged 7 commits into from
Jun 14, 2024
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
6 changes: 4 additions & 2 deletions libs/pubsub/query/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
gen_query_parser:
go get -u -v github.com/pointlander/peg
gen_query_parser: peg
peg -inline -switch query.peg

peg:
@go install github.com/pointlander/peg@v1.0.1

fuzzy_test:
go get -u -v github.com/dvyukov/go-fuzz/go-fuzz
go get -u -v github.com/dvyukov/go-fuzz/go-fuzz-build
Expand Down
6 changes: 6 additions & 0 deletions libs/pubsub/query/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func TestParser(t *testing.T) {

{"hash='136E18F7E4C348B780CF873A0BF43922E5BAFA63'", true},
{"hash=136E18F7E4C348B780CF873A0BF43922E5BAFA63", false},

{"root.dummy.v1.EventDummy.seq='\"1\"'", true},
{"root.dummy.v1.EventDummy.descr='\\\"slash included\\\"'", true},
{"root.dummy.v1.EventDummy.descr='\"single quote \\' included\"'", true},
{"root.dummy.v1.EventDummy.descr='\"single quote ' included\"'", false},
{"root.dummy.v1.EventDummy.descr='{\"object\":\"style\"}'", true},
}

for _, c := range cases {
Expand Down
11 changes: 8 additions & 3 deletions libs/pubsub/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ type Condition struct {
// invalid.
func New(s string) (*Query, error) {
p := &QueryParser{Buffer: fmt.Sprintf(`"%s"`, s)}
p.Init()
err := p.Init()
if err != nil {
return nil, err
}

if err := p.Parse(); err != nil {
return nil, err
}

return &Query{str: s, parser: p}, nil
}

Expand Down Expand Up @@ -101,7 +106,7 @@ func (q *Query) Conditions() ([]Condition, error) {
buffer, begin, end := q.parser.Buffer, 0, 0

// tokens must be in the following order: tag ("tx.gas") -> operator ("=") -> operand ("7")
for token := range q.parser.Tokens() {
for _, token := range q.parser.Tokens() {
switch token.pegRule {
case rulePegText:
begin, end = int(token.begin), int(token.end)
Expand Down Expand Up @@ -213,7 +218,7 @@ func (q *Query) Matches(events map[string][]string) (bool, error) {
// tokens must be in the following order:

// tag ("tx.gas") -> operator ("=") -> operand ("7")
for token := range q.parser.Tokens() {
for _, token := range q.parser.Tokens() {
switch token.pegRule {
case rulePegText:
begin, end = int(token.begin), int(token.end)
Expand Down
2 changes: 1 addition & 1 deletion libs/pubsub/query/query.peg
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ condition <- tag ' '* (le ' '* (number / time / date)
)

tag <- < (![ \t\n\r\\()"'=><] .)+ >
value <- < '\'' (!["'] .)* '\''>
value <- < '\'' (('\\' .) / (!['] .))* '\''>
ulbqb marked this conversation as resolved.
Show resolved Hide resolved
number <- < ('0'
/ [1-9] digit* ('.' digit*)?) >
digit <- [0-9]
Expand Down
Loading
Loading