Skip to content

Commit

Permalink
Merge pull request vitessio#10068 from vmg/sqlparser-format-options
Browse files Browse the repository at this point in the history
sqlparser: introduce custom formatting options
  • Loading branch information
vmg authored Apr 12, 2022
2 parents a90d0dc + 5eb60d9 commit 9c082bd
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 200 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ issues:
linters:
- errcheck
# This code is autogenerated and should be permanently excluded.
- path: '^go/vt/sqlparser/(ast_format|ast_format_fast).go'
linters:
- errcheck
- path: '^go/vt/sqlparser/goyacc'
linters:
- errcheck
Expand Down
32 changes: 21 additions & 11 deletions go/tools/astfmtgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,27 @@ func (r *Rewriter) replaceAstfmtCalls(cursor *astutil.Cursor) bool {
}
case *ast.ExprStmt:
if call, ok := v.X.(*ast.CallExpr); ok {
if r.isPrintfCall(call) {
switch r.methodName(call) {
case "astPrintf":
return r.rewriteAstPrintf(cursor, call)
case "literal":
callexpr := call.Fun.(*ast.SelectorExpr)
callexpr.Sel.Name = "WriteString"
return true
}
}
}
return true
}

func (r *Rewriter) isPrintfCall(n *ast.CallExpr) bool {
s, ok := n.Fun.(*ast.SelectorExpr)
if !ok {
return false
}
id := s.Sel
if id != nil && !r.pkg.TypesInfo.Types[id].IsType() {
return id.Name == "astPrintf"
func (r *Rewriter) methodName(n *ast.CallExpr) string {
if call, ok := n.Fun.(*ast.SelectorExpr); ok {
id := call.Sel
if id != nil && !r.pkg.TypesInfo.Types[id].IsType() {
return id.Name
}
}
return false
return ""
}

func (r *Rewriter) rewriteLiteral(rcv ast.Expr, method string, arg ast.Expr) ast.Stmt {
Expand All @@ -138,7 +141,10 @@ func (r *Rewriter) rewriteLiteral(rcv ast.Expr, method string, arg ast.Expr) ast
func (r *Rewriter) rewriteAstPrintf(cursor *astutil.Cursor, expr *ast.CallExpr) bool {
callexpr := expr.Fun.(*ast.SelectorExpr)
lit := expr.Args[1].(*ast.BasicLit)
format, _ := strconv.Unquote(lit.Value)
format, err := strconv.Unquote(lit.Value)
if err != nil {
panic("bad literal argument")
}

end := len(format)
fieldnum := 0
Expand Down Expand Up @@ -172,6 +178,10 @@ func (r *Rewriter) rewriteAstPrintf(cursor *astutil.Cursor, expr *ast.CallExpr)
break
}
i++ // '%'
if format[i] == '#' {
i++
}

token := format[i]
switch token {
case 'c':
Expand Down
1 change: 1 addition & 0 deletions go/vt/sqlparser/ast_equals.go

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

Loading

0 comments on commit 9c082bd

Please sign in to comment.