-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetadata.go
152 lines (126 loc) · 2.84 KB
/
metadata.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
package rrpc
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
)
const Version = uint16(1)
type Metadata []byte
func (p Metadata) String() string {
var tr string
if b := p.Tracing(); len(b) < 1 {
tr = "<nil>"
} else {
tr = "0x" + hex.EncodeToString(b)
}
var m string
if b := p.Metadata(); len(b) < 1 {
m = "<nil>"
} else {
m = "0x" + hex.EncodeToString(b)
}
return fmt.Sprintf(
"Metadata{version=%d, service=%s, method=%s, tracing=%s, metadata=%s}",
p.Version(),
p.Service(),
p.Method(),
tr,
m,
)
}
func (p Metadata) Version() uint16 {
return binary.BigEndian.Uint16(p.raw())
}
func (p Metadata) Service() string {
offset := 2
raw := p.raw()
serviceLen := int(binary.BigEndian.Uint16(raw[offset : offset+2]))
offset += 2
return string(raw[offset : offset+serviceLen])
}
func (p Metadata) Method() string {
offset := 2
raw := p.raw()
serviceLen := int(binary.BigEndian.Uint16(raw[offset : offset+2]))
offset += 2 + serviceLen
methodLen := int(binary.BigEndian.Uint16(raw[offset : offset+2]))
offset += 2
return string(raw[offset : offset+methodLen])
}
func (p Metadata) Tracing() []byte {
offset := 2
raw := p.raw()
serviceLen := int(binary.BigEndian.Uint16(raw[offset : offset+2]))
offset += 2 + serviceLen
methodLen := int(binary.BigEndian.Uint16(raw[offset : offset+2]))
offset += 2 + methodLen
tracingLen := int(binary.BigEndian.Uint16(raw[offset : offset+2]))
offset += 2
if tracingLen > 0 {
return raw[offset : offset+tracingLen]
} else {
return nil
}
}
func (p Metadata) Metadata() []byte {
offset := 2
raw := p.raw()
serviceLen := int(binary.BigEndian.Uint16(raw[offset : offset+2]))
offset += 2 + serviceLen
methodLen := int(binary.BigEndian.Uint16(raw[offset : offset+2]))
offset += 2 + methodLen
tracingLen := int(binary.BigEndian.Uint16(raw[offset : offset+2]))
offset += 2 + tracingLen
return raw[offset:]
}
func (p Metadata) raw() []byte {
return ([]byte)(p)
}
func EncodeMetadata(service string, method string, tracing []byte, metadata []byte) (m Metadata, err error) {
w := &bytes.Buffer{}
// write version
err = binary.Write(w, binary.BigEndian, Version)
if err != nil {
return
}
// write service
err = binary.Write(w, binary.BigEndian, uint16(len(service)))
if err != nil {
return
}
_, err = w.WriteString(service)
if err != nil {
return
}
// write method
err = binary.Write(w, binary.BigEndian, uint16(len(method)))
if err != nil {
return
}
_, err = w.WriteString(method)
if err != nil {
return
}
// write tracing
lenTracing := uint16(len(tracing))
err = binary.Write(w, binary.BigEndian, lenTracing)
if err != nil {
return
}
if lenTracing > 0 {
_, err = w.Write(tracing)
if err != nil {
return
}
}
// write metadata
if l := len(metadata); l > 0 {
_, err = w.Write(metadata)
if err != nil {
return
}
}
m = w.Bytes()
return
}