feat: Skip source directory for gen addition
This commit is contained in:
parent
8d45714628
commit
17b47b1184
@ -12,6 +12,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var gormSourceDir string
|
var gormSourceDir string
|
||||||
|
var skipSourceDirs []string
|
||||||
|
var matchSkipSourceDir = false
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
_, file, _, _ := runtime.Caller(0)
|
_, file, _, _ := runtime.Caller(0)
|
||||||
@ -19,15 +21,33 @@ func init() {
|
|||||||
gormSourceDir = regexp.MustCompile(`utils.utils\.go`).ReplaceAllString(file, "")
|
gormSourceDir = regexp.MustCompile(`utils.utils\.go`).ReplaceAllString(file, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddSkipSourceDir(dirs ...string) {
|
||||||
|
skipSourceDirs = append(skipSourceDirs, dirs...)
|
||||||
|
if len(skipSourceDirs) > 0 {
|
||||||
|
matchSkipSourceDir = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// FileWithLineNum return the file name and line number of the current file
|
// FileWithLineNum return the file name and line number of the current file
|
||||||
func FileWithLineNum() string {
|
func FileWithLineNum() string {
|
||||||
// the second caller usually from gorm internal, so set i start from 2
|
// the second caller usually from gorm internal, so set i start from 2
|
||||||
for i := 2; i < 15; i++ {
|
for i := 2; i < 15; i++ {
|
||||||
_, file, line, ok := runtime.Caller(i)
|
_, file, line, ok := runtime.Caller(i)
|
||||||
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
|
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
|
||||||
|
matchSkip := false
|
||||||
|
if matchSkipSourceDir {
|
||||||
|
for _, dir := range skipSourceDirs {
|
||||||
|
if strings.HasPrefix(file, dir) {
|
||||||
|
matchSkip = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !matchSkip {
|
||||||
return file + ":" + strconv.FormatInt(int64(line), 10)
|
return file + ":" + strconv.FormatInt(int64(line), 10)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user