Skip to content

Commit e9eadfa

Browse files
committed
types: improve "Request" and "Response" document based on JSON-RPC spec
1 parent bbff146 commit e9eadfa

File tree

1 file changed

+61
-35
lines changed

1 file changed

+61
-35
lines changed

types.go

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,34 @@ func (id *ID) UnmarshalJSON(data []byte) error {
5858
var _ json.Marshaler = (*ID)(nil)
5959
var _ json.Unmarshaler = (*ID)(nil)
6060

61-
// Request is a request message to describe a request between the client and the server.
61+
// Request represents a rpc call by sending a request object to a Server.
62+
// This is a request message to describe a request between the client and the server.
6263
//
6364
// Every processed request must send a response back to the sender of the request.
6465
type Request struct {
65-
// JSONRPC is a general message as defined by JSON-RPC.
66+
// JSONRPC is a string specifying the version of the JSON-RPC protocol.
67+
//
68+
// MUST be exactly "2.0".
6669
JSONRPC string `json:"jsonrpc"`
6770

68-
// The request id.
69-
ID *ID `json:"id"`
70-
71-
// The method to be invoked.
71+
// Method is a string containing the name of the method to be invoked.
72+
//
73+
// Method names that begin with the word rpc followed by a period character (U+002E or ASCII 46) are reserved
74+
// for rpc-internal methods and extensions and MUST NOT be used for anything else.
7275
Method string `json:"method"`
7376

74-
// The method's params.
77+
// Params is a string containing the name of the method to be invoked.
78+
//
79+
// Method names that begin with the word rpc followed by a period character (U+002E or ASCII 46) are reserved
80+
// for rpc-internal methods and extensions and MUST NOT be used for anything else.
7581
Params *json.RawMessage `json:"params,omitempty"`
82+
83+
// ID is an identifier established by the Client that MUST contain a String, Number, or NULL value if included.
84+
//
85+
// If it is not included it is assumed to be a notification.
86+
//
87+
// The value SHOULD normally not be Null and Numbers SHOULD NOT contain fractional parts.
88+
ID *ID `json:"id"`
7689
}
7790

7891
// IsNotify returns true if this request is a notification.
@@ -81,57 +94,70 @@ func (r *Request) IsNotify() bool {
8194
}
8295

8396
// Response is a response ressage sent as a result of a request.
97+
// When a rpc call is made, the Server MUST reply with a Response, except for in the case of Notifications.
8498
//
8599
// If a request doesn't provide a result value the receiver of a request still needs to return a response message to
86100
// conform to the JSON RPC specification.
87101
// The result property of the ResponseMessage should be set to null in this case to signal a successful request.
88102
type Response struct {
89-
// JSONRPC is a general message as defined by JSON-RPC.
103+
// JSONRPC is a string specifying the version of the JSON-RPC protocol.
104+
//
105+
// MUST be exactly "2.0".
90106
JSONRPC string `json:"jsonrpc"`
91107

92-
// The request id.
93-
ID *ID `json:"id"`
108+
// Result is the result of a request.
109+
//
110+
// This member is REQUIRED on success.
111+
// This member MUST NOT exist if there was an error invoking the method.
112+
//
113+
// The value of this member is determined by the method invoked on the Server.
114+
Result *json.RawMessage `json:"result,omitempty"`
94115

95-
// The error object in case a request fails.
116+
// Error is the object in case a request fails.
117+
//
118+
// This member is REQUIRED on error.
119+
// This member MUST NOT exist if there was no error triggered during invocation.
120+
//
121+
// The value for this member MUST be an Object.
96122
Error *Error `json:"error,omitempty"`
97123

98-
// The result of a request. This member is REQUIRED on success.
99-
// This member MUST NOT exist if there was an error invoking the method.
100-
Result *json.RawMessage `json:"result,omitempty"`
124+
// ID is the request id.
125+
//
126+
// This member is REQUIRED.
127+
// It MUST be the same as the value of the id member in the Request Object.
128+
//
129+
// If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request), it MUST be Null.
130+
ID *ID `json:"id"`
101131
}
102132

103133
// Combined represents a all the fields of both Request and Response.
104134
type Combined struct {
105-
// JSONRPC is a general message as defined by JSON-RPC.
106-
JSONRPC string `json:"jsonrpc"`
107-
108-
// The request id.
109-
ID *ID `json:"id,omitempty"`
110-
111-
// The method to be invoked.
112-
Method string `json:"method"`
113-
114-
// The method's params.
115-
Params *json.RawMessage `json:"params,omitempty"`
116-
117-
// The error object in case a request fails.
118-
Error *Error `json:"error,omitempty"`
119-
120-
// The result of a request. This member is REQUIRED on success.
121-
// This member MUST NOT exist if there was an error invoking the method.
122-
Result *json.RawMessage `json:"result,omitempty"`
135+
JSONRPC string `json:"jsonrpc"`
136+
Method string `json:"method"`
137+
Params *json.RawMessage `json:"params,omitempty"`
138+
Result *json.RawMessage `json:"result,omitempty"`
139+
Error *Error `json:"error,omitempty"`
140+
ID *ID `json:"id,omitempty"`
123141
}
124142

125143
// NotificationMessage is a notification message.
126144
//
127145
// A processed notification message must not send a response back. They work like events.
128146
type NotificationMessage struct {
129-
// JSONRPC is a general message as defined by JSON-RPC.
147+
// JSONRPC is a string specifying the version of the JSON-RPC protocol.
148+
//
149+
// MUST be exactly "2.0".
130150
JSONRPC string `json:"jsonrpc"`
131151

132-
// Method is the method to be invoked.
152+
// Method is a string containing the name of the method to be invoked.
153+
//
154+
// Method names that begin with the word rpc followed by a period character (U+002E or ASCII 46) are reserved
155+
// for rpc-internal methods and extensions and MUST NOT be used for anything else.
133156
Method string `json:"method"`
134157

135-
// Params is the notification's params.
158+
// Params is a string containing the name of the method to be invoked.
159+
//
160+
// Method names that begin with the word rpc followed by a period character (U+002E or ASCII 46) are reserved
161+
// for rpc-internal methods and extensions and MUST NOT be used for anything else.
136162
Params *json.RawMessage `json:"params,omitempty"`
137163
}

0 commit comments

Comments
 (0)