72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package json
|
|
|
|
import (
|
|
"git.loafle.net/commons_go/rpc/codec"
|
|
crp "git.loafle.net/commons_go/rpc/protocol"
|
|
cuej "git.loafle.net/commons_go/util/encoding/json"
|
|
jsoniter "github.com/json-iterator/go"
|
|
)
|
|
|
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// 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 {
|
|
noti *clientNotification
|
|
err error
|
|
codec codec.Codec
|
|
}
|
|
|
|
func (cnc *ClientNotificationCodec) HasResponse() bool {
|
|
return false
|
|
}
|
|
|
|
func (cnc *ClientNotificationCodec) Method() string {
|
|
return cnc.noti.Method
|
|
}
|
|
|
|
func (cnc *ClientNotificationCodec) ReadParams(args []interface{}) error {
|
|
if cnc.err == nil && cnc.noti.Params != nil {
|
|
// 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.
|
|
if err := cuej.SetValueWithJSONStringArray(*cnc.noti.Params, args); nil != err {
|
|
cnc.err = &Error{
|
|
Code: crp.E_INVALID_REQ,
|
|
Message: err.Error(),
|
|
Data: cnc.noti.Params,
|
|
}
|
|
return cnc.err
|
|
}
|
|
return nil
|
|
}
|
|
return cnc.err
|
|
}
|
|
|
|
func (cnc *ClientNotificationCodec) Params() ([]string, error) {
|
|
if cnc.err == nil && cnc.noti.Params != nil {
|
|
var values []string
|
|
|
|
if err := json.Unmarshal(*cnc.noti.Params, &values); err != nil {
|
|
cnc.err = &Error{
|
|
Code: crp.E_INVALID_REQ,
|
|
Message: err.Error(),
|
|
Data: cnc.noti.Params,
|
|
}
|
|
return nil, cnc.err
|
|
}
|
|
|
|
return values, nil
|
|
}
|
|
return nil, cnc.err
|
|
}
|