-
Notifications
You must be signed in to change notification settings - Fork 14
/
message.go
94 lines (77 loc) · 1.63 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
package gosf
// Message - Standard message type for Socket communications
type Message struct {
ID int `json:"id,omitempty"`
GUID string `json:"guid,omitempty"`
UUID string `json:"uuid,omitempty"`
Success bool `json:"success"`
Text string `json:"text,omitempty"`
Meta map[string]interface{} `json:"meta,omitempty"`
Body map[string]interface{} `json:"body,omitempty"`
}
// WithoutMeta removes meta information before returning a copy of the message
func (m *Message) WithoutMeta() *Message {
if m.Meta == nil {
return m
}
var metaFreeMessage = new(Message)
(*metaFreeMessage) = *m
metaFreeMessage.Meta = nil
return metaFreeMessage
}
// NewSuccessMessage generates a success message
func NewSuccessMessage(args ...interface{}) *Message {
m := new(Message)
m.Success = true
var text = ""
var body = map[string]interface{}(nil)
for i, p := range args {
switch i {
case 0: // text
param, ok := p.(string)
if !ok {
break
}
text = param
case 1: // body
param, ok := p.(map[string]interface{})
if !ok {
break
}
body = param
}
}
if text != "" {
m.Text = text
}
m.Body = body
return m
}
// NewFailureMessage generates a failure message
func NewFailureMessage(args ...interface{}) *Message {
m := new(Message)
m.Success = false
var text = ""
var body = map[string]interface{}(nil)
for i, p := range args {
switch i {
case 0: // text
param, ok := p.(string)
if !ok {
break
}
text = param
case 1: // body
param, ok := p.(map[string]interface{})
if !ok {
break
}
body = param
}
}
if text != "" {
m.Text = text
}
m.Body = body
return m
}