-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.go
91 lines (80 loc) · 1.75 KB
/
hello.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
package nu
import (
"fmt"
"github.com/vmihailenco/msgpack/v5"
)
type hello struct {
Protocol string `msgpack:"protocol"`
Version string `msgpack:"version"`
Features features `msgpack:"features"`
}
type features struct {
LocalSocket bool
}
var _ msgpack.CustomEncoder = (*hello)(nil)
func (h *hello) EncodeMsgpack(enc *msgpack.Encoder) error {
if err := encodeMapStart(enc, "Hello"); err != nil {
return err
}
if err := enc.EncodeMapLen(3); err != nil {
return err
}
if err := enc.EncodeString("protocol"); err != nil {
return err
}
if err := enc.EncodeString(h.Protocol); err != nil {
return err
}
if err := enc.EncodeString("version"); err != nil {
return err
}
if err := enc.EncodeString(h.Version); err != nil {
return err
}
if err := enc.EncodeString("features"); err != nil {
return err
}
if err := h.EncodeMsgpackFeatures(enc); err != nil {
return fmt.Errorf("encoding features: %w", err)
}
return nil
}
func (h *hello) EncodeMsgpackFeatures(enc *msgpack.Encoder) error {
cnt := 0
if h.Features.LocalSocket {
cnt++
}
if err := enc.EncodeArrayLen(cnt); err != nil {
return err
}
if h.Features.LocalSocket {
if err := enc.EncodeMapLen(1); err != nil {
return err
}
if err := enc.EncodeString("name"); err != nil {
return err
}
if err := enc.EncodeString("LocalSocket"); err != nil {
return err
}
}
return nil
}
var _ msgpack.CustomDecoder = (*features)(nil)
func (f *features) DecodeMsgpack(dec *msgpack.Decoder) error {
cnt, err := dec.DecodeArrayLen()
if err != nil {
return err
}
if cnt < 1 {
return nil
}
for idx := 0; idx < cnt; idx++ {
ftre, err := dec.DecodeMap()
if err != nil {
return err
}
f.LocalSocket = f.LocalSocket || ftre["name"] == "LocalSocket"
}
return nil
}