From 17b47b11841b590173a3a01d9df608611eb75e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=86=8A=E9=97=AF?= Date: Wed, 15 Jun 2022 21:53:51 +0800 Subject: [PATCH] feat: Skip source directory for gen addition --- utils/utils.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index 296917b9..4cc4e36c 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -12,6 +12,8 @@ import ( ) var gormSourceDir string +var skipSourceDirs []string +var matchSkipSourceDir = false func init() { _, file, _, _ := runtime.Caller(0) @@ -19,13 +21,31 @@ func init() { 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 func FileWithLineNum() string { // the second caller usually from gorm internal, so set i start from 2 for i := 2; i < 15; i++ { _, file, line, ok := runtime.Caller(i) if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) { - return file + ":" + strconv.FormatInt(int64(line), 10) + 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) + } } }