rpc-go/protocol/json/client_notification.go

54 lines
1.5 KiB
Go
Raw Normal View History

2018-04-03 08:58:26 +00:00
package json
import (
"encoding/json"
2018-04-05 01:12:33 +00:00
"fmt"
2018-04-03 08:58:26 +00:00
cuej "git.loafle.net/commons/util-go/encoding/json"
)
// ----------------------------------------------------------------------------
// 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
}
func (cnc *ClientNotificationCodec) Method() string {
return cnc.noti.Method
}
func (cnc *ClientNotificationCodec) ReadParams(args []interface{}) error {
2018-04-05 01:12:33 +00:00
if nil != cnc.noti.Params {
2018-04-03 08:58:26 +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-04-10 09:02:35 +00:00
if err := cuej.SetValueWithJSONStringArrayBytes(*cnc.noti.Params, args); nil != err {
2018-04-05 01:12:33 +00:00
return err
2018-04-03 08:58:26 +00:00
}
return nil
}
2018-04-05 01:12:33 +00:00
return fmt.Errorf("There is not params")
2018-04-03 08:58:26 +00:00
}
func (cnc *ClientNotificationCodec) Params() ([]string, error) {
2018-04-05 01:12:33 +00:00
if nil != cnc.noti.Params {
2018-04-03 08:58:26 +00:00
var values []string
if err := json.Unmarshal(*cnc.noti.Params, &values); err != nil {
2018-04-05 01:12:33 +00:00
return nil, err
2018-04-03 08:58:26 +00:00
}
return values, nil
}
2018-04-05 01:12:33 +00:00
return nil, fmt.Errorf("There is not params")
2018-04-03 08:58:26 +00:00
}