-
Notifications
You must be signed in to change notification settings - Fork 8
/
json.go
156 lines (133 loc) · 2.74 KB
/
json.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
153
154
155
156
package json
import (
"encoding/json"
"sync"
"github.com/cuigh/auxo/data"
"github.com/cuigh/auxo/errors"
"github.com/cuigh/auxo/net/rpc"
)
type request struct {
Head rpc.RequestHead `json:"head"`
Args []json.RawMessage `json:"args"`
}
func (r *request) reset() {
r.Head.ID = 0
r.Head.Service = ""
r.Head.Method = ""
if r.Head.Labels != nil {
r.Head.Labels = r.Head.Labels[:0]
}
if r.Args != nil {
r.Args = r.Args[:0]
}
}
type response struct {
Head rpc.ResponseHead `json:"head"`
Result struct {
Value json.RawMessage `json:"value"`
Error *errors.CodedError `json:"error"`
} `json:"result"`
}
func (r *response) reset() {
r.Head.ID = 0
r.Result.Value = nil
r.Result.Error = nil
}
type ClientCodec struct {
rpc.Stream
enc *json.Encoder
dec *json.Decoder
resp *response
lock sync.Mutex // protect for writing
}
func (c *ClientCodec) Encode(req *rpc.Request) (err error) {
c.lock.Lock()
defer c.lock.Unlock()
err = c.enc.Encode(req)
if err == nil {
err = c.Flush()
}
return
}
func (c *ClientCodec) DecodeHead(head *rpc.ResponseHead) error {
c.resp.reset()
err := c.dec.Decode(c.resp)
if err != nil {
return err
}
*head = c.resp.Head
return nil
}
func (c *ClientCodec) DecodeResult(result *rpc.Result) error {
result.Error = c.resp.Result.Error
if result.Value == nil {
return nil
}
return json.Unmarshal(c.resp.Result.Value, result.Value)
}
func (*ClientCodec) DiscardResult() error {
return nil
}
type ServerCodec struct {
rpc.Stream
enc *json.Encoder
dec *json.Decoder
req *request
lock sync.Mutex // protect for writing
}
func (c *ServerCodec) Encode(resp *rpc.Response) (err error) {
c.lock.Lock()
defer c.lock.Unlock()
err = c.enc.Encode(resp)
if err == nil {
err = c.Flush()
}
return
}
func (c *ServerCodec) DecodeHead(head *rpc.RequestHead) error {
c.req.reset()
err := c.dec.Decode(c.req)
if err != nil {
return err
}
*head = c.req.Head
return nil
}
func (c *ServerCodec) DecodeArgs(args []interface{}) error {
for i, arg := range c.req.Args {
err := json.Unmarshal(arg, args[i])
if err != nil {
return err
}
}
return nil
}
func (*ServerCodec) DiscardArgs() error {
return nil
}
// Matcher is a JSON Matcher.
func Matcher(p rpc.ReadPeeker) bool {
b, err := p.Peek(1)
return err == nil && b[0] == '{'
}
type Builder struct {
}
func (Builder) NewClient(s rpc.Stream, opts data.Map) rpc.ClientCodec {
return &ClientCodec{
Stream: s,
enc: json.NewEncoder(s),
dec: json.NewDecoder(s),
resp: &response{},
}
}
func (Builder) NewServer(s rpc.Stream, opts data.Map) rpc.ServerCodec {
return &ServerCodec{
Stream: s,
enc: json.NewEncoder(s),
dec: json.NewDecoder(s),
req: &request{},
}
}
func init() {
rpc.RegisterCodec("json", Builder{})
}