-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreflection-helpers.go-
More file actions
137 lines (106 loc) · 3.21 KB
/
reflection-helpers.go-
File metadata and controls
137 lines (106 loc) · 3.21 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package helpers
import (
"fmt"
"path"
"reflect"
"runtime"
"strings"
)
var strToReplace = "github.com/"
var strToReplaceWith = ""
var mainPackagePath string
// InitMainPackagePath - to be called in `main()`
func InitMainPackagePath() {
_, filename, _, ok := runtime.Caller(1)
if ok {
mainPackagePath = path.Dir(filename) + "/"
fmt.Println(mainPackagePath)
mainPackagePath = strings.Replace(mainPackagePath, strToReplace, strToReplaceWith, 1)
}
}
// GetPathForThisPackage -
func GetPathForThisPackage() string {
_, filename, _, ok := runtime.Caller(1)
if ok {
return path.Dir(filename) + "/"
}
return "./"
}
// GetStructName - returns struct name
func GetStructName(i interface{}) string {
var t = reflect.TypeOf(i)
var result string
if t.Kind() == reflect.Ptr {
result = t.Elem().Name()
} else {
result = t.Name()
}
return strings.Replace(result, strToReplace, strToReplaceWith, 1)
}
// GetPackageName - returns package name
func GetPackageName(i interface{}) string {
var t = reflect.TypeOf(i)
var result string
if t.Kind() == reflect.Ptr {
result = t.Elem().PkgPath()
} else {
result = t.PkgPath()
}
return strings.Replace(result, strToReplace, strToReplaceWith, 1)
}
// GetPackageNameWithStruct - returns package name with struct
func GetPackageNameWithStruct(i interface{}) string {
var t = reflect.TypeOf(i)
var result string
if t.Kind() == reflect.Ptr {
result = t.Elem().PkgPath() + "/" + t.Elem().Name()
} else {
result = t.PkgPath() + "/" + t.Name()
}
return strings.Replace(result, strToReplace, strToReplaceWith, 1)
}
// GetCallingFuncName - returns package + function name at runtime
func GetCallingFuncName() string {
pc, _, _, _ := runtime.Caller(1)
var functionName = runtime.FuncForPC(pc).Name()
functionName = strings.Replace(functionName, strToReplace, strToReplaceWith, 1)
functionName = strings.Replace(functionName, "(", "", 1)
functionName = strings.Replace(functionName, ")", "", 1)
functionName = strings.Replace(functionName, "*", "", 1)
return functionName + "()"
}
// GetCallStackWithFileAndLineNumber - traces a call with line number
func GetCallStackWithFileAndLineNumber() string {
pc := make([]uintptr, 15)
n := runtime.Callers(2, pc)
frames := runtime.CallersFrames(pc[:n])
var logLine = ""
frame, more := frames.Next()
for more {
frame.File = strings.Replace(frame.File, mainPackagePath, "", 1)
// logLine += fmt.Sprintf("%s,:%d %s", frame.File, frame.Line, frame.Function)
logLine += fmt.Sprintf("%s | ", frame.Function)
frame, more = frames.Next()
}
return logLine
}
// CallStack - returns function name at runtime
func CallStack() string {
pc, _, _, _ := runtime.Caller(1)
var functionName = runtime.FuncForPC(pc).Name()
return strings.Replace(functionName, "_"+mainPackagePath, "", 1)
}
// PackageName - returns function name at runtime
func PackageName(i interface{}) string {
var packagePath = reflect.TypeOf(i).PkgPath()
runes := []rune(packagePath)
return string(runes[1:len(packagePath)])
}
// Trace - traces a call with line number
func Trace() {
pc := make([]uintptr, 15)
n := runtime.Callers(2, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
fmt.Printf("%s,:%d %s\n", frame.File, frame.Line, frame.Function)
}