-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinary_parameters.go
More file actions
95 lines (81 loc) · 1.91 KB
/
binary_parameters.go
File metadata and controls
95 lines (81 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package pgproto
import (
"io"
)
// BinaryParameters represents a client message for sending binary parameters to the server
type BinaryParameters struct {
Fields [][]byte
}
func (p *BinaryParameters) client() {}
// ParseBinaryParameters will attempt to read an BinaryParameter message from the io.Reader
func ParseBinaryParameters(r io.Reader) (*BinaryParameters, error) {
b := newReadBuffer(r)
// 'D' [int32 - length] [int16 - field count] ([int32 - length] [string - data])+
err := b.ReadTag('D')
if err != nil {
return nil, err
}
b, err = b.ReadLength()
if err != nil {
return nil, err
}
// Field count - int16
c, err := b.ReadInt16()
if err != nil {
return nil, err
}
p := &BinaryParameters{
Fields: make([][]byte, c),
}
for i := 0; i < c; i++ {
// [int32 - length] [string - data]
l, err := b.ReadInt()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
if l == -1 {
p.Fields[i] = nil
} else {
p.Fields[i] = make([]byte, l)
_, err = b.Read(p.Fields[i])
if err != nil {
return nil, err
}
}
}
return p, nil
}
// Encode will return the byte representation of this message
func (p *BinaryParameters) Encode() []byte {
b := newWriteBuffer()
b.WriteInt16(len(p.Fields))
for _, f := range p.Fields {
b.WriteInt(len(f))
b.WriteBytes(f)
}
b.Wrap('D')
return b.Bytes()
}
// AsMap method returns a common map representation of this message:
//
// map[string]interface{}{
// "Type": "BinaryParameters",
// "Payload": map[string]interface{}{
// "Fields": <BinaryParameters.Fields>,
// },
// }
func (p *BinaryParameters) AsMap() map[string]interface{} {
f := make([]string, len(p.Fields))
for k, v := range p.Fields {
f[k] = string(v)
}
return map[string]interface{}{
"Type": "BinaryParameters",
"Payload": map[string]interface{}{
"Fields": f,
},
}
}
func (p *BinaryParameters) String() string { return messageToString(p) }