Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,40 @@ if err := client.Call(); err != nil {
}
```

## Options

### Using `WithMethodNameFormatter`

```go
func main() {
// create a new server instance with a custom separator
rpcServer := jsonrpc.NewServer(jsonrpc.WithMethodNameFormatter(
func(namespace, method string) string {
return namespace + "_" + method
}),
)

// create a handler instance and register it
serverHandler := &SimpleServerHandler{}
rpcServer.Register("SimpleServerHandler", serverHandler)

// serve the api
testServ := httptest.NewServer(rpcServer)
defer testServ.Close()

fmt.Println("URL: ", "ws://"+testServ.Listener.Addr().String())

// rpc method becomes SimpleServerHandler_AddGet

[..do other app stuff / wait..]
}
```


## Contribute

PRs are welcome!

## License

Dual-licensed under [MIT](https://github.com/filecoin-project/go-jsonrpc/blob/master/LICENSE-MIT) + [Apache 2.0](https://github.com/filecoin-project/go-jsonrpc/blob/master/LICENSE-APACHE)
Dual-licensed under [MIT](https://github.com/filecoin-project/go-jsonrpc/blob/master/LICENSE-MIT) + [Apache 2.0](https://github.com/filecoin-project/go-jsonrpc/blob/master/LICENSE-APACHE)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/filecoin-project/go-jsonrpc

go 1.23
go 1.23.0

require (
github.com/google/uuid v1.1.1
Expand Down
6 changes: 5 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type handler struct {

paramDecoders map[reflect.Type]ParamDecoder

methodNameFormatter MethodNameFormatter

tracer Tracer
}

Expand All @@ -90,6 +92,8 @@ func makeHandler(sc ServerConfig) *handler {
aliasedMethods: map[string]string{},
paramDecoders: sc.paramDecoders,

methodNameFormatter: sc.methodNameFormatter,

maxRequestSize: sc.maxRequestSize,

tracer: sc.tracer,
Expand Down Expand Up @@ -126,7 +130,7 @@ func (s *handler) register(namespace string, r interface{}) {

valOut, errOut, _ := processFuncOut(funcType)

s.methods[namespace+"."+method.Name] = methodHandler{
s.methods[s.methodNameFormatter(namespace, method.Name)] = methodHandler{
paramReceivers: recvs,
nParams: ins,

Expand Down
12 changes: 12 additions & 0 deletions options_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type jsonrpcReverseClient struct{ reflect.Type }

type ParamDecoder func(ctx context.Context, json []byte) (reflect.Value, error)

type MethodNameFormatter func(namespace, method string) string

type ServerConfig struct {
maxRequestSize int64
pingInterval time.Duration
Expand All @@ -22,6 +24,7 @@ type ServerConfig struct {

reverseClientBuilder func(context.Context, *wsConn) (context.Context, error)
tracer Tracer
methodNameFormatter MethodNameFormatter
}

type ServerOption func(c *ServerConfig)
Expand All @@ -32,6 +35,9 @@ func defaultServerConfig() ServerConfig {
maxRequestSize: DEFAULT_MAX_REQUEST_SIZE,

pingInterval: 5 * time.Second,
methodNameFormatter: func(namespace, method string) string {
return namespace + "." + method
},
}
}

Expand Down Expand Up @@ -59,6 +65,12 @@ func WithServerPingInterval(d time.Duration) ServerOption {
}
}

func WithMethodNameFormatter(formatter MethodNameFormatter) ServerOption {
return func(c *ServerConfig) {
c.methodNameFormatter = formatter
}
}

// WithTracer allows the instantiator to trace the method calls and results.
// This is useful for debugging a client-server interaction.
func WithTracer(l Tracer) ServerOption {
Expand Down
40 changes: 39 additions & 1 deletion rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,11 @@ func TestIDHandling(t *testing.T) {
expect interface{}
expectErr bool
}{
{`{"id":"8116d306-56cc-4637-9dd7-39ce1548a5a0","jsonrpc":"2.0","method":"eth_blockNumber","params":[]}`, "8116d306-56cc-4637-9dd7-39ce1548a5a0", false},
{
`{"id":"8116d306-56cc-4637-9dd7-39ce1548a5a0","jsonrpc":"2.0","method":"eth_blockNumber","params":[]}`,
"8116d306-56cc-4637-9dd7-39ce1548a5a0",
false,
},
{`{"id":1234,"jsonrpc":"2.0","method":"eth_blockNumber","params":[]}`, float64(1234), false},
{`{"id":null,"jsonrpc":"2.0","method":"eth_blockNumber","params":[]}`, nil, false},
{`{"id":1234.0,"jsonrpc":"2.0","method":"eth_blockNumber","params":[]}`, 1234.0, false},
Expand Down Expand Up @@ -1711,3 +1715,37 @@ func TestNewCustomClient(t *testing.T) {
require.Equal(t, 13, n)
require.Equal(t, int32(13), serverHandler.n)
}

func TestReverseCallWithCustomMethodName(t *testing.T) {
// setup server

rpcServer := NewServer(WithMethodNameFormatter(func(namespace, method string) string { return namespace + "_" + method }))
rpcServer.Register("Server", &RawParamHandler{})

// httptest stuff
testServ := httptest.NewServer(rpcServer)
defer testServ.Close()

// setup client

var client struct {
Call func(ctx context.Context, ps RawParams) error `rpc_method:"Server_Call"`
}
closer, err := NewMergeClient(context.Background(), "ws://"+testServ.Listener.Addr().String(), "Server", []interface{}{
&client,
}, nil)
require.NoError(t, err)

// do the call!

e := client.Call(context.Background(), []byte(`{"I": 1}`))
require.NoError(t, e)

closer()
}

type MethodTransformedHandler struct{}

func (h *RawParamHandler) CallSomethingInSnakeCase(ctx context.Context, v int) (int, error) {
return v + 1, nil
}