diff options
author | hozan23 <hozan23@karyontech.net> | 2024-06-13 06:02:24 +0200 |
---|---|---|
committer | hozan23 <hozan23@karyontech.net> | 2024-06-13 06:02:24 +0200 |
commit | 8c2d37e093ca64d591fc0aec15a7e2ed424b2e47 (patch) | |
tree | fd9bc62e29087a18e7eb4bdd0a1f587ba63e5dd5 /jsonrpc/message | |
parent | a338905a7f8a2206161cc15f07bda872b9bfc09c (diff) |
use message dispatcher to process responses and notifications & spread out comments
Diffstat (limited to 'jsonrpc/message')
-rw-r--r-- | jsonrpc/message/message.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/jsonrpc/message/message.go b/jsonrpc/message/message.go new file mode 100644 index 0000000..ff68da2 --- /dev/null +++ b/jsonrpc/message/message.go @@ -0,0 +1,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: %s, RESULT: %s, 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) +} |