-
Notifications
You must be signed in to change notification settings - Fork 1
/
event_support.go
134 lines (126 loc) · 2.36 KB
/
event_support.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
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
package log
import (
"fmt"
"time"
)
// GetMessageOf returns for the given Event the contained message (if exists).
func GetMessageOf(e Event, using Provider) *string {
if e == nil {
return nil
}
pv, _ := e.Get(using.GetFieldKeysSpec().GetMessage())
if lv, ok := pv.(interface {
Get() interface{}
}); ok {
pv = lv.Get()
}
switch v := pv.(type) {
case nil:
return nil
case string:
return &v
case *string:
return v
case fmt.Stringer:
s := v.String()
return &s
default:
result := fmt.Sprint(pv)
return &result
}
}
// GetErrorOf returns for the given Event the contained error (if exists).
func GetErrorOf(e Event, using Provider) error {
if e == nil {
return nil
}
pv, _ := e.Get(using.GetFieldKeysSpec().GetError())
if lv, ok := pv.(interface {
Get() interface{}
}); ok {
pv = lv.Get()
}
switch v := pv.(type) {
case nil:
return nil
case error:
return v
case string:
return stringError(v)
case *string:
return stringError(*v)
case fmt.Stringer:
return stringError(v.String())
default:
return stringError(fmt.Sprint(pv))
}
}
// GetTimestampOf returns for the given Event the contained timestamp
// (if exists).
func GetTimestampOf(e Event, using Provider) *time.Time {
if e == nil {
return nil
}
pv, _ := e.Get(using.GetFieldKeysSpec().GetTimestamp())
if lv, ok := pv.(interface {
Get() interface{}
}); ok {
pv = lv.Get()
}
switch v := pv.(type) {
case time.Time:
if v.IsZero() {
return nil
}
return &v
case *time.Time:
if v.IsZero() {
return nil
}
return v
default:
return nil
}
}
// GetLoggerOf returns for the given Event the contained logger (name)
// (if exists).
func GetLoggerOf(e Event, using Provider) *string {
if e == nil {
return nil
}
pv, _ := e.Get(using.GetFieldKeysSpec().GetLogger())
if lv, ok := pv.(interface {
Get() interface{}
}); ok {
pv = lv.Get()
}
switch v := pv.(type) {
case nil:
return nil
case string:
return &v
case *string:
return v
case Logger:
result := v.GetName()
return &result
case interface {
GetName() string
}:
result := v.GetName()
return &result
case fmt.Stringer:
result := v.String()
return &result
default:
result := fmt.Sprint(pv)
return &result
}
}
type stringError string
func (instance stringError) Error() string {
return string(instance)
}
func (instance stringError) String() string {
return string(instance)
}