backend_key_data.go (1355B)
1 package pgproto3 2 3 import ( 4 "encoding/binary" 5 "encoding/json" 6 7 "github.com/jackc/pgio" 8 ) 9 10 type BackendKeyData struct { 11 ProcessID uint32 12 SecretKey uint32 13 } 14 15 // Backend identifies this message as sendable by the PostgreSQL backend. 16 func (*BackendKeyData) Backend() {} 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 *BackendKeyData) Decode(src []byte) error { 21 if len(src) != 8 { 22 return &invalidMessageLenErr{messageType: "BackendKeyData", expectedLen: 8, actualLen: len(src)} 23 } 24 25 dst.ProcessID = binary.BigEndian.Uint32(src[:4]) 26 dst.SecretKey = binary.BigEndian.Uint32(src[4:]) 27 28 return nil 29 } 30 31 // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. 32 func (src *BackendKeyData) Encode(dst []byte) []byte { 33 dst = append(dst, 'K') 34 dst = pgio.AppendUint32(dst, 12) 35 dst = pgio.AppendUint32(dst, src.ProcessID) 36 dst = pgio.AppendUint32(dst, src.SecretKey) 37 return dst 38 } 39 40 // MarshalJSON implements encoding/json.Marshaler. 41 func (src BackendKeyData) MarshalJSON() ([]byte, error) { 42 return json.Marshal(struct { 43 Type string 44 ProcessID uint32 45 SecretKey uint32 46 }{ 47 Type: "BackendKeyData", 48 ProcessID: src.ProcessID, 49 SecretKey: src.SecretKey, 50 }) 51 }