This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_reader.go
156 lines (124 loc) · 2.76 KB
/
value_reader.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package pgx
import (
"errors"
)
// ValueReader is used by the Scanner interface to decode values.
type ValueReader struct {
mr *msgReader
fd *FieldDescription
valueBytesRemaining int32
err error
}
// Err returns any error that the ValueReader has experienced
func (r *ValueReader) Err() error {
return r.err
}
// Fatal tells r that a Fatal error has occurred
func (r *ValueReader) Fatal(err error) {
r.err = err
}
// Len returns the number of unread bytes
func (r *ValueReader) Len() int32 {
return r.valueBytesRemaining
}
// Type returns the *FieldDescription of the value
func (r *ValueReader) Type() *FieldDescription {
return r.fd
}
func (r *ValueReader) ReadByte() byte {
if r.err != nil {
return 0
}
r.valueBytesRemaining--
if r.valueBytesRemaining < 0 {
r.Fatal(errors.New("read past end of value"))
return 0
}
return r.mr.readByte()
}
func (r *ValueReader) ReadInt16() int16 {
if r.err != nil {
return 0
}
r.valueBytesRemaining -= 2
if r.valueBytesRemaining < 0 {
r.Fatal(errors.New("read past end of value"))
return 0
}
return r.mr.readInt16()
}
func (r *ValueReader) ReadUint16() uint16 {
if r.err != nil {
return 0
}
r.valueBytesRemaining -= 2
if r.valueBytesRemaining < 0 {
r.Fatal(errors.New("read past end of value"))
return 0
}
return r.mr.readUint16()
}
func (r *ValueReader) ReadInt32() int32 {
if r.err != nil {
return 0
}
r.valueBytesRemaining -= 4
if r.valueBytesRemaining < 0 {
r.Fatal(errors.New("read past end of value"))
return 0
}
return r.mr.readInt32()
}
func (r *ValueReader) ReadUint32() uint32 {
if r.err != nil {
return 0
}
r.valueBytesRemaining -= 4
if r.valueBytesRemaining < 0 {
r.Fatal(errors.New("read past end of value"))
return 0
}
return r.mr.readUint32()
}
func (r *ValueReader) ReadInt64() int64 {
if r.err != nil {
return 0
}
r.valueBytesRemaining -= 8
if r.valueBytesRemaining < 0 {
r.Fatal(errors.New("read past end of value"))
return 0
}
return r.mr.readInt64()
}
func (r *ValueReader) ReadOid() Oid {
return Oid(r.ReadUint32())
}
// ReadString reads count bytes and returns as string
func (r *ValueReader) ReadString(count int32) string {
if r.err != nil {
return ""
}
r.valueBytesRemaining -= count
if r.valueBytesRemaining < 0 {
r.Fatal(errors.New("read past end of value"))
return ""
}
return r.mr.readString(count)
}
// ReadBytes reads count bytes and returns as []byte
func (r *ValueReader) ReadBytes(count int32) []byte {
if r.err != nil {
return nil
}
if count < 0 {
r.Fatal(errors.New("count must not be negative"))
return nil
}
r.valueBytesRemaining -= count
if r.valueBytesRemaining < 0 {
r.Fatal(errors.New("read past end of value"))
return nil
}
return r.mr.readBytes(count)
}