-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
62 lines (53 loc) · 1.65 KB
/
core.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
55
56
57
58
59
60
61
62
package util
import (
"bytes"
"encoding/gob"
"path/filepath"
log "github.com/sirupsen/logrus"
)
// CallerTree returns data that precomputes paths for convenience
type CallerTree struct {
DotPath string
DotDotPath string
DotDotDotPath string
DotDotDotDotPath string
}
// CallerPaths takes as input the results from calling runtime.Caller(0) and
// returns a data structure of current caller's path and a set of parent paths.
// In a Golang-standard project directory for this project, the following
// would be true:
//
// paths := CallerPaths(pc, file, line, ok)
// strings.HasSuffix(paths.DotPath, "/github.com/bluest-eel/common/util")
// strings.HasSuffix(paths.DotDotPath, "/github.com/bluest-eel/common")
// strings.HasSuffix(paths.DotDotDotPath, "/github.com/bluest-eel")
// strings.HasSuffix(paths.DotDotDotDotPath, "/github.com")
func CallerPaths(pc uintptr, file string, line int, ok bool) *CallerTree {
if !ok {
log.Warn("Couldn't find caller")
return &CallerTree{}
}
callerPath := filepath.Dir(file)
dotCallerPath := filepath.Dir(callerPath)
dotDotCallerPath := filepath.Dir(dotCallerPath)
dotDotDotCallerPath := filepath.Dir(dotDotCallerPath)
return &CallerTree{
DotPath: callerPath,
DotDotPath: dotCallerPath,
DotDotDotPath: dotDotCallerPath,
DotDotDotDotPath: dotDotDotCallerPath,
}
}
// GOBEncode ...
func GOBEncode(v interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(v)
return buf.Bytes(), err
}
// GOBDecode ...
func GOBDecode(b []byte, result interface{}) error {
buf := bytes.NewBuffer(b)
enc := gob.NewDecoder(buf)
return enc.Decode(result)
}