Skip to content

Commit

Permalink
Merge pull request #66 from sashamelentyev/feat/sql-isolation-level
Browse files Browse the repository at this point in the history
feat: add sql.IsolationLevel
  • Loading branch information
sashamelentyev authored Sep 1, 2022
2 parents e82da9e + 0b118d6 commit 034f17d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
30 changes: 22 additions & 8 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (
)

const (
TimeWeekdayFlag = "time-weekday"
TimeMonthFlag = "time-month"
TimeLayoutFlag = "time-layout"
CryptoHashFlag = "crypto-hash"
HTTPMethodFlag = "http-method"
HTTPStatusCodeFlag = "http-status-code"
RPCDefaultPathFlag = "rpc-default-path"
OSDevNullFlag = "os-dev-null"
TimeWeekdayFlag = "time-weekday"
TimeMonthFlag = "time-month"
TimeLayoutFlag = "time-layout"
CryptoHashFlag = "crypto-hash"
HTTPMethodFlag = "http-method"
HTTPStatusCodeFlag = "http-status-code"
RPCDefaultPathFlag = "rpc-default-path"
OSDevNullFlag = "os-dev-null"
SQLIsolationLevelFlag = "sql-isolation-level"
)

// New returns new usestdlibvars analyzer.
Expand All @@ -45,6 +46,7 @@ func flags() flag.FlagSet {
flags.Bool(CryptoHashFlag, false, "suggest the use of crypto.Hash")
flags.Bool(RPCDefaultPathFlag, false, "suggest the use of rpc.DefaultXXPath")
flags.Bool(OSDevNullFlag, false, "suggest the use of os.DevNull")
flags.Bool(SQLIsolationLevelFlag, false, "suggest the use of sql.LevelXX")
return *flags
}

Expand Down Expand Up @@ -99,6 +101,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
checkOSDevNull(pass, n)
}

if lookupFlag(pass, SQLIsolationLevelFlag) {
checkSQLIsolationLevel(pass, n)
}

case *ast.CompositeLit:
typ, ok := n.Type.(*ast.SelectorExpr)
if !ok {
Expand Down Expand Up @@ -403,6 +409,14 @@ func checkOSDevNull(pass *analysis.Pass, basicLit *ast.BasicLit) {
}
}

func checkSQLIsolationLevel(pass *analysis.Pass, basicLit *ast.BasicLit) {
currentVal := getBasicLitValue(basicLit)

if newVal, ok := mapping.SQLIsolationLevel[currentVal]; ok {
report(pass, basicLit.Pos(), currentVal, newVal)
}
}

// getBasicLitFromArgs gets the *ast.BasicLit of a function argument.
//
// Arguments:
Expand Down
4 changes: 4 additions & 0 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestUseStdlibVars(t *testing.T) {
"a/rpc",
"a/time",
"a/os",
"a/sql",
}

a := analyzer.New()
Expand All @@ -37,6 +38,9 @@ func TestUseStdlibVars(t *testing.T) {
if err := a.Flags.Set(analyzer.OSDevNullFlag, "true"); err != nil {
t.Error(err)
}
if err := a.Flags.Set(analyzer.SQLIsolationLevelFlag, "true"); err != nil {
t.Error(err)
}

analysistest.Run(t, analysistest.TestData(), a, pkgs...)
}
6 changes: 6 additions & 0 deletions pkg/analyzer/internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ func main() {
templateName: "test-template.go.tmpl",
fileName: "pkg/analyzer/testdata/src/a/os/devnull.go",
},
{
mapping: mapping.SQLIsolationLevel,
packageName: "sql_test",
templateName: "test-template.go.tmpl",
fileName: "pkg/analyzer/testdata/src/a/sql/isolationlevel.go",
},
}

for _, operation := range operations {
Expand Down
12 changes: 12 additions & 0 deletions pkg/analyzer/internal/mapping/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mapping

import (
"crypto"
"database/sql"
"net/http"
"net/rpc"
"os"
Expand Down Expand Up @@ -164,3 +165,14 @@ var TimeLayout = map[string]string{
var OSDevNull = map[string]string{
os.DevNull: "os.DevNull",
}

var SQLIsolationLevel = map[string]string{
// sql.LevelDefault.String(): "sql.LevelDefault.String()",
sql.LevelReadUncommitted.String(): "sql.LevelReadUncommitted.String()",
sql.LevelReadCommitted.String(): "sql.LevelReadCommitted.String()",
sql.LevelWriteCommitted.String(): "sql.LevelWriteCommitted.String()",
sql.LevelRepeatableRead.String(): "sql.LevelRepeatableRead.String()",
// sql.LevelSnapshot.String(): "sql.LevelSnapshot.String()",
// sql.LevelSerializable.String(): "sql.LevelSerializable.String()",
// sql.LevelLinearizable.String(): "sql.LevelLinearizable.String()",
}
49 changes: 49 additions & 0 deletions pkg/analyzer/testdata/src/a/sql/isolationlevel.go

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

0 comments on commit 034f17d

Please sign in to comment.