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