2017-11-26 10:15:51 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2018-03-23 03:22:16 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2017-11-26 10:15:51 +00:00
|
|
|
"git.loafle.net/commons_go/rpc/codec"
|
2018-03-20 06:31:54 +00:00
|
|
|
crp "git.loafle.net/commons_go/rpc/protocol"
|
2018-03-20 05:21:44 +00:00
|
|
|
cuej "git.loafle.net/commons_go/util/encoding/json"
|
2018-03-23 02:28:59 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2017-11-26 10:15:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// ClientNotificationCodec
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// clientRequest represents a JSON-RPC notification sent to a client.
|
|
|
|
type clientNotification struct {
|
|
|
|
// A String containing the name of the method to be invoked.
|
|
|
|
Method string `json:"method"`
|
|
|
|
// Object to pass as request parameter to the method.
|
|
|
|
Params *json.RawMessage `json:"params,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientNotificationCodec decodes and encodes a single notification.
|
|
|
|
type ClientNotificationCodec struct {
|
2017-11-28 16:19:03 +00:00
|
|
|
noti *clientNotification
|
|
|
|
err error
|
|
|
|
codec codec.Codec
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:37:28 +00:00
|
|
|
func (cnc *ClientNotificationCodec) HasResponse() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-28 16:19:03 +00:00
|
|
|
func (cnc *ClientNotificationCodec) Method() string {
|
|
|
|
return cnc.noti.Method
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:19:03 +00:00
|
|
|
func (cnc *ClientNotificationCodec) ReadParams(args []interface{}) error {
|
|
|
|
if cnc.err == nil && cnc.noti.Params != nil {
|
2017-11-26 10:15:51 +00:00
|
|
|
// Note: if scr.request.Params is nil it's not an error, it's an optional member.
|
|
|
|
// JSON params structured object. Unmarshal to the args object.
|
2018-03-20 05:21:44 +00:00
|
|
|
if err := cuej.SetValueWithJSONStringArray(*cnc.noti.Params, args); nil != err {
|
2017-11-28 16:19:03 +00:00
|
|
|
cnc.err = &Error{
|
2018-03-20 06:31:54 +00:00
|
|
|
Code: crp.E_INVALID_REQ,
|
2017-11-26 10:15:51 +00:00
|
|
|
Message: err.Error(),
|
2017-11-28 16:19:03 +00:00
|
|
|
Data: cnc.noti.Params,
|
|
|
|
}
|
|
|
|
return cnc.err
|
|
|
|
}
|
2018-03-20 05:21:44 +00:00
|
|
|
return nil
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
2017-11-28 16:19:03 +00:00
|
|
|
return cnc.err
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:19:03 +00:00
|
|
|
func (cnc *ClientNotificationCodec) Params() ([]string, error) {
|
|
|
|
if cnc.err == nil && cnc.noti.Params != nil {
|
2018-03-20 06:31:54 +00:00
|
|
|
var values []string
|
2017-11-26 10:15:51 +00:00
|
|
|
|
2018-03-23 03:22:16 +00:00
|
|
|
if err := jsoniter.Unmarshal(*cnc.noti.Params, &values); err != nil {
|
2017-11-28 16:19:03 +00:00
|
|
|
cnc.err = &Error{
|
2018-03-20 06:31:54 +00:00
|
|
|
Code: crp.E_INVALID_REQ,
|
2017-11-26 10:15:51 +00:00
|
|
|
Message: err.Error(),
|
2017-11-28 16:19:03 +00:00
|
|
|
Data: cnc.noti.Params,
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
2017-11-28 16:19:03 +00:00
|
|
|
return nil, cnc.err
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 06:31:54 +00:00
|
|
|
return values, nil
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
2017-11-28 16:19:03 +00:00
|
|
|
return nil, cnc.err
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|