forked from leandro-lugaresi/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
40 lines (32 loc) · 766 Bytes
/
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
package hub
import (
"fmt"
"sort"
"strings"
)
type (
// Fields is a [key]value storage for Messages values.
Fields map[string]interface{}
// Message represent some message/event passed into the hub
// It also contain some helper functions to convert the fields to primitive types.
Message struct {
Name string
Body []byte
Fields Fields
}
)
// Topic return the message topic used when the message was sended.
func (m *Message) Topic() string {
return m.Name
}
func (f Fields) String() string {
if len(f) == 0 {
return "Fields(<empty>)"
}
fields := make([]string, 0, len(f))
for k, v := range f {
fields = append(fields, fmt.Sprintf("[%s]%v", k, v))
}
sort.Strings(fields)
return "Fields( " + strings.Join(fields, ", ") + " )"
}