parameter_status.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package pgproto3
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. )
  6. type ParameterStatus struct {
  7. Name string
  8. Value string
  9. }
  10. // Backend identifies this message as sendable by the PostgreSQL backend.
  11. func (*ParameterStatus) Backend() {}
  12. // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
  13. // type identifier and 4 byte message length.
  14. func (dst *ParameterStatus) Decode(src []byte) error {
  15. buf := bytes.NewBuffer(src)
  16. b, err := buf.ReadBytes(0)
  17. if err != nil {
  18. return err
  19. }
  20. name := string(b[:len(b)-1])
  21. b, err = buf.ReadBytes(0)
  22. if err != nil {
  23. return err
  24. }
  25. value := string(b[:len(b)-1])
  26. *dst = ParameterStatus{Name: name, Value: value}
  27. return nil
  28. }
  29. // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
  30. func (src *ParameterStatus) Encode(dst []byte) ([]byte, error) {
  31. dst, sp := beginMessage(dst, 'S')
  32. dst = append(dst, src.Name...)
  33. dst = append(dst, 0)
  34. dst = append(dst, src.Value...)
  35. dst = append(dst, 0)
  36. return finishMessage(dst, sp)
  37. }
  38. // MarshalJSON implements encoding/json.Marshaler.
  39. func (ps ParameterStatus) MarshalJSON() ([]byte, error) {
  40. return json.Marshal(struct {
  41. Type string
  42. Name string
  43. Value string
  44. }{
  45. Type: "ParameterStatus",
  46. Name: ps.Name,
  47. Value: ps.Value,
  48. })
  49. }