mirror of
https://github.com/tendermint/tendermint.git
synced 2026-07-19 22:42:24 +00:00
rpc: clean up the RPCFunc constructor signature (#7586)
Instead of taking a comma-separated string of parameter names, take each parameter name as a separate argument. Now that we no longer have an extra flag for caching, this fits nicely into a variadic trailer. * Update all usage of NewRPCFunc and NewWSRPCFunc.
This commit is contained in:
+1
-1
@@ -55,7 +55,7 @@
|
||||
// Define some routes
|
||||
//
|
||||
// var Routes = map[string]*rpcserver.RPCFunc{
|
||||
// "status": rpcserver.NewRPCFunc(Status, "arg", false),
|
||||
// "status": rpcserver.NewRPCFunc(Status, "arg"),
|
||||
// }
|
||||
//
|
||||
// An rpc function:
|
||||
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
|
||||
func testMux() *http.ServeMux {
|
||||
funcMap := map[string]*RPCFunc{
|
||||
"c": NewRPCFunc(func(ctx context.Context, s string, i int) (string, error) { return "foo", nil }, "s,i"),
|
||||
"c": NewRPCFunc(func(ctx context.Context, s string, i int) (string, error) { return "foo", nil }, "s", "i"),
|
||||
"block": NewRPCFunc(func(ctx context.Context, h int) (string, error) { return "block", nil }, "height"),
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
|
||||
@@ -135,7 +135,7 @@ func TestParseJSONArray(t *testing.T) {
|
||||
|
||||
func TestParseJSONRPC(t *testing.T) {
|
||||
demo := func(ctx context.Context, height int, name string) {}
|
||||
call := NewRPCFunc(demo, "height,name")
|
||||
call := NewRPCFunc(demo, "height", "name")
|
||||
|
||||
cases := []struct {
|
||||
raw string
|
||||
@@ -172,7 +172,7 @@ func TestParseJSONRPC(t *testing.T) {
|
||||
|
||||
func TestParseURI(t *testing.T) {
|
||||
demo := func(ctx context.Context, height int, name string) {}
|
||||
call := NewRPCFunc(demo, "height,name")
|
||||
call := NewRPCFunc(demo, "height", "name")
|
||||
|
||||
cases := []struct {
|
||||
raw []string
|
||||
|
||||
@@ -3,7 +3,6 @@ package server
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
)
|
||||
@@ -35,26 +34,22 @@ type RPCFunc struct {
|
||||
|
||||
// NewRPCFunc wraps a function for introspection.
|
||||
// f is the function, args are comma separated argument names
|
||||
func NewRPCFunc(f interface{}, args string) *RPCFunc {
|
||||
return newRPCFunc(f, args, false)
|
||||
func NewRPCFunc(f interface{}, argNames ...string) *RPCFunc {
|
||||
return newRPCFunc(f, argNames, false)
|
||||
}
|
||||
|
||||
// NewWSRPCFunc wraps a function for introspection and use in the websockets.
|
||||
func NewWSRPCFunc(f interface{}, args string) *RPCFunc {
|
||||
return newRPCFunc(f, args, true)
|
||||
func NewWSRPCFunc(f interface{}, argNames ...string) *RPCFunc {
|
||||
return newRPCFunc(f, argNames, true)
|
||||
}
|
||||
|
||||
func newRPCFunc(f interface{}, args string, ws bool) *RPCFunc {
|
||||
var argNames []string
|
||||
if args != "" {
|
||||
argNames = strings.Split(args, ",")
|
||||
}
|
||||
func newRPCFunc(f interface{}, argNames []string, wsOnly bool) *RPCFunc {
|
||||
return &RPCFunc{
|
||||
f: reflect.ValueOf(f),
|
||||
args: funcArgTypes(f),
|
||||
returns: funcReturnTypes(f),
|
||||
argNames: argNames,
|
||||
ws: ws,
|
||||
ws: wsOnly,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ func TestWebsocketManagerHandler(t *testing.T) {
|
||||
|
||||
func newWSServer(t *testing.T, logger log.Logger) *httptest.Server {
|
||||
funcMap := map[string]*RPCFunc{
|
||||
"c": NewWSRPCFunc(func(ctx context.Context, s string, i int) (string, error) { return "foo", nil }, "s,i"),
|
||||
"c": NewWSRPCFunc(func(ctx context.Context, s string, i int) (string, error) { return "foo", nil }, "s", "i"),
|
||||
}
|
||||
wm := NewWebsocketManager(logger, funcMap)
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
var routes = map[string]*rpcserver.RPCFunc{
|
||||
"hello_world": rpcserver.NewRPCFunc(HelloWorld, "name,num"),
|
||||
"hello_world": rpcserver.NewRPCFunc(HelloWorld, "name", "num"),
|
||||
}
|
||||
|
||||
func HelloWorld(ctx context.Context, name string, num int) (Result, error) {
|
||||
|
||||
Reference in New Issue
Block a user