зеркало из https://github.com/github/vitess-gh.git
Merge pull request #5770 from systay/rewriter
Visitor generator cleanup
This commit is contained in:
Коммит
6a71f2418a
4
Makefile
4
Makefile
|
@ -87,9 +87,7 @@ parser:
|
|||
make -C go/vt/sqlparser
|
||||
|
||||
visitor:
|
||||
go build -o visitorgen go/visitorgen/main/main.go
|
||||
./visitorgen -input=go/vt/sqlparser/ast.go -output=$(REWRITER)
|
||||
rm ./visitorgen
|
||||
go generate go/vt/sqlparser/rewriter.go
|
||||
|
||||
# To pass extra flags, run test.go manually.
|
||||
# For example: go run test.go -docker=false -- --extra-flag
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
package sqlparser
|
||||
|
||||
//go:generate make visitor
|
||||
//go:generate go run visitorgen/main -input=ast.go -output=rewriter.go
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
|
|
@ -17,44 +17,46 @@ limitations under the License.
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"vitess.io/vitess/go/visitorgen"
|
||||
"vitess.io/vitess/go/exit"
|
||||
"vitess.io/vitess/go/vt/log"
|
||||
|
||||
"vitess.io/vitess/go/vt/sqlparser/visitorgen"
|
||||
)
|
||||
|
||||
var (
|
||||
inputFile = flag.String("input", "", "input file to use")
|
||||
outputFile = flag.String("output", "", "output file")
|
||||
compare = flag.Bool("compareOnly", false, "instead of writing to the output file, compare if the generated visitor is still valid for this ast.go")
|
||||
)
|
||||
|
||||
const usage = `Usage of visitorgen:
|
||||
|
||||
go run go/visitorgen/main/main.go -input=/path/to/ast.go -output=/path/to/rewriter.go
|
||||
go run /path/to/visitorgen/main -input=/path/to/ast.go -output=/path/to/rewriter.go
|
||||
`
|
||||
|
||||
func main() {
|
||||
flag.Usage = func() {
|
||||
os.Stderr.WriteString(usage)
|
||||
os.Stderr.WriteString("\nOptions:\n")
|
||||
flag.PrintDefaults()
|
||||
|
||||
}
|
||||
defer exit.Recover()
|
||||
flag.Usage = printUsage
|
||||
flag.Parse()
|
||||
|
||||
if *inputFile == "" || *outputFile == "" {
|
||||
fmt.Println("> " + *inputFile)
|
||||
fmt.Println("> " + *outputFile)
|
||||
panic("need input and output file")
|
||||
printUsage()
|
||||
exit.Return(1)
|
||||
}
|
||||
|
||||
fs := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fs, *inputFile, nil, parser.DeclarationErrors)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Error(err)
|
||||
exit.Return(1)
|
||||
}
|
||||
|
||||
astWalkResult := visitorgen.Walk(file)
|
||||
|
@ -64,49 +66,45 @@ func main() {
|
|||
replacementMethods := visitorgen.EmitReplacementMethods(vd)
|
||||
typeSwitch := visitorgen.EmitTypeSwitches(vd)
|
||||
|
||||
fw := newFileWriter(*outputFile)
|
||||
defer fw.Close()
|
||||
b := &bytes.Buffer{}
|
||||
fmt.Fprint(b, fileHeader)
|
||||
fmt.Fprintln(b)
|
||||
fmt.Fprintln(b, replacementMethods)
|
||||
fmt.Fprint(b, applyHeader)
|
||||
fmt.Fprintln(b, typeSwitch)
|
||||
fmt.Fprintln(b, fileFooter)
|
||||
|
||||
fw.writeln(fileHeader)
|
||||
fw.writeln(replacementMethods)
|
||||
fw.write(applyHeader)
|
||||
fw.writeln(typeSwitch)
|
||||
fw.writeln(fileFooter)
|
||||
}
|
||||
|
||||
type fileWriter struct {
|
||||
file *os.File
|
||||
}
|
||||
|
||||
func newFileWriter(file string) *fileWriter {
|
||||
f, err := os.Create(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
if *compare {
|
||||
currentFile, err := ioutil.ReadFile(*outputFile)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
exit.Return(1)
|
||||
}
|
||||
if !bytes.Equal(b.Bytes(), currentFile) {
|
||||
fmt.Println("rewriter needs to be re-generated: go generate " + *outputFile)
|
||||
exit.Return(1)
|
||||
}
|
||||
} else {
|
||||
err = ioutil.WriteFile(*outputFile, b.Bytes(), 0644)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
exit.Return(1)
|
||||
}
|
||||
}
|
||||
return &fileWriter{file: f}
|
||||
|
||||
}
|
||||
|
||||
func (fw *fileWriter) writeln(s string) {
|
||||
fw.write(s)
|
||||
fw.write("\n")
|
||||
}
|
||||
|
||||
func (fw *fileWriter) write(s string) {
|
||||
_, err := fw.file.WriteString(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (fw *fileWriter) Close() {
|
||||
fw.file.Close()
|
||||
func printUsage() {
|
||||
os.Stderr.WriteString(usage)
|
||||
os.Stderr.WriteString("\nOptions:\n")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
const fileHeader = `// Code generated by visitorgen/main/main.go. DO NOT EDIT.
|
||||
|
||||
package sqlparser
|
||||
|
||||
//go:generate make visitor
|
||||
//go:generate go run visitorgen/main -input=ast.go -output=rewriter.go
|
||||
|
||||
import (
|
||||
"reflect"
|
|
@ -15,11 +15,4 @@
|
|||
|
||||
# this script, which should run before committing code, makes sure that the visitor is re-generated when the ast changes
|
||||
|
||||
make visitor REWRITER=tmp_rewriter.go
|
||||
if ! cmp -s "tmp_rewriter.go" "go/vt/sqlparser/rewriter.go"; then
|
||||
echo "The ast.go has changed, but not rewriter.go"
|
||||
echo "You should 'make visitor' to update the generated rewriter"
|
||||
rm -f tmp_rewriter.go
|
||||
exit 1
|
||||
fi
|
||||
rm -f tmp_rewriter.go
|
||||
go run ./go/vt/sqlparser/visitorgen/main -compareOnly=true -input=go/vt/sqlparser/ast.go -output=go/vt/sqlparser/rewriter.go
|
|
@ -29,6 +29,17 @@
|
|||
"RetryMax": 1,
|
||||
"Tags": []
|
||||
},
|
||||
"check_make_visitor": {
|
||||
"File": "",
|
||||
"Args": [],
|
||||
"Command": [
|
||||
"misc/git/hooks/visitorgen"
|
||||
],
|
||||
"Manual": false,
|
||||
"Shard": 5,
|
||||
"RetryMax": 1,
|
||||
"Tags": []
|
||||
},
|
||||
"java": {
|
||||
"File": "",
|
||||
"Args": [],
|
||||
|
|
Загрузка…
Ссылка в новой задаче