...

Source file src/github.com/jackc/pgx/v5/pgproto3/backend_key_data.go

Documentation: github.com/jackc/pgx/v5/pgproto3

     1  package pgproto3
     2  
     3  import (
     4  	"encoding/binary"
     5  	"encoding/json"
     6  
     7  	"github.com/jackc/pgx/v5/internal/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, error) {
    33  	dst, sp := beginMessage(dst, 'K')
    34  	dst = pgio.AppendUint32(dst, src.ProcessID)
    35  	dst = pgio.AppendUint32(dst, src.SecretKey)
    36  	return finishMessage(dst, sp)
    37  }
    38  
    39  // MarshalJSON implements encoding/json.Marshaler.
    40  func (src BackendKeyData) MarshalJSON() ([]byte, error) {
    41  	return json.Marshal(struct {
    42  		Type      string
    43  		ProcessID uint32
    44  		SecretKey uint32
    45  	}{
    46  		Type:      "BackendKeyData",
    47  		ProcessID: src.ProcessID,
    48  		SecretKey: src.SecretKey,
    49  	})
    50  }
    51  

View as plain text