libs/pubsub/query: specify peg version in go.mod (#9099)

* libs/pubsub/query: specify peg version in go.mod

The code to generate the pubsub queries was dependent on an unspecified
version of the peg tool. This brings peg into go.mod so it is on a fixed
version. This should also enable dependabot to notify us of future
updates to peg.

The version of query.peg.go generated from the current version of peg
correctly contains the special "Code generated by..." line to indicate
to other tools that the file is automatically generated and should
therefore be excluded from linters, etc.

I removed the make target as there were no git grep results referencing
"gen_query_parser"; directly running "go generate" is a reasonable
expectation in Go projects.

Now that "go run" is module aware, I would typically use "go run" inside
the go:generate directive, but in this case we go build to a gitignore-d
directory in order to work around the nondeterministic output detailed
in pointlander/peg#129.

* libs/pubsub/query: check error from (*QueryParser).Init()

The newly generated peg code returns an error from Init(); the previous
version was niladic.

Co-authored-by: Sam Kleinman <garen@tychoish.com>
This commit is contained in:
Mark Rushakoff
2022-07-27 16:35:15 -04:00
committed by GitHub
parent c4e235243b
commit 4309f54349
8 changed files with 290 additions and 504 deletions

1
go.mod
View File

@@ -47,6 +47,7 @@ require (
github.com/bufbuild/buf v1.4.0
github.com/creachadair/taskgroup v0.3.2
github.com/golangci/golangci-lint v1.47.2
github.com/pointlander/peg v1.0.1 // indirect
github.com/prometheus/common v0.34.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca
github.com/vektra/mockery/v2 v2.14.0

6
go.sum
View File

@@ -921,6 +921,12 @@ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qR
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pointlander/compress v1.1.1-0.20190518213731-ff44bd196cc3 h1:hUmXhbljNFtrH5hzV9kiRoddZ5nfPTq3K0Sb2hYYiqE=
github.com/pointlander/compress v1.1.1-0.20190518213731-ff44bd196cc3/go.mod h1:q5NXNGzqj5uPnVuhGkZfmgHqNUhf15VLi6L9kW0VEc0=
github.com/pointlander/jetset v1.0.1-0.20190518214125-eee7eff80bd4 h1:RHHRCZeaNyBXdYPMjZNH8/XHDBH38TZzw8izrW7dmBE=
github.com/pointlander/jetset v1.0.1-0.20190518214125-eee7eff80bd4/go.mod h1:RdR1j20Aj5pB6+fw6Y9Ur7lMHpegTEjY1vc19hEZL40=
github.com/pointlander/peg v1.0.1 h1:mgA/GQE8TeS9MdkU6Xn6iEzBmQUQCNuWD7rHCK6Mjs0=
github.com/pointlander/peg v1.0.1/go.mod h1:5hsGDQR2oZI4QoWz0/Kdg3VSVEC31iJw/b7WjqCBGRI=
github.com/polyfloyd/go-errorlint v1.0.0 h1:pDrQG0lrh68e602Wfp68BlUTRFoHn8PZYAjLgt2LFsM=
github.com/polyfloyd/go-errorlint v1.0.0/go.mod h1:KZy4xxPJyy88/gldCe5OdW6OQRtNO3EZE7hXzmnebgA=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=

7
libs/pubsub/query/.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
# This is a temporary directory to hold the peg binary,
# to work around https://github.com/pointlander/peg/issues/129.
# Note that once we have a new version of peg fixing #129,
# we may still want to keep this .gitignore to prevent anyone
# from accidentally running "git add ." and including their built
# peg binary in a commit.
.bin/

View File

@@ -1,11 +1,7 @@
gen_query_parser:
go get -u -v github.com/pointlander/peg
peg -inline -switch query.peg
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
go-fuzz-build github.com/tendermint/tendermint/libs/pubsub/query/fuzz_test
go-fuzz -bin=./fuzz_test-fuzz.zip -workdir=./fuzz_test/output
.PHONY: gen_query_parser fuzzy_test
.PHONY: fuzzy_test

View File

@@ -1,3 +1,10 @@
package query
//go:generate peg -inline -switch query.peg
// Normally I would use go run,
// but the "Code generated by" comment for peg includes the full arg0,
// which includes an unpredictable temporary directory,
// resulting in a nondeterminstic generated source file.
// Using go build is the workaround as detailed in https://github.com/pointlander/peg/issues/129.
//go:generate go build -o ./.bin/peg github.com/pointlander/peg
//go:generate ./.bin/peg -inline -switch query.peg

View File

@@ -39,7 +39,9 @@ type Condition struct {
// invalid.
func New(s string) (*Query, error) {
p := &QueryParser{Buffer: fmt.Sprintf(`"%s"`, s)}
p.Init()
if err := p.Init(); err != nil {
return nil, err
}
if err := p.Parse(); err != nil {
return nil, err
}
@@ -101,7 +103,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)
@@ -213,7 +215,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)

File diff suppressed because it is too large Load Diff

View File

@@ -9,5 +9,6 @@ package tools
import (
_ "github.com/bufbuild/buf/cmd/buf"
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
_ "github.com/pointlander/peg"
_ "github.com/vektra/mockery/v2"
)