From 42f2306c82950f3fb6429282dc3708da01b73ee1 Mon Sep 17 00:00:00 2001
From: Benjamin Bollen <ben@monax.io>
Date: Fri, 7 Apr 2017 18:54:02 +0200
Subject: [PATCH] rpc, rpc/v0: sort misalignment between master v0.16 and
 develop v0.17

---
 rpc/codec.go               | 28 ++++++++++++++++++++++
 rpc/{rpc.go => jsonrpc.go} | 21 +----------------
 rpc/rpc_test.go            | 48 ++++++++++++++++++++++++++++++++++++++
 rpc/v0/codec.go            | 16 ++++++++-----
 4 files changed, 87 insertions(+), 26 deletions(-)
 create mode 100644 rpc/codec.go
 rename rpc/{rpc.go => jsonrpc.go} (86%)
 create mode 100644 rpc/rpc_test.go

diff --git a/rpc/codec.go b/rpc/codec.go
new file mode 100644
index 00000000..750a5a88
--- /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 a42e57c0..99162494 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 00000000..fbdfca37
--- /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 a1f4ec0a..c26d19ee 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
 }
-- 
GitLab