-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
44 lines (38 loc) · 1.01 KB
/
message.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
package jsonrpc
import (
"encoding/json"
)
// Message incoming message
type Message struct {
ID json.RawMessage `json:"id"`
Version string `json:"jsonrpc"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
Result json.RawMessage `json:"result"`
Error *Error `json:"error"`
}
// IsRequest checks that incoming message is JSONRPC request
func (message Message) IsRequest() bool {
if message.Result == nil && message.Error == nil {
return true
}
return false
}
// ToRequest build JSONRPC request object from Message struct
func (message Message) ToRequest() Request {
return Request{
ID: message.ID,
Method: message.Method,
Params: &Params{Data: message.Params},
Version: message.Version,
}
}
// ToResponse build JSONRPC response object from Message struct
func (message Message) ToResponse() Response {
return Response{
ID: message.ID,
Result: &Params{Data: message.Result},
Error: message.Error,
Version: message.Version,
}
}