Skip to content

Commit 15eee17

Browse files
authored
Merge pull request #1 from filecoin-project/master
add options to set custom namespace separator and a method name trans…
2 parents 9d4c6fb + 8e8f524 commit 15eee17

File tree

5 files changed

+88
-4
lines changed

5 files changed

+88
-4
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,40 @@ if err := client.Call(); err != nil {
244244
}
245245
```
246246

247+
## Options
248+
249+
### Using `WithMethodNameFormatter`
250+
251+
```go
252+
func main() {
253+
// create a new server instance with a custom separator
254+
rpcServer := jsonrpc.NewServer(jsonrpc.WithMethodNameFormatter(
255+
func(namespace, method string) string {
256+
return namespace + "_" + method
257+
}),
258+
)
259+
260+
// create a handler instance and register it
261+
serverHandler := &SimpleServerHandler{}
262+
rpcServer.Register("SimpleServerHandler", serverHandler)
263+
264+
// serve the api
265+
testServ := httptest.NewServer(rpcServer)
266+
defer testServ.Close()
267+
268+
fmt.Println("URL: ", "ws://"+testServ.Listener.Addr().String())
269+
270+
// rpc method becomes SimpleServerHandler_AddGet
271+
272+
[..do other app stuff / wait..]
273+
}
274+
```
275+
276+
247277
## Contribute
248278

249279
PRs are welcome!
250280

251281
## License
252282

253-
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)
283+
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)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/filecoin-project/go-jsonrpc
22

3-
go 1.23
3+
go 1.23.0
44

55
require (
66
github.com/google/uuid v1.1.1

handler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ type handler struct {
7777

7878
paramDecoders map[reflect.Type]ParamDecoder
7979

80+
methodNameFormatter MethodNameFormatter
81+
8082
tracer Tracer
8183
}
8284

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

95+
methodNameFormatter: sc.methodNameFormatter,
96+
9397
maxRequestSize: sc.maxRequestSize,
9498

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

127131
valOut, errOut, _ := processFuncOut(funcType)
128132

129-
s.methods[namespace+"."+method.Name] = methodHandler{
133+
s.methods[s.methodNameFormatter(namespace, method.Name)] = methodHandler{
130134
paramReceivers: recvs,
131135
nParams: ins,
132136

options_server.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type jsonrpcReverseClient struct{ reflect.Type }
1313

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

16+
type MethodNameFormatter func(namespace, method string) string
17+
1618
type ServerConfig struct {
1719
maxRequestSize int64
1820
pingInterval time.Duration
@@ -22,6 +24,7 @@ type ServerConfig struct {
2224

2325
reverseClientBuilder func(context.Context, *wsConn) (context.Context, error)
2426
tracer Tracer
27+
methodNameFormatter MethodNameFormatter
2528
}
2629

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

3437
pingInterval: 5 * time.Second,
38+
methodNameFormatter: func(namespace, method string) string {
39+
return namespace + "." + method
40+
},
3541
}
3642
}
3743

@@ -59,6 +65,12 @@ func WithServerPingInterval(d time.Duration) ServerOption {
5965
}
6066
}
6167

68+
func WithMethodNameFormatter(formatter MethodNameFormatter) ServerOption {
69+
return func(c *ServerConfig) {
70+
c.methodNameFormatter = formatter
71+
}
72+
}
73+
6274
// WithTracer allows the instantiator to trace the method calls and results.
6375
// This is useful for debugging a client-server interaction.
6476
func WithTracer(l Tracer) ServerOption {

rpc_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,11 @@ func TestIDHandling(t *testing.T) {
12481248
expect interface{}
12491249
expectErr bool
12501250
}{
1251-
{`{"id":"8116d306-56cc-4637-9dd7-39ce1548a5a0","jsonrpc":"2.0","method":"eth_blockNumber","params":[]}`, "8116d306-56cc-4637-9dd7-39ce1548a5a0", false},
1251+
{
1252+
`{"id":"8116d306-56cc-4637-9dd7-39ce1548a5a0","jsonrpc":"2.0","method":"eth_blockNumber","params":[]}`,
1253+
"8116d306-56cc-4637-9dd7-39ce1548a5a0",
1254+
false,
1255+
},
12521256
{`{"id":1234,"jsonrpc":"2.0","method":"eth_blockNumber","params":[]}`, float64(1234), false},
12531257
{`{"id":null,"jsonrpc":"2.0","method":"eth_blockNumber","params":[]}`, nil, false},
12541258
{`{"id":1234.0,"jsonrpc":"2.0","method":"eth_blockNumber","params":[]}`, 1234.0, false},
@@ -1711,3 +1715,37 @@ func TestNewCustomClient(t *testing.T) {
17111715
require.Equal(t, 13, n)
17121716
require.Equal(t, int32(13), serverHandler.n)
17131717
}
1718+
1719+
func TestReverseCallWithCustomMethodName(t *testing.T) {
1720+
// setup server
1721+
1722+
rpcServer := NewServer(WithMethodNameFormatter(func(namespace, method string) string { return namespace + "_" + method }))
1723+
rpcServer.Register("Server", &RawParamHandler{})
1724+
1725+
// httptest stuff
1726+
testServ := httptest.NewServer(rpcServer)
1727+
defer testServ.Close()
1728+
1729+
// setup client
1730+
1731+
var client struct {
1732+
Call func(ctx context.Context, ps RawParams) error `rpc_method:"Server_Call"`
1733+
}
1734+
closer, err := NewMergeClient(context.Background(), "ws://"+testServ.Listener.Addr().String(), "Server", []interface{}{
1735+
&client,
1736+
}, nil)
1737+
require.NoError(t, err)
1738+
1739+
// do the call!
1740+
1741+
e := client.Call(context.Background(), []byte(`{"I": 1}`))
1742+
require.NoError(t, e)
1743+
1744+
closer()
1745+
}
1746+
1747+
type MethodTransformedHandler struct{}
1748+
1749+
func (h *RawParamHandler) CallSomethingInSnakeCase(ctx context.Context, v int) (int, error) {
1750+
return v + 1, nil
1751+
}

0 commit comments

Comments
 (0)