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
|
package message
import (
"encoding/json"
"fmt"
)
// RequestID is used to identify a request.
type RequestID = string
// SubscriptionID is used to identify a subscription.
type SubscriptionID = int
// Request represents a JSON-RPC request message.
// It includes the JSON-RPC version, an identifier for the request, the method
// to be invoked, and optional parameters.
type Request struct {
JSONRPC string `json:"jsonrpc"` // JSON-RPC version, typically "2.0".
ID RequestID `json:"id"` // Unique identifier for the request, can be a number or a string.
Method string `json:"method"` // The name of the method to be invoked.
Params *json.RawMessage `json:"params,omitempty"` // Optional parameters for the method.
}
// Response represents a JSON-RPC response message.
// It includes the JSON-RPC version, an identifier matching the request, the result of the request, and an optional error.
type Response struct {
JSONRPC string `json:"jsonrpc"` // JSON-RPC version, typically "2.0".
ID *RequestID `json:"id,omitempty"` // Unique identifier matching the request ID, can be null for notifications.
Result json.RawMessage `json:"result,omitempty"` // Result of the request if it was successful.
Error *Error `json:"error,omitempty"` // Error object if the request failed.
}
// Notification represents a JSON-RPC notification message.
type Notification struct {
JSONRPC string `json:"jsonrpc"` // JSON-RPC version, typically "2.0".
Method string `json:"method"` // The name of the method to be invoked.
Params json.RawMessage `json:"params,omitempty"` // Optional parameters for the method.
}
// NotificationResult represents the result of a subscription notification.
// It includes the result and the subscription ID that triggered the notification.
type NotificationResult struct {
Result json.RawMessage `json:"result,omitempty"` // Result data of the notification.
Subscription SubscriptionID `json:"subscription"` // ID of the subscription that triggered the notification.
}
// Error represents an error in a JSON-RPC response.
// It includes an error code, a message, and optional additional data.
type Error struct {
Code int `json:"code"` // Error code indicating the type of error.
Message string `json:"message"` // Human-readable error message.
Data json.RawMessage `json:"data,omitempty"` // Optional additional data about the error.
}
func (req *Request) String() string {
return fmt.Sprintf("{JSONRPC: %s, ID: %s, METHOD: %s, PARAMS: %s}", req.JSONRPC, req.ID, req.Method, *req.Params)
}
func (res *Response) String() string {
return fmt.Sprintf("{JSONRPC: %s, ID: %v, RESULT: %v, ERROR: %v}", res.JSONRPC, res.ID, res.Result, res.Error)
}
func (nt *Notification) String() string {
return fmt.Sprintf("{JSONRPC: %s, METHOD: %s, PARAMS: %s}", nt.JSONRPC, nt.Method, nt.Params)
}
func (err *Error) String() string {
return fmt.Sprintf("{CODE: %d, MESSAGE: %s, DATA: %b}", err.Code, err.Message, err.Data)
}
|