-
Notifications
You must be signed in to change notification settings - Fork 6
/
call.go
121 lines (99 loc) · 2.57 KB
/
call.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
package protocols
import (
"bytes"
"fmt"
"github.com/linkedin/goavro/v2"
)
type CallProtocol interface {
PrepareRequest(method string, datum interface{}) ([]byte, error)
ParseResponse(method string, responseBytes []byte) (interface{}, error)
}
// The Avro Call format implementation for the Avro RPC protocol.
//
// It is used for preparing an Avro RPC request and parsing an Avro RPC response.
//
// See http:https://avro.apache.org/docs/1.8.2/spec.html#Call+Format for details.
type сallProtocol struct {
proto MessageProtocol
metaCodec *goavro.Codec
stringCodec *goavro.Codec
booleanCodec *goavro.Codec
}
func NewCall(proto MessageProtocol) (CallProtocol, error) {
p := &сallProtocol{
proto: proto,
}
err := p.init()
if err != nil {
return nil, err
}
return p, nil
}
func (p *сallProtocol) init() (err error) {
p.metaCodec, err = goavro.NewCodec(`{"type": "map", "values": "bytes"}`)
if err != nil {
return
}
p.stringCodec, err = goavro.NewCodec(`"string"`)
if err != nil {
return
}
p.booleanCodec, err = goavro.NewCodec(`"boolean"`)
if err != nil {
return
}
return
}
func (p *сallProtocol) PrepareRequest(method string, datum interface{}) ([]byte, error) {
meta := make(map[string][]byte)
metaBytes, err := p.metaCodec.BinaryFromNative(nil, meta)
if err != nil {
return nil, err
}
methodBytes, err := p.stringCodec.BinaryFromNative(nil, method)
if err != nil {
return nil, err
}
paramsBytes, err := p.proto.PrepareMessage(method, datum)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(metaBytes)
buf.Write(methodBytes)
buf.Write(paramsBytes)
return buf.Bytes(), nil
}
func (p *сallProtocol) ParseResponse(method string, responseBytes []byte) (interface{}, error) {
meta, responseBytes, err := p.metaCodec.NativeFromBinary(responseBytes)
if err != nil {
return nil, err
}
_ = meta
flag, responseBytes, err := p.booleanCodec.NativeFromBinary(responseBytes)
if err != nil {
return nil, err
}
flagBool, ok := flag.(bool)
if !ok {
return nil, fmt.Errorf("cannot convert error flag to boolean: %v", flag)
}
if flagBool {
responseBytes, err = p.proto.ParseError(method, responseBytes)
if err != nil {
return nil, err
}
return nil, p.checkResponseBytes(responseBytes)
}
message, responseBytes, err := p.proto.ParseMessage(method, responseBytes)
if err != nil {
return nil, err
}
return message, p.checkResponseBytes(responseBytes)
}
func (p *сallProtocol) checkResponseBytes(b []byte) error {
n := len(b)
if n > 0 {
return fmt.Errorf("response buffer is not empty: len=%d, rest=0x%X", n, b)
}
return nil
}