-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnotification.go
More file actions
72 lines (59 loc) · 1.32 KB
/
notification.go
File metadata and controls
72 lines (59 loc) · 1.32 KB
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
package pgproto
import (
"io"
)
type Notification struct {
PID int
Channel []byte
Payload []byte
}
func (n *Notification) server() {}
func ParseNotification(r io.Reader) (*Notification, error) {
buf := newReadBuffer(r)
// 'A' [int32 - length] [int32 - pid] [string - channel] \0 [string - payload] \0
err := buf.ReadTag('A')
if err != nil {
return nil, err
}
buf, err = buf.ReadLength()
if err != nil {
return nil, err
}
pid, err := buf.ReadInt()
if err != nil {
return nil, err
}
channel, err := buf.ReadString(true)
if err != nil {
return nil, err
}
payload, err := buf.ReadString(true)
if err != nil {
return nil, err
}
return &Notification{
PID: pid,
Channel: channel,
Payload: payload,
}, nil
}
func (n *Notification) Encode() []byte {
// 'A' [int32 - length] [int32 - pid] [string - channel] \0 [string - payload] \0
buf := newWriteBuffer()
buf.WriteInt(n.PID)
buf.WriteString(n.Channel, true)
buf.WriteString(n.Payload, true)
buf.Wrap('N')
return buf.Bytes()
}
func (n *Notification) AsMap() map[string]interface{} {
return map[string]interface{}{
"Type": "Notification",
"Payload": map[string]interface{}{
"PID": n.PID,
"Channel": string(n.Channel),
"Payload": string(n.Payload),
},
}
}
func (n *Notification) String() string { return messageToString(n) }