mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 18:57:44 +08:00
fix(debug/gdebug): incorrect package name handling in function CallerPackage (#3771)
This commit is contained in:
parent
6a99931798
commit
e186eab1a5
@ -133,16 +133,25 @@ func filterFileByFilters(file string, filters []string) (filtered bool) {
|
|||||||
// CallerPackage returns the package name of the caller.
|
// CallerPackage returns the package name of the caller.
|
||||||
func CallerPackage() string {
|
func CallerPackage() string {
|
||||||
function, _, _ := Caller()
|
function, _, _ := Caller()
|
||||||
|
// it defines a new internal function to retrieve the package name from caller function name,
|
||||||
|
// which is for unit testing purpose for core logic of this function.
|
||||||
|
return getPackageFromCallerFunction(function)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPackageFromCallerFunction(function string) string {
|
||||||
indexSplit := strings.LastIndexByte(function, '/')
|
indexSplit := strings.LastIndexByte(function, '/')
|
||||||
if indexSplit == -1 {
|
if indexSplit == -1 {
|
||||||
return function[:strings.IndexByte(function, '.')]
|
return function[:strings.IndexByte(function, '.')]
|
||||||
} else {
|
|
||||||
leftPart := function[:indexSplit+1]
|
|
||||||
rightPart := function[indexSplit+1:]
|
|
||||||
indexDot := strings.IndexByte(function, '.')
|
|
||||||
rightPart = rightPart[:indexDot-1]
|
|
||||||
return leftPart + rightPart
|
|
||||||
}
|
}
|
||||||
|
var (
|
||||||
|
leftPart = function[:indexSplit+1]
|
||||||
|
rightPart = function[indexSplit+1:]
|
||||||
|
indexDot = strings.IndexByte(rightPart, '.')
|
||||||
|
)
|
||||||
|
if indexDot >= 0 {
|
||||||
|
rightPart = rightPart[:indexDot]
|
||||||
|
}
|
||||||
|
return leftPart + rightPart
|
||||||
}
|
}
|
||||||
|
|
||||||
// CallerFunction returns the function name of the caller.
|
// CallerFunction returns the function name of the caller.
|
||||||
|
26
debug/gdebug/gdebug_z_unit_internal_test.go
Normal file
26
debug/gdebug/gdebug_z_unit_internal_test.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the MIT License.
|
||||||
|
// If a copy of the MIT was not distributed with this file,
|
||||||
|
// You can obtain one at https://github.com/gogf/gf.
|
||||||
|
|
||||||
|
package gdebug
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_getPackageFromCallerFunction(t *testing.T) {
|
||||||
|
dataMap := map[string]string{
|
||||||
|
"github.com/gogf/gf/v2/test/a": "github.com/gogf/gf/v2/test/a",
|
||||||
|
"github.com/gogf/gf/v2/test/a.C": "github.com/gogf/gf/v2/test/a",
|
||||||
|
"github.com/gogf/gf/v2/test/aa.C": "github.com/gogf/gf/v2/test/aa",
|
||||||
|
"github.com/gogf/gf/v2/test/gtest.C": "github.com/gogf/gf/v2/test/gtest",
|
||||||
|
}
|
||||||
|
for functionName, packageName := range dataMap {
|
||||||
|
if result := getPackageFromCallerFunction(functionName); result != packageName {
|
||||||
|
t.Logf(`%s != %s`, result, packageName)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,9 @@
|
|||||||
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the MIT License.
|
||||||
|
// If a copy of the MIT was not distributed with this file,
|
||||||
|
// You can obtain one at https://github.com/gogf/gf.
|
||||||
|
|
||||||
package gdebug_test
|
package gdebug_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
Loading…
Reference in New Issue
Block a user