-
Notifications
You must be signed in to change notification settings - Fork 582
/
Copy pathjsonrpc.go
134 lines (110 loc) · 2.75 KB
/
jsonrpc.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
package lsproto
import (
"encoding/json"
"errors"
"fmt"
)
type JSONRPCVersion struct{}
const jsonRPCVersion = `"2.0"`
func (JSONRPCVersion) MarshalJSON() ([]byte, error) {
return []byte(jsonRPCVersion), nil
}
var ErrInvalidJSONRPCVersion = errors.New("invalid JSON-RPC version")
func (*JSONRPCVersion) UnmarshalJSON(data []byte) error {
if string(data) != jsonRPCVersion {
return ErrInvalidJSONRPCVersion
}
return nil
}
type ID struct {
str string
int int32
}
func (id *ID) MarshalJSON() ([]byte, error) {
if id.str != "" {
return json.Marshal(id.str)
}
return json.Marshal(id.int)
}
func (id *ID) UnmarshalJSON(data []byte) error {
*id = ID{}
if len(data) > 0 && data[0] == '"' {
return json.Unmarshal(data, &id.str)
}
return json.Unmarshal(data, &id.int)
}
type Message struct {
JSONRPC JSONRPCVersion `json:"jsonrpc"`
}
type NotificationMessage struct {
Message
Method Method `json:"method"`
Params any `json:"params"`
}
type RequestMessage struct {
Message
ID *ID `json:"id"`
Method Method `json:"method"`
Params any `json:"params"`
}
type RequestOrNotificationMessage struct {
NotificationMessage *NotificationMessage
RequestMessage *RequestMessage
}
func (r *RequestOrNotificationMessage) UnmarshalJSON(data []byte) error {
var raw struct {
JSONRPC JSONRPCVersion `json:"jsonrpc"`
ID *ID `json:"id,omitzero"`
Method Method `json:"method"`
Params json.RawMessage `json:"params"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return fmt.Errorf("%w: %w", ErrInvalidRequest, err)
}
params, err := unmarshalParams(raw.Method, raw.Params)
if err != nil {
return err
}
if raw.ID != nil {
r.RequestMessage = &RequestMessage{
ID: raw.ID,
Method: raw.Method,
Params: params,
}
} else {
r.NotificationMessage = &NotificationMessage{
Method: raw.Method,
Params: params,
}
}
return nil
}
func unmarshalParams(rawMethod Method, rawParams []byte) (any, error) {
if rawMethod == MethodShutdown || rawMethod == MethodExit {
// These methods have no params.
return nil, nil
}
var params any
var err error
if unmarshaller, ok := unmarshallers[rawMethod]; ok {
params, err = unmarshaller(rawParams)
} else {
// Fall back to default; it's probably an unknown message and we will probably not handle it.
err = json.Unmarshal(rawParams, ¶ms)
}
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrInvalidRequest, err)
}
return params, nil
}
type ResponseMessage struct {
Message
ID *ID `json:"id,omitzero"`
Result any `json:"result"`
Error *ResponseError `json:"error,omitzero"`
}
type ResponseError struct {
Code int32 `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitzero"`
}