gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

pgproto3.go (1629B)


      1 package pgproto3
      2 
      3 import (
      4 	"encoding/hex"
      5 	"errors"
      6 	"fmt"
      7 )
      8 
      9 // Message is the interface implemented by an object that can decode and encode
     10 // a particular PostgreSQL message.
     11 type Message interface {
     12 	// Decode is allowed and expected to retain a reference to data after
     13 	// returning (unlike encoding.BinaryUnmarshaler).
     14 	Decode(data []byte) error
     15 
     16 	// Encode appends itself to dst and returns the new buffer.
     17 	Encode(dst []byte) []byte
     18 }
     19 
     20 type FrontendMessage interface {
     21 	Message
     22 	Frontend() // no-op method to distinguish frontend from backend methods
     23 }
     24 
     25 type BackendMessage interface {
     26 	Message
     27 	Backend() // no-op method to distinguish frontend from backend methods
     28 }
     29 
     30 type AuthenticationResponseMessage interface {
     31 	BackendMessage
     32 	AuthenticationResponse() // no-op method to distinguish authentication responses
     33 }
     34 
     35 type invalidMessageLenErr struct {
     36 	messageType string
     37 	expectedLen int
     38 	actualLen   int
     39 }
     40 
     41 func (e *invalidMessageLenErr) Error() string {
     42 	return fmt.Sprintf("%s body must have length of %d, but it is %d", e.messageType, e.expectedLen, e.actualLen)
     43 }
     44 
     45 type invalidMessageFormatErr struct {
     46 	messageType string
     47 }
     48 
     49 func (e *invalidMessageFormatErr) Error() string {
     50 	return fmt.Sprintf("%s body is invalid", e.messageType)
     51 }
     52 
     53 // getValueFromJSON gets the value from a protocol message representation in JSON.
     54 func getValueFromJSON(v map[string]string) ([]byte, error) {
     55 	if v == nil {
     56 		return nil, nil
     57 	}
     58 	if text, ok := v["text"]; ok {
     59 		return []byte(text), nil
     60 	}
     61 	if binary, ok := v["binary"]; ok {
     62 		return hex.DecodeString(binary)
     63 	}
     64 	return nil, errors.New("unknown protocol representation")
     65 }