diff --git a/rpc/codec.go b/rpc/codec.go new file mode 100644 index 0000000000000000000000000000000000000000..750a5a88e8a6ea672fe635ef41c2f69d2c36274d --- /dev/null +++ b/rpc/codec.go @@ -0,0 +1,28 @@ +// Copyright 2017 Monax Industries Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package rpc + +import ( + "io" +) + +// Used for rpc request and response data. +type Codec interface { + EncodeBytes(interface{}) ([]byte, error) + Encode(interface{}, io.Writer) error + DecodeBytes(interface{}, []byte) error + DecodeBytesPtr(interface{}, []byte) error + Decode(interface{}, io.Reader) error +} diff --git a/rpc/rpc.go b/rpc/jsonrpc.go similarity index 86% rename from rpc/rpc.go rename to rpc/jsonrpc.go index a42e57c0ca349f11cc007fffaf3556e5c9128787..99162494b9bac5036d83921b1f0a061764746921 100644 --- a/rpc/rpc.go +++ b/rpc/jsonrpc.go @@ -16,16 +16,15 @@ package rpc import ( "encoding/json" - "io" ) // JSON-RPC 2.0 error codes. const ( - PARSE_ERROR = -32700 INVALID_REQUEST = -32600 METHOD_NOT_FOUND = -32601 INVALID_PARAMS = -32602 INTERNAL_ERROR = -32603 + PARSE_ERROR = -32700 ) // Request and Response objects. Id is a string. Error data not used. @@ -65,26 +64,8 @@ type ( // Note: Data is currently unused, and the data member may be omitted // Data interface{} `json:"data"` } - - Codec interface { - EncodeBytes(interface{}) ([]byte, error) - Encode(interface{}, io.Writer) error - DecodeBytes(interface{}, []byte) error - Decode(interface{}, io.Reader) error - } ) -// Create a new RPC request. This is the generic struct that is passed to RPC -// methods -func NewRPCRequest(id string, method string, params json.RawMessage) *RPCRequest { - return &RPCRequest{ - JSONRPC: "2.0", - Id: id, - Method: method, - Params: params, - } -} - // NewRPCResponse creates a new response object from a result func NewRPCResponse(id string, res interface{}) RPCResponse { return RPCResponse(&RPCResultResponse{ diff --git a/rpc/rpc_test.go b/rpc/rpc_test.go new file mode 100644 index 0000000000000000000000000000000000000000..fbdfca374c8cd4a764ea064d06e74ce5b5ca0e4b --- /dev/null +++ b/rpc/rpc_test.go @@ -0,0 +1,48 @@ +// Copyright 2017 Monax Industries Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package rpc + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +// ... +func TestNewJsonRpcResponse(t *testing.T) { + id := "testId" + data := "a string" + resp := RPCResponse(&RPCResultResponse{ + Result: data, + Id: id, + JSONRPC: "2.0", + }) + respGen := NewRPCResponse(id, data) + assert.Equal(t, respGen, resp) +} + +// ... +func TestNewJsonRpcErrorResponse(t *testing.T) { + id := "testId" + code := 100 + message := "the error" + resp := RPCResponse(&RPCErrorResponse{ + Error: &RPCError{code, message}, + Id: id, + JSONRPC: "2.0", + }) + respGen := NewRPCErrorResponse(id, code, message) + assert.Equal(t, respGen, resp) +} diff --git a/rpc/v0/codec.go b/rpc/v0/codec.go index a1f4ec0aea6fef20ae988bd11690a46c5aaa2490..c26d19ee4e3bebb8756fff7c5d25708bbfe80f3a 100644 --- a/rpc/v0/codec.go +++ b/rpc/v0/codec.go @@ -45,6 +45,8 @@ func (this *TCodec) EncodeBytes(v interface{}) ([]byte, error) { return wire.JSONBytes(v), nil } +// TODO: [ben] implement EncodeBytesPtr ? + // Decode from an io.Reader. func (this *TCodec) Decode(v interface{}, r io.Reader) error { bts, errR := ioutil.ReadAll(r) @@ -59,11 +61,13 @@ func (this *TCodec) Decode(v interface{}, r io.Reader) error { // Decode from a byte array. func (this *TCodec) DecodeBytes(v interface{}, bts []byte) error { var err error - rv := reflect.ValueOf(v) - if rv.Kind() == reflect.Ptr { - wire.ReadJSONPtr(v, bts, &err) - } else { - wire.ReadJSON(v, bts, &err) - } + wire.ReadJSON(v, bts, &err) + return err +} + +// Decode from a byte array pointer. +func (this *TCodec) DecodeBytesPtr(v interface{}, bts []byte) error { + var err error + wire.ReadJSONPtr(v, bts, &err) return err }