-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
74 lines (62 loc) · 1.23 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
package gomsg
import (
"encoding/json"
"errors"
"fmt"
"log"
)
type C interface {
Code() interface{}
Msg() string
Err() error
Print()
LPrint()
}
type Message struct {
Status interface{}
Text string
}
func (m *Message) Code() interface{} {
return m.Status
}
func (m *Message) Msg() string {
return m.Text
}
func (m *Message) Err() error {
mJson, _ := json.Marshal(m)
return errors.New(string(mJson))
}
func (m *Message) Print() {
fmt.Printf("%sstatus%s%s%v%s %stext%s%s%s%s\n",
blueBg, reset, cyanBg, m.Status, reset, blueBg, reset, cyanBg, m.Text, reset)
}
func (m *Message) LPrint() {
log.Printf("%sstatus%s%s%v%s %stext%s%s%s%s\n",
blueBg, reset, cyanBg, m.Status, reset, blueBg, reset, cyanBg, m.Text, reset)
}
func Marshal(status interface{}, msg string) error {
return NewMsg(status, msg).Err()
}
func UnMarshal(msg string) *Message {
m := new(Message)
err := json.Unmarshal([]byte(msg), &m)
if err != nil {
return &Message{
Status: "Unknown",
Text: msg,
}
}
return m
}
func NewMsg(status interface{}, msg string) *Message {
return &Message{
Status: status,
Text: msg,
}
}
func NewMsgByError(status interface{}, err error) *Message {
return &Message{
Status: status,
Text: err.Error(),
}
}