...

Source file src/github.com/jackc/pgproto3/v2/copy_both_response.go

Documentation: github.com/jackc/pgproto3/v2

     1  package pgproto3
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"encoding/json"
     7  	"errors"
     8  	"math"
     9  
    10  	"github.com/jackc/pgio"
    11  )
    12  
    13  type CopyBothResponse struct {
    14  	OverallFormat     byte
    15  	ColumnFormatCodes []uint16
    16  }
    17  
    18  // Backend identifies this message as sendable by the PostgreSQL backend.
    19  func (*CopyBothResponse) Backend() {}
    20  
    21  // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
    22  // type identifier and 4 byte message length.
    23  func (dst *CopyBothResponse) Decode(src []byte) error {
    24  	buf := bytes.NewBuffer(src)
    25  
    26  	if buf.Len() < 3 {
    27  		return &invalidMessageFormatErr{messageType: "CopyBothResponse"}
    28  	}
    29  
    30  	overallFormat := buf.Next(1)[0]
    31  
    32  	columnCount := int(binary.BigEndian.Uint16(buf.Next(2)))
    33  	if buf.Len() != columnCount*2 {
    34  		return &invalidMessageFormatErr{messageType: "CopyBothResponse"}
    35  	}
    36  
    37  	columnFormatCodes := make([]uint16, columnCount)
    38  	for i := 0; i < columnCount; i++ {
    39  		columnFormatCodes[i] = binary.BigEndian.Uint16(buf.Next(2))
    40  	}
    41  
    42  	*dst = CopyBothResponse{OverallFormat: overallFormat, ColumnFormatCodes: columnFormatCodes}
    43  
    44  	return nil
    45  }
    46  
    47  // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
    48  func (src *CopyBothResponse) Encode(dst []byte) ([]byte, error) {
    49  	dst, sp := beginMessage(dst, 'W')
    50  	dst = append(dst, src.OverallFormat)
    51  	if len(src.ColumnFormatCodes) > math.MaxUint16 {
    52  		return nil, errors.New("too many column format codes")
    53  	}
    54  	dst = pgio.AppendUint16(dst, uint16(len(src.ColumnFormatCodes)))
    55  	for _, fc := range src.ColumnFormatCodes {
    56  		dst = pgio.AppendUint16(dst, fc)
    57  	}
    58  
    59  	return finishMessage(dst, sp)
    60  }
    61  
    62  // MarshalJSON implements encoding/json.Marshaler.
    63  func (src CopyBothResponse) MarshalJSON() ([]byte, error) {
    64  	return json.Marshal(struct {
    65  		Type              string
    66  		ColumnFormatCodes []uint16
    67  	}{
    68  		Type:              "CopyBothResponse",
    69  		ColumnFormatCodes: src.ColumnFormatCodes,
    70  	})
    71  }
    72  
    73  // UnmarshalJSON implements encoding/json.Unmarshaler.
    74  func (dst *CopyBothResponse) UnmarshalJSON(data []byte) error {
    75  	// Ignore null, like in the main JSON package.
    76  	if string(data) == "null" {
    77  		return nil
    78  	}
    79  
    80  	var msg struct {
    81  		OverallFormat     string
    82  		ColumnFormatCodes []uint16
    83  	}
    84  	if err := json.Unmarshal(data, &msg); err != nil {
    85  		return err
    86  	}
    87  
    88  	if len(msg.OverallFormat) != 1 {
    89  		return errors.New("invalid length for CopyBothResponse.OverallFormat")
    90  	}
    91  
    92  	dst.OverallFormat = msg.OverallFormat[0]
    93  	dst.ColumnFormatCodes = msg.ColumnFormatCodes
    94  	return nil
    95  }
    96  

View as plain text