github.com/jackc/pgx/v5@v5.5.5/pgproto3/copy_data.go (about) 1 package pgproto3 2 3 import ( 4 "encoding/hex" 5 "encoding/json" 6 ) 7 8 type CopyData struct { 9 Data []byte 10 } 11 12 // Backend identifies this message as sendable by the PostgreSQL backend. 13 func (*CopyData) Backend() {} 14 15 // Frontend identifies this message as sendable by a PostgreSQL frontend. 16 func (*CopyData) Frontend() {} 17 18 // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message 19 // type identifier and 4 byte message length. 20 func (dst *CopyData) Decode(src []byte) error { 21 dst.Data = src 22 return nil 23 } 24 25 // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. 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 // MarshalJSON implements encoding/json.Marshaler. 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 // UnmarshalJSON implements encoding/json.Unmarshaler. 44 func (dst *CopyData) UnmarshalJSON(data []byte) error { 45 // Ignore null, like in the main JSON package. 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 }