/
types.go
49 lines (40 loc) · 1.28 KB
/
types.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
package jsonrpc
import (
"encoding/json"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
const version string = "2.0"
var (
unmarshalOpts = protojson.UnmarshalOptions{}
marshalOpts = protojson.MarshalOptions{
UseProtoNames: false,
}
)
// inboundMsg is a JSON-RPC message decoded from a reader. It supports both
// requests and responses.
type inboundMsg struct {
Version string `json:"jsonrpc,"`
ID interface{} `json:"id,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
Method *string `json:"method,omitempty"`
}
type protoPayload struct {
payload proto.Message
}
func (pr protoPayload) MarshalJSON() ([]byte, error) {
return marshalOpts.Marshal(pr.payload)
}
// outboundMsg is a message that is going to be written on a writer. It supports
// both requests and responses.
type outboundMsg struct {
Version string `json:"jsonrpc,"`
ID interface{} `json:"id,omitempty"`
Params *protoPayload `json:"params,omitempty"`
Result *protoPayload `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
Method *string `json:"method,omitempty"`
sentChan chan struct{}
}