forked from starius/api2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_utils.go
54 lines (49 loc) · 1.33 KB
/
gen_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package api2
import (
"path"
"reflect"
"runtime"
"strings"
)
type FnInfo struct {
PkgFull string
PkgName string
StructName string
Method string
}
type FuncInfoer interface {
FuncInfo() (pkgFull, pkgName, structName, method string)
}
func GetFnInfo(i interface{}) FnInfo {
if f, ok := i.(FuncInfoer); ok {
pkgFull, pkgName, structName, method := f.FuncInfo()
return FnInfo{
PkgFull: pkgFull,
PkgName: pkgName,
StructName: structName,
Method: method,
}
}
// Format: pgkpath.(object).Method-fm.
funcName := runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
lastSlash := strings.LastIndexByte(funcName, '/')
if lastSlash < 0 {
lastSlash = 0
}
lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash
baseNameWithService := path.Base(funcName[:lastDot])
lastDotInService := strings.LastIndexByte(baseNameWithService, '.')
lastMinusInName := strings.LastIndexByte(funcName, '-')
pkgName := baseNameWithService[:lastDotInService]
replacer := strings.NewReplacer("(", "", ")", "", "*", "")
serviceName := replacer.Replace(baseNameWithService[lastDotInService+1:])
pkgBase := pkgName
method := funcName[lastDot+1 : lastMinusInName]
pkgFull := funcName[:lastDot]
return FnInfo{
PkgFull: pkgFull,
PkgName: pkgBase,
StructName: serviceName,
Method: method,
}
}