-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessagefactory.go
More file actions
51 lines (41 loc) · 1.26 KB
/
messagefactory.go
File metadata and controls
51 lines (41 loc) · 1.26 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
package jsoff
import (
"encoding/json"
)
type MessageFactory struct {
opts MessageOptions
}
func NewMessageFactory(options ...MessageOptions) *MessageFactory {
opts := MessageOptions{}
if len(options) > 0 {
opts = options[0]
}
return &MessageFactory{opts: opts}
}
func (f MessageFactory) Parse(data []byte) (Message, error) {
return ParseBytes(data, f.opts)
}
func (f MessageFactory) Decode(decoder *json.Decoder) (Message, error) {
return DecodeMessage(decoder, f.opts)
}
func (f MessageFactory) NewRequest(id any, method string, params any) *RequestMessage {
if f.opts.IdNotNull && id == nil {
panic("Request id cannot be null")
}
return NewRequestMessage(id, method, params)
}
func (f MessageFactory) NewNotify(method string, params any) *NotifyMessage {
return NewNotifyMessage(method, params)
}
func (f MessageFactory) NewResult(reqmsg Message, result any) *ResultMessage {
if f.opts.IdNotNull && (reqmsg == nil || reqmsg.MustId() == nil) {
panic("Result id cannot be null")
}
return NewResultMessage(reqmsg, result)
}
func (f MessageFactory) NewError(reqmsg Message, errbody *RPCError) *ErrorMessage {
if f.opts.IdNotNull && (reqmsg == nil || reqmsg.MustId() == nil) {
panic("Error id cannot be null")
}
return NewErrorMessage(reqmsg, errbody)
}