2018-04-03 08:58:26 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.loafle.net/commons/rpc-go/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// ClientResponseCodec
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// clientResponse represents a JSON-RPC response returned to a client.
|
|
|
|
type clientResponse struct {
|
|
|
|
Version string `json:"jsonrpc"`
|
|
|
|
Result *json.RawMessage `json:"result,omitempty"`
|
2018-04-05 01:12:33 +00:00
|
|
|
Error *protocol.Error `json:"error,omitempty"`
|
2018-04-03 08:58:26 +00:00
|
|
|
ID interface{} `json:"id,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientResponseCodec decodes and encodes a single request.
|
|
|
|
type ClientResponseCodec struct {
|
|
|
|
res *clientResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crc *ClientResponseCodec) ID() interface{} {
|
|
|
|
return crc.res.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crc *ClientResponseCodec) Result(result interface{}) error {
|
2018-04-05 01:12:33 +00:00
|
|
|
if nil == crc.res.Error && nil != crc.res.Result {
|
2018-04-03 08:58:26 +00:00
|
|
|
if err := json.Unmarshal(*crc.res.Result, result); nil != err {
|
2018-04-05 01:12:33 +00:00
|
|
|
return err
|
2018-04-03 08:58:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 01:12:33 +00:00
|
|
|
return fmt.Errorf("There is no result")
|
2018-04-03 08:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (crc *ClientResponseCodec) Error() *protocol.Error {
|
2018-04-05 01:12:33 +00:00
|
|
|
return crc.res.Error
|
2018-04-03 08:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec, error) {
|
|
|
|
if nil != crc.res.ID || nil == crc.res.Result {
|
2018-04-05 01:12:33 +00:00
|
|
|
return nil, fmt.Errorf("This is not notification")
|
2018-04-03 08:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
noti := &clientNotification{}
|
|
|
|
err := json.Unmarshal(*crc.res.Result, noti)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-05 01:12:33 +00:00
|
|
|
return &ClientNotificationCodec{noti: noti}, nil
|
2018-04-03 08:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newClientMessageCodec returns a new ClientMessageCodec.
|
|
|
|
func newClientResponseCodec(buf []byte) (protocol.ClientResponseCodec, error) {
|
|
|
|
|
|
|
|
res := &clientResponse{}
|
|
|
|
err := json.Unmarshal(buf, res)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-04-05 01:12:33 +00:00
|
|
|
return nil, fmt.Errorf("Cannot unmarshal response [%s] err: %v", string(buf), err)
|
2018-04-03 08:58:26 +00:00
|
|
|
}
|
|
|
|
if res.Version != Version {
|
2018-04-05 01:12:33 +00:00
|
|
|
return nil, fmt.Errorf("The protocol version of response[%s] is not %s", string(buf), Version)
|
2018-04-03 08:58:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 01:12:33 +00:00
|
|
|
return &ClientResponseCodec{res: res}, nil
|
2018-04-03 08:58:26 +00:00
|
|
|
}
|