...
1 package pgproto3
2
3 import (
4 "encoding/json"
5 "errors"
6 )
7
8 type ReadyForQuery struct {
9 TxStatus byte
10 }
11
12
13 func (*ReadyForQuery) Backend() {}
14
15
16
17 func (dst *ReadyForQuery) Decode(src []byte) error {
18 if len(src) != 1 {
19 return &invalidMessageLenErr{messageType: "ReadyForQuery", expectedLen: 1, actualLen: len(src)}
20 }
21
22 dst.TxStatus = src[0]
23
24 return nil
25 }
26
27
28 func (src *ReadyForQuery) Encode(dst []byte) ([]byte, error) {
29 return append(dst, 'Z', 0, 0, 0, 5, src.TxStatus), nil
30 }
31
32
33 func (src ReadyForQuery) MarshalJSON() ([]byte, error) {
34 return json.Marshal(struct {
35 Type string
36 TxStatus string
37 }{
38 Type: "ReadyForQuery",
39 TxStatus: string(src.TxStatus),
40 })
41 }
42
43
44 func (dst *ReadyForQuery) UnmarshalJSON(data []byte) error {
45
46 if string(data) == "null" {
47 return nil
48 }
49
50 var msg struct {
51 TxStatus string
52 }
53 if err := json.Unmarshal(data, &msg); err != nil {
54 return err
55 }
56 if len(msg.TxStatus) != 1 {
57 return errors.New("invalid length for ReadyForQuery.TxStatus")
58 }
59 dst.TxStatus = msg.TxStatus[0]
60 return nil
61 }
62
View as plain text