/
reader.go
269 lines (217 loc) · 6.44 KB
/
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright (c) 2023 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package binary
import (
"fmt"
"io"
"go.uber.org/thriftrw/wire"
)
// offsetReader provides a type that satisfies an io.Reader with only an
// io.ReaderAt.
type offsetReader struct {
offset int64
reader io.ReaderAt
}
var (
_ io.Reader = (*offsetReader)(nil)
_ io.Seeker = (*offsetReader)(nil)
)
// Read reads len(p) bytes into p.
func (or *offsetReader) Read(p []byte) (int, error) {
n, err := or.reader.ReadAt(p, or.offset)
or.offset += int64(n)
return n, err
}
func (or *offsetReader) Seek(offset int64, whence int) (int64, error) {
switch whence {
case io.SeekStart:
or.offset = offset
case io.SeekCurrent:
or.offset += offset
default:
return or.offset, fmt.Errorf("unsupported whence %d", whence)
}
return or.offset, nil
}
// reader functions as the actual reader behind the exported `Reader` type.
// This is necessary to avoid new calls to a `Reader.ReadValue` from changing
// the offset in already running 'ReadValue' calls.
type reader struct {
or *offsetReader
sr *StreamReader
}
func newReader(r io.ReaderAt, off int64) reader {
or := offsetReader{reader: r, offset: off}
return reader{
or: &or,
sr: NewStreamReader(&or),
}
}
func (r *reader) readStructStream() (wire.Struct, error) {
var fields []wire.Field
if err := r.sr.ReadStructBegin(); err != nil {
return wire.Struct{}, err
}
fh, ok, err := r.sr.ReadFieldBegin()
if err != nil {
return wire.Struct{}, err
}
for ok {
val, _, err := r.ReadValue(fh.Type, r.or.offset)
if err != nil {
return wire.Struct{}, err
}
fields = append(fields, wire.Field{ID: fh.ID, Value: val})
if err := r.sr.ReadFieldEnd(); err != nil {
return wire.Struct{}, err
}
if fh, ok, err = r.sr.ReadFieldBegin(); err != nil {
return wire.Struct{}, err
}
}
if err := r.sr.ReadStructEnd(); err != nil {
return wire.Struct{}, err
}
return wire.Struct{Fields: fields}, nil
}
func (r *reader) readMapStream() (wire.MapItemList, error) {
mh, err := r.sr.ReadMapBegin()
if err != nil {
return nil, err
}
start := r.or.offset
if err := r.sr.skipMapItems(mh.KeyType, mh.ValueType, int32(mh.Length)); err != nil {
return nil, err
}
if err := r.sr.ReadMapEnd(); err != nil {
return nil, err
}
items := borrowLazyMapItemList()
items.ktype = mh.KeyType
items.vtype = mh.ValueType
items.count = int32(mh.Length)
items.readerAt = r.or.reader
items.startOffset = start
return items, nil
}
func (r *reader) readListStream() (wire.ValueList, error) {
lh, err := r.sr.ReadListBegin()
if err != nil {
return nil, err
}
start := r.or.offset
if err := r.sr.skipListItems(lh.Type, lh.Length); err != nil {
return nil, err
}
if err := r.sr.ReadListEnd(); err != nil {
return nil, err
}
items := borrowLazyValueList()
items.count = int32(lh.Length)
items.typ = lh.Type
items.readerAt = r.or.reader
items.startOffset = start
return items, nil
}
func (r *reader) readSetStream() (wire.ValueList, error) {
sh, err := r.sr.ReadSetBegin()
if err != nil {
return nil, err
}
start := r.or.offset
if err := r.sr.skipListItems(sh.Type, sh.Length); err != nil {
return nil, err
}
if err := r.sr.ReadSetEnd(); err != nil {
return nil, err
}
items := borrowLazyValueList()
items.count = int32(sh.Length)
items.typ = sh.Type
items.readerAt = r.or.reader
items.startOffset = start
return items, nil
}
func (r *reader) close() error {
err := r.sr.Close()
r.sr = nil
r.or = nil
return err
}
// ReadValue is the underlying call made from the exported `Reader.ReadValue`
// that's meant to be safe for concurrent calls.
func (r *reader) ReadValue(t wire.Type, off int64) (wire.Value, int64, error) {
r.or.offset = off
switch t {
case wire.TBool:
b, err := r.sr.ReadBool()
return wire.NewValueBool(b), r.or.offset, err
case wire.TI8:
b, err := r.sr.ReadInt8()
return wire.NewValueI8(int8(b)), r.or.offset, err
case wire.TDouble:
value, err := r.sr.ReadDouble()
return wire.NewValueDouble(value), r.or.offset, err
case wire.TI16:
n, err := r.sr.ReadInt16()
return wire.NewValueI16(n), r.or.offset, err
case wire.TI32:
n, err := r.sr.ReadInt32()
return wire.NewValueI32(n), r.or.offset, err
case wire.TI64:
n, err := r.sr.ReadInt64()
return wire.NewValueI64(n), r.or.offset, err
case wire.TBinary:
v, err := r.sr.ReadBinary()
return wire.NewValueBinary(v), r.or.offset, err
case wire.TStruct:
s, err := r.readStructStream()
return wire.NewValueStruct(s), r.or.offset, err
case wire.TMap:
m, err := r.readMapStream()
return wire.NewValueMap(m), r.or.offset, err
case wire.TSet:
s, err := r.readSetStream()
return wire.NewValueSet(s), r.or.offset, err
case wire.TList:
l, err := r.readListStream()
return wire.NewValueList(l), r.or.offset, err
default:
return wire.Value{}, r.or.offset, decodeErrorf("unknown ttype %v", t)
}
}
// Reader implements a parser for the Thrift Binary Protocol based on an
// io.ReaderAt.
type Reader struct {
reader io.ReaderAt
}
// NewReader builds a new Reader based on the given io.ReaderAt.
func NewReader(r io.ReaderAt) Reader {
return Reader{reader: r}
}
// ReadValue reads a value off the given type off the wire starting at the
// given offset.
//
// Returns the Value, the new offset, and an error if there was a decode error.
func (br *Reader) ReadValue(t wire.Type, off int64) (wire.Value, int64, error) {
reader := newReader(br.reader, off)
defer reader.close()
return reader.ReadValue(t, off)
}