backend_key_data.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package pgproto3
  2. import (
  3. "encoding/binary"
  4. "encoding/json"
  5. "github.com/jackc/pgx/v5/internal/pgio"
  6. )
  7. type BackendKeyData struct {
  8. ProcessID uint32
  9. SecretKey uint32
  10. }
  11. // Backend identifies this message as sendable by the PostgreSQL backend.
  12. func (*BackendKeyData) Backend() {}
  13. // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
  14. // type identifier and 4 byte message length.
  15. func (dst *BackendKeyData) Decode(src []byte) error {
  16. if len(src) != 8 {
  17. return &invalidMessageLenErr{messageType: "BackendKeyData", expectedLen: 8, actualLen: len(src)}
  18. }
  19. dst.ProcessID = binary.BigEndian.Uint32(src[:4])
  20. dst.SecretKey = binary.BigEndian.Uint32(src[4:])
  21. return nil
  22. }
  23. // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
  24. func (src *BackendKeyData) Encode(dst []byte) ([]byte, error) {
  25. dst, sp := beginMessage(dst, 'K')
  26. dst = pgio.AppendUint32(dst, src.ProcessID)
  27. dst = pgio.AppendUint32(dst, src.SecretKey)
  28. return finishMessage(dst, sp)
  29. }
  30. // MarshalJSON implements encoding/json.Marshaler.
  31. func (src BackendKeyData) MarshalJSON() ([]byte, error) {
  32. return json.Marshal(struct {
  33. Type string
  34. ProcessID uint32
  35. SecretKey uint32
  36. }{
  37. Type: "BackendKeyData",
  38. ProcessID: src.ProcessID,
  39. SecretKey: src.SecretKey,
  40. })
  41. }