rpc/protocol/json/client_notification.go

86 lines
2.2 KiB
Go
Raw Normal View History

2017-11-26 10:15:51 +00:00
package json
import (
"encoding/json"
2018-03-20 02:51:14 +00:00
"reflect"
2017-11-26 10:15:51 +00:00
"git.loafle.net/commons_go/rpc/codec"
2018-03-20 02:51:14 +00:00
cur "git.loafle.net/commons_go/util/reflect"
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
}
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 02:51:14 +00:00
var values []string
if err := json.Unmarshal(*cnc.noti.Params, &values); err != nil {
2017-11-28 16:19:03 +00:00
cnc.err = &Error{
2017-11-26 10:15:51 +00:00
Code: E_INVALID_REQ,
Message: err.Error(),
2017-11-28 16:19:03 +00:00
Data: cnc.noti.Params,
}
return cnc.err
}
for indexI := 0; indexI < len(args); indexI++ {
2018-03-20 02:51:14 +00:00
value := values[indexI]
2017-11-28 16:19:03 +00:00
arg := args[indexI]
2018-03-20 02:51:14 +00:00
if cur.IsTypeKind(reflect.TypeOf(arg), reflect.String, true) {
arg = arg.(*string)
arg = &value
} else {
if err := json.Unmarshal([]byte(value), &arg); err != nil {
cnc.err = &Error{
Code: E_INVALID_REQ,
Message: err.Error(),
Data: cnc.noti.Params,
}
return cnc.err
2017-11-28 16:19:03 +00:00
}
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 {
2017-11-26 10:15:51 +00:00
var results []string
2017-11-28 16:19:03 +00:00
if err := json.Unmarshal(*cnc.noti.Params, &results); err != nil {
cnc.err = &Error{
2017-11-26 10:15:51 +00:00
Code: E_INVALID_REQ,
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
}
return results, nil
}
2017-11-28 16:19:03 +00:00
return nil, cnc.err
2017-11-26 10:15:51 +00:00
}