forked from Meituan-Dianping/cat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
110 lines (89 loc) · 1.64 KB
/
message.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
package message
import (
"bytes"
"time"
)
const (
CatSuccess = "0"
CatError = "-1"
)
type Flush func(m Messager)
//noinspection GoNameStartsWithPackageName
type MessageGetter interface {
GetType() string
GetName() string
GetStatus() string
GetData() *bytes.Buffer
GetTime() time.Time
}
type Messager interface {
MessageGetter
AddData(k string, v ...string)
SetData(v string)
SetStatus(status string)
SetTime(time time.Time)
Complete()
}
type Message struct {
Type string
Name string
Status string
timestamp time.Time
data *bytes.Buffer
flush Flush
}
func NewMessage(mtype, name string, flush Flush) Message {
return Message{
Type: mtype,
Name: name,
Status: CatSuccess,
timestamp: time.Now(),
data: new(bytes.Buffer),
flush: flush,
}
}
func (m *Message) Complete() {
if m.flush != nil {
m.flush(m)
}
}
func (m *Message) GetType() string {
return m.Type
}
func (m *Message) GetName() string {
return m.Name
}
func (m *Message) GetStatus() string {
return m.Status
}
func (m *Message) GetData() *bytes.Buffer {
return m.data
}
func (m *Message) GetTime() time.Time {
return m.timestamp
}
func (m *Message) SetTime(t time.Time) {
m.timestamp = t
}
func (m *Message) AddData(k string, v ...string) {
if m.data.Len() != 0 {
m.data.WriteRune('&')
}
if len(v) == 0 {
m.data.WriteString(k)
} else {
m.data.WriteString(k)
m.data.WriteRune('=')
m.data.WriteString(v[0])
}
}
func (m *Message) SetData(v string) {
m.data.Reset()
m.data.WriteString(v)
}
func (m *Message) SetStatus(status string) {
m.Status = status
}
func (m *Message) SetSuccessStatus() {
m.Status = CatSuccess
}