forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathuuid.go
57 lines (44 loc) · 1.31 KB
/
uuid.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
package uuid
import (
"fmt"
"reflect"
"github.com/google/uuid"
"gopkg.in/vmihailenco/msgpack.v2"
)
// UUID external type
// Supported since Tarantool 2.4.1. See more in commit messages.
// https://github.com/tarantool/tarantool/commit/d68fc29246714eee505bc9bbcd84a02de17972c5
const UUID_extId = 2
func encodeUUID(e *msgpack.Encoder, v reflect.Value) error {
id := v.Interface().(uuid.UUID)
bytes, err := id.MarshalBinary()
if err != nil {
return fmt.Errorf("msgpack: can't marshal binary uuid: %w", err)
}
_, err = e.Writer().Write(bytes)
if err != nil {
return fmt.Errorf("msgpack: can't write bytes to encoder writer: %w", err)
}
return nil
}
func decodeUUID(d *msgpack.Decoder, v reflect.Value) error {
var bytesCount int = 16
bytes := make([]byte, bytesCount)
n, err := d.Buffered().Read(bytes)
if err != nil {
return fmt.Errorf("msgpack: can't read bytes on uuid decode: %w", err)
}
if n < bytesCount {
return fmt.Errorf("msgpack: unexpected end of stream after %d uuid bytes", n)
}
id, err := uuid.FromBytes(bytes)
if err != nil {
return fmt.Errorf("msgpack: can't create uuid from bytes: %w", err)
}
v.Set(reflect.ValueOf(id))
return nil
}
func init() {
msgpack.Register(reflect.TypeOf((*uuid.UUID)(nil)).Elem(), encodeUUID, decodeUUID)
msgpack.RegisterExt(UUID_extId, (*uuid.UUID)(nil))
}