package pgproto3
import (
)
const maxMessageBodyLen = (0x3fffffff - 1)
type Message interface {
Decode(data []byte) error
Encode(dst []byte) ([]byte, error)
}
type FrontendMessage interface {
Message
Frontend()
}
type BackendMessage interface {
Message
Backend()
}
type AuthenticationResponseMessage interface {
BackendMessage
AuthenticationResponse()
}
type invalidMessageLenErr struct {
messageType string
expectedLen int
actualLen int
}
func ( *invalidMessageLenErr) () string {
return fmt.Sprintf("%s body must have length of %d, but it is %d", .messageType, .expectedLen, .actualLen)
}
type invalidMessageFormatErr struct {
messageType string
details string
}
func ( *invalidMessageFormatErr) () string {
return fmt.Sprintf("%s body is invalid %s", .messageType, .details)
}
type writeError struct {
err error
safeToRetry bool
}
func ( *writeError) () string {
return fmt.Sprintf("write failed: %s", .err.Error())
}
func ( *writeError) () bool {
return .safeToRetry
}
func ( *writeError) () error {
return .err
}
type ExceededMaxBodyLenErr struct {
MaxExpectedBodyLen int
ActualBodyLen int
}
func ( *ExceededMaxBodyLenErr) () string {
return fmt.Sprintf("invalid body length: expected at most %d, but got %d", .MaxExpectedBodyLen, .ActualBodyLen)
}
func ( map[string]string) ([]byte, error) {
if == nil {
return nil, nil
}
if , := ["text"]; {
return []byte(), nil
}
if , := ["binary"]; {
return hex.DecodeString()
}
return nil, errors.New("unknown protocol representation")
}
func ( []byte, byte) ([]byte, int) {
= append(, )
:= len()
= pgio.AppendInt32(, -1)
return ,
}
func ( []byte, int) ([]byte, error) {
:= len([:])
if > maxMessageBodyLen {
return nil, errors.New("message body too large")
}
pgio.SetInt32([:], int32())
return , nil
}