rpc-go/protocol/json/client_notification.go
crusader 23658adc4b ing
2018-04-05 10:12:33 +09:00

58 lines
1.5 KiB
Go

package json
import (
"encoding/json"
"fmt"
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) HasResponse() bool {
return false
}
func (cnc *ClientNotificationCodec) Method() string {
return cnc.noti.Method
}
func (cnc *ClientNotificationCodec) ReadParams(args []interface{}) error {
if nil != cnc.noti.Params {
// 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 {
return err
}
return nil
}
return fmt.Errorf("There is not params")
}
func (cnc *ClientNotificationCodec) Params() ([]string, error) {
if nil != cnc.noti.Params {
var values []string
if err := json.Unmarshal(*cnc.noti.Params, &values); err != nil {
return nil, err
}
return values, nil
}
return nil, fmt.Errorf("There is not params")
}