package pgproto3

import (
	
	
	

	
)

type BackendKeyData struct {
	ProcessID uint32
	SecretKey []byte
}

// Backend identifies this message as sendable by the PostgreSQL backend.
func (*BackendKeyData) () {}

// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
// type identifier and 4 byte message length.
func ( *BackendKeyData) ( []byte) error {
	if len() < 8 {
		return &invalidMessageLenErr{messageType: "BackendKeyData", expectedLen: 8, actualLen: len()}
	}

	.ProcessID = binary.BigEndian.Uint32([:4])
	.SecretKey = make([]byte, len()-4)
	copy(.SecretKey, [4:])

	return nil
}

// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
func ( *BackendKeyData) ( []byte) ([]byte, error) {
	,  := beginMessage(, 'K')
	 = pgio.AppendUint32(, .ProcessID)
	 = append(, .SecretKey...)
	return finishMessage(, )
}

// MarshalJSON implements encoding/json.Marshaler.
func ( BackendKeyData) () ([]byte, error) {
	return json.Marshal(struct {
		      string
		 uint32
		 string
	}{
		:      "BackendKeyData",
		: .ProcessID,
		: hex.EncodeToString(.SecretKey),
	})
}

// UnmarshalJSON implements encoding/json.Unmarshaler.
func ( *BackendKeyData) ( []byte) error {
	var  struct {
		 uint32
		 string
	}
	if  := json.Unmarshal(, &);  != nil {
		return 
	}

	.ProcessID = .
	,  := hex.DecodeString(.)
	if  != nil {
		return 
	}
	.SecretKey = 
	return nil
}