github.com/jackc/pgx/v5@v5.5.5/pgproto3/parameter_description.go (about) 1 package pgproto3 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "encoding/json" 7 "errors" 8 "math" 9 10 "github.com/jackc/pgx/v5/internal/pgio" 11 ) 12 13 type ParameterDescription struct { 14 ParameterOIDs []uint32 15 } 16 17 // Backend identifies this message as sendable by the PostgreSQL backend. 18 func (*ParameterDescription) Backend() {} 19 20 // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message 21 // type identifier and 4 byte message length. 22 func (dst *ParameterDescription) Decode(src []byte) error { 23 buf := bytes.NewBuffer(src) 24 25 if buf.Len() < 2 { 26 return &invalidMessageFormatErr{messageType: "ParameterDescription"} 27 } 28 29 // Reported parameter count will be incorrect when number of args is greater than uint16 30 buf.Next(2) 31 // Instead infer parameter count by remaining size of message 32 parameterCount := buf.Len() / 4 33 34 *dst = ParameterDescription{ParameterOIDs: make([]uint32, parameterCount)} 35 36 for i := 0; i < parameterCount; i++ { 37 dst.ParameterOIDs[i] = binary.BigEndian.Uint32(buf.Next(4)) 38 } 39 40 return nil 41 } 42 43 // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. 44 func (src *ParameterDescription) Encode(dst []byte) ([]byte, error) { 45 dst, sp := beginMessage(dst, 't') 46 47 if len(src.ParameterOIDs) > math.MaxUint16 { 48 return nil, errors.New("too many parameter oids") 49 } 50 dst = pgio.AppendUint16(dst, uint16(len(src.ParameterOIDs))) 51 for _, oid := range src.ParameterOIDs { 52 dst = pgio.AppendUint32(dst, oid) 53 } 54 55 return finishMessage(dst, sp) 56 } 57 58 // MarshalJSON implements encoding/json.Marshaler. 59 func (src ParameterDescription) MarshalJSON() ([]byte, error) { 60 return json.Marshal(struct { 61 Type string 62 ParameterOIDs []uint32 63 }{ 64 Type: "ParameterDescription", 65 ParameterOIDs: src.ParameterOIDs, 66 }) 67 }