forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy patharrow.go
58 lines (48 loc) · 1.37 KB
/
arrow.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
package arrow
import (
"fmt"
"reflect"
"github.com/vmihailenco/msgpack/v5"
)
// ExtID represents the Arrow MessagePack extension type identifier.
const ExtID = 8
// Arrow struct wraps a raw arrow data buffer.
type Arrow struct {
data []byte
}
// MakeArrow returns a new arrow.Arrow object that contains
// wrapped a raw arrow data buffer.
func MakeArrow(arrow []byte) (Arrow, error) {
return Arrow{arrow}, nil
}
// Raw returns a []byte that contains Arrow raw data.
func (a Arrow) Raw() []byte {
return a.data
}
// EncodeExt encodes an Arrow into a MessagePack extension.
func EncodeExt(_ *msgpack.Encoder, v reflect.Value) ([]byte, error) {
arr, ok := v.Interface().(Arrow)
if !ok {
return []byte{}, fmt.Errorf("encode: not an Arrow type")
}
return arr.data, nil
}
// DecodeExt decodes a MessagePack extension into an Arrow.
func DecodeExt(d *msgpack.Decoder, v reflect.Value, extLen int) error {
arrow := Arrow{
data: make([]byte, extLen),
}
n, err := d.Buffered().Read(arrow.data)
if err != nil {
return fmt.Errorf("decode: can't read bytes on Arrow decode: %w", err)
}
if n < extLen || n != len(arrow.data) {
return fmt.Errorf("decode: unexpected end of stream after %d Arrow bytes", n)
}
v.Set(reflect.ValueOf(arrow))
return nil
}
func init() {
msgpack.RegisterExtEncoder(ExtID, Arrow{}, EncodeExt)
msgpack.RegisterExtDecoder(ExtID, Arrow{}, DecodeExt)
}