Skip to content

Commit

Permalink
initialize関数判定切り出し
Browse files Browse the repository at this point in the history
  • Loading branch information
mazrean committed Nov 3, 2023
1 parent 523928a commit 4c23cc2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
31 changes: 6 additions & 25 deletions analysers/cache/analyser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"go/format"
"go/token"
"reflect"
"strings"
"unicode"

"github.com/mazrean/isucon-go-tools/pkg/analyze"
"github.com/mazrean/isucon-go-tools/pkg/suggest"
"golang.org/x/tools/go/analysis"
)
Expand Down Expand Up @@ -37,7 +36,6 @@ func run(pass *analysis.Pass) (any, error) {
initializeFuncDecl *ast.FuncDecl
initializeFuncName string
)
FILE_LOOP:
for _, f := range pass.Files {
for _, decl := range f.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
Expand All @@ -50,14 +48,11 @@ FILE_LOOP:
continue
}

words := camelCaseSplit(funcName.Name)
for _, word := range words {
if strings.ToLower(word) == initializeKeyword {
initializeFuncFile = f
initializeFuncDecl = funcDecl
initializeFuncName = funcName.Name
break FILE_LOOP
}
if analyze.IsInitializeFuncName(funcName.Name) {
initializeFuncFile = f
initializeFuncDecl = funcDecl
initializeFuncName = funcName.Name
break
}
}
}
Expand Down Expand Up @@ -159,17 +154,3 @@ FILE_LOOP:

return importPkgs, nil
}

func camelCaseSplit(s string) []string {
var result []string
start := 0
for i, r := range s {
if unicode.IsUpper(r) {
result = append(result, s[start:i])
start = i
}
}
result = append(result, s[start:])

return result
}
35 changes: 35 additions & 0 deletions pkg/analyze/initialize_func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package analyze

import (
"strings"
"unicode"
)

const (
initializeKeyword = "initialize"
)

func IsInitializeFuncName(name string) bool {
words := camelCaseSplit(name)
for _, word := range words {
if strings.ToLower(word) == initializeKeyword {
return true
}
}

return false
}

func camelCaseSplit(s string) []string {
var result []string
start := 0
for i, r := range s {
if unicode.IsUpper(r) {
result = append(result, s[start:i])
start = i
}
}
result = append(result, s[start:])

return result
}

0 comments on commit 4c23cc2

Please sign in to comment.