copy_fail.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package pgproto3
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. )
  6. type CopyFail struct {
  7. Message string
  8. }
  9. // Frontend identifies this message as sendable by a PostgreSQL frontend.
  10. func (*CopyFail) Frontend() {}
  11. // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
  12. // type identifier and 4 byte message length.
  13. func (dst *CopyFail) Decode(src []byte) error {
  14. idx := bytes.IndexByte(src, 0)
  15. if idx != len(src)-1 {
  16. return &invalidMessageFormatErr{messageType: "CopyFail"}
  17. }
  18. dst.Message = string(src[:idx])
  19. return nil
  20. }
  21. // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
  22. func (src *CopyFail) Encode(dst []byte) ([]byte, error) {
  23. dst, sp := beginMessage(dst, 'f')
  24. dst = append(dst, src.Message...)
  25. dst = append(dst, 0)
  26. return finishMessage(dst, sp)
  27. }
  28. // MarshalJSON implements encoding/json.Marshaler.
  29. func (src CopyFail) MarshalJSON() ([]byte, error) {
  30. return json.Marshal(struct {
  31. Type string
  32. Message string
  33. }{
  34. Type: "CopyFail",
  35. Message: src.Message,
  36. })
  37. }