...
1 package pgproto3
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "encoding/json"
7
8 "github.com/jackc/pgio"
9 )
10
11 type NotificationResponse struct {
12 PID uint32
13 Channel string
14 Payload string
15 }
16
17
18 func (*NotificationResponse) Backend() {}
19
20
21
22 func (dst *NotificationResponse) Decode(src []byte) error {
23 buf := bytes.NewBuffer(src)
24
25 pid := binary.BigEndian.Uint32(buf.Next(4))
26
27 b, err := buf.ReadBytes(0)
28 if err != nil {
29 return err
30 }
31 channel := string(b[:len(b)-1])
32
33 b, err = buf.ReadBytes(0)
34 if err != nil {
35 return err
36 }
37 payload := string(b[:len(b)-1])
38
39 *dst = NotificationResponse{PID: pid, Channel: channel, Payload: payload}
40 return nil
41 }
42
43
44 func (src *NotificationResponse) Encode(dst []byte) ([]byte, error) {
45 dst, sp := beginMessage(dst, 'A')
46 dst = pgio.AppendUint32(dst, src.PID)
47 dst = append(dst, src.Channel...)
48 dst = append(dst, 0)
49 dst = append(dst, src.Payload...)
50 dst = append(dst, 0)
51 return finishMessage(dst, sp)
52 }
53
54
55 func (src NotificationResponse) MarshalJSON() ([]byte, error) {
56 return json.Marshal(struct {
57 Type string
58 PID uint32
59 Channel string
60 Payload string
61 }{
62 Type: "NotificationResponse",
63 PID: src.PID,
64 Channel: src.Channel,
65 Payload: src.Payload,
66 })
67 }
68
View as plain text