Skip to content

Commit

Permalink
Migration to singlechecker
Browse files Browse the repository at this point in the history
  • Loading branch information
Toru-Takagi committed Jul 5, 2023
1 parent b3be07d commit 80a1807
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 115 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -4,4 +4,10 @@ Format SQL strings in go files.

# install command

`go install github.com/Toru-Takagi/sql_formatter_go`
`go install github.com/Toru-Takagi/sql_formatter_go@latest`


# exec

1. `$ cd [your go project root]`
2. `$ sql_formatter_go ./...`
87 changes: 87 additions & 0 deletions analyzer/format_sql_analyzer.go
@@ -0,0 +1,87 @@
package analyzer

import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/token"
"io/ioutil"
"strings"

"github.com/Toru-Takagi/sql_formatter_go/formatter"
"golang.org/x/tools/go/analysis"
)

var FormatSQLAnalyzer = &analysis.Analyzer{
Name: "format_sql",
Doc: "Only target those of sql string",
Run: formatSQLRun,
}

func formatSQLRun(pass *analysis.Pass) (interface{}, error) {
for _, astFile := range pass.Files {
var formatErr error
isFormatted := false

fname := pass.Fset.Position(astFile.Package).Filename

if strings.HasSuffix(fname, "_gen.go") {
// Skip generated files
return nil, nil
}

ast.Inspect(astFile, func(n ast.Node) bool {
if n == nil {
return false
}

switch x := n.(type) {
case *ast.GenDecl:
if x.Tok == token.CONST {
for _, spec := range x.Specs {
vspec := spec.(*ast.ValueSpec)
for _, v := range vspec.Values {
if basicList, ok := v.(*ast.BasicLit); ok {
trimSQL := strings.TrimSpace(strings.NewReplacer([]string{
"`", "",
`"`, "",
}...).Replace(basicList.Value))
upperSQL := strings.ToUpper(trimSQL)
if strings.HasPrefix(upperSQL, "SELECT") || strings.HasPrefix(upperSQL, "INSERT") || strings.HasPrefix(upperSQL, "UPDATE") || strings.HasPrefix(upperSQL, "DELETE") {
result, err := formatter.Format(trimSQL, nil)
if err != nil {
formatErr = err
return false
}
basicList.Value = fmt.Sprintf("`%s`", result)
isFormatted = true
}
}
}
}
}
}
return true
})

if formatErr != nil {
return nil, formatErr
}

if isFormatted {
var buf bytes.Buffer
if err := format.Node(&buf, pass.Fset, astFile); err != nil {
return nil, err
}

// Overwrite the original file with the new code
filename := pass.Fset.File(astFile.Pos()).Name()
if err := ioutil.WriteFile(filename, buf.Bytes(), 0644); err != nil {
return nil, err
}
}

}
return nil, nil
}
3 changes: 3 additions & 0 deletions go.mod
Expand Up @@ -12,6 +12,9 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/tools v0.10.0 // indirect
google.golang.org/protobuf v1.23.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Expand Up @@ -18,6 +18,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.10.0 h1:tvDr/iQoUqNdohiYm0LmmKcBk+q86lb9EprIUFhHHGg=
golang.org/x/tools v0.10.0/go.mod h1:UJwyiVBsOA2uwvK/e5OY3GTpDUJriEd+/YlqAwLPmyM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand Down
9 changes: 6 additions & 3 deletions main.go
@@ -1,5 +1,8 @@
package main

func main() {
sql_formatter_go_main()
}
import (
"github.com/Toru-Takagi/sql_formatter_go/analyzer"
"golang.org/x/tools/go/analysis/singlechecker"
)

func main() { singlechecker.Main(analyzer.FormatSQLAnalyzer) }
111 changes: 0 additions & 111 deletions sql_formatter_go.go

This file was deleted.

0 comments on commit 80a1807

Please sign in to comment.