...
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 ParameterDescription struct {
14 ParameterOIDs []uint32
15 }
16
17
18 func (*ParameterDescription) Backend() {}
19
20
21
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
30 buf.Next(2)
31
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
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
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 }
68
View as plain text