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