-
Notifications
You must be signed in to change notification settings - Fork 0
/
constructor.go
52 lines (45 loc) · 1.01 KB
/
constructor.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
package re
import (
"runtime"
"strings"
)
func New(op Op, internal error, args ...interface{}) *Bag {
pc, fileName, lineNumber, _ := runtime.Caller(1)
funcPt := runtime.FuncForPC(pc)
functionName := "Unknown"
if funcPt != nil {
parts := strings.Split(funcPt.Name(), "/")
secs := strings.SplitN(parts[len(parts)-1], ".", 2)
functionName = secs[len(secs)-1]
}
e := &Bag{
ops: []Op{op},
internal: internal,
codeInfo: CodeInfo{
FileName: fileName,
FunctionName: functionName,
LineNumber: lineNumber,
},
}
for _, arg := range args {
switch typedArg := arg.(type) {
case string:
e.message = typedArg
case Kind:
e.kind = typedArg
case Meta:
if currentMeta, has := e.metaData[op.String()].(map[string]interface{}); has {
for k, v := range typedArg {
currentMeta[k] = v
}
e.metaData[op.String()] = currentMeta
} else {
e.metaData = map[string]interface{}{op.String(): typedArg}
}
}
}
if e.kind == "" {
e.kind = KindUnexpected
}
return e
}