-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdescribe.go
71 lines (57 loc) · 1.26 KB
/
describe.go
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
package pgproto
import (
"fmt"
"io"
)
type Describe struct {
ObjectType ObjectType
Name []byte
}
func (d *Describe) client() {}
func ParseDescribe(r io.Reader) (*Describe, error) {
b := newReadBuffer(r)
// 'D' [int32 - length] [byte - object type] [string - name] \0
err := b.ReadTag('D')
if err != nil {
return nil, err
}
buf, err := b.ReadLength()
if err != nil {
return nil, err
}
d := &Describe{}
t, err := buf.ReadByte()
if err != nil {
return nil, err
}
switch o := ObjectType(t); o {
case ObjectTypePreparedStatement:
case ObjectTypePortal:
d.ObjectType = o
default:
return nil, fmt.Errorf("unknown describe object type %#v", t)
}
d.Name, err = buf.ReadString(true)
if err != nil {
return nil, err
}
return d, nil
}
func (d *Describe) Encode() []byte {
// 'D' [int32 - length] [byte - object type] [string - name] \0
w := newWriteBuffer()
w.WriteByte(byte(d.ObjectType))
w.WriteString(d.Name, true)
w.Wrap('D')
return w.Bytes()
}
func (d *Describe) AsMap() map[string]interface{} {
return map[string]interface{}{
"Type": "Describe",
"Payload": map[string]interface{}{
"ObjectType": byte(d.ObjectType),
"Name": string(d.Name),
},
}
}
func (d *Describe) String() string { return messageToString(d) }