-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
135 lines (110 loc) · 2.72 KB
/
types.go
File metadata and controls
135 lines (110 loc) · 2.72 KB
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
135
// jsoff is JSONRPC 2.0 libaray in golang
package jsoff
import (
log "github.com/sirupsen/logrus"
"net/http"
)
type UID string
// message kinds
const (
MKRequest = iota
MKNotify
MKResult
MKError
)
// RPC error object
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
type MessageOptions struct {
IdNotNull bool // Request.id cannot be null
}
// The abstract interface of JSONRPC message. refer to
// https://www.jsonrpc.org/specification
//
// data Message = Request id method params |
//
// Notify method params |
// Result id result |
// Error id error={ code message data }
type Message interface {
// Return's the judgement of message types
IsRequest() bool
IsNotify() bool
IsResponse() bool
IsResult() bool
IsError() bool
IsResultOrError() bool
// TraceId can be used to analyse the flow of whole message
// transportation
SetTraceId(traceId string)
TraceId() string
// Returns template structures, this structure can be used to
// marshal and turn into map
Interface() any
// MustXX are convenience methods to make code cleaner by
// avoiding frequent type casting, Note that there will be
// panics when used inproperly, add some IsXX type checking
// beforehead to add guarantee.
// MustId returns the Id field of a message, will panic when
// message is a Notify
MustId() any
// MustMethod returns the method of a message, will panic when
// message is an Result or Error.
MustMethod() string
// MustParams returns the params of a message, will panic when
// message is a Result or Error
MustParams() []any
// MustResult returns the result field of a message, will
// panic when the message is not a Result
MustResult() any
// MustError returns the error field of a message, will panic
// when the message is not an Error
MustError() *RPCError
// Clone the message with a new Id
ReplaceId(id any) Message
// Log returns a Logger object with message specific
// infomations attached.
Log() *log.Entry
}
// The base class of JSONRPC types
type BaseMessage struct {
kind int
traceId string
}
// Request message kind
type RequestMessage struct {
BaseMessage
Id any
Method string
Params []any
paramsAreList bool
// request specific fields
}
// Notify message kind
type NotifyMessage struct {
BaseMessage
Method string
Params []any
paramsAreList bool
}
type ResponseMessage interface {
HasResponseHeader() bool
ResponseHeader() http.Header
}
// Result message kind
type ResultMessage struct {
BaseMessage
Id any
Result any
responseHeader http.Header
}
// Error message kind
type ErrorMessage struct {
BaseMessage
Id any
Error *RPCError
responseHeader http.Header
}