Skip to content

Commit b997fd5

Browse files
committed
docs: expand README.md with detailed examples of method name alias usage for JSON-RPC
1 parent b710b30 commit b997fd5

File tree

1 file changed

+106
-1
lines changed

1 file changed

+106
-1
lines changed

README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,116 @@ func main() {
323323
nil,
324324
jsonrpc.WithMethodNameFormatter(jsonrpc.NewMethodNameFormatter(false, OriginalCase)),
325325
jsonrpc.WithClientHandler("Client", &RevCallTestClientHandler{}),
326-
jsonrpc.WithClientHandlerFormatter(jsonrpc.NewMethodNameFormatter(false, OriginalCase)),
326+
jsonrpc.WithClientHandlerFormatter(jsonrpc.NewMethodNameFormatter(false, OriginalCase)),
327327
)
328328
defer closer()
329329
}
330330
```
331+
### Using method name alias
332+
333+
You can also create an alias for a method name. This is useful if you want to use a different method name in the JSON-RPC
334+
request than the actual method name for a specific method.
335+
336+
#### Usage of method name alias in the server
337+
338+
```go
339+
type SimpleServerHandler struct {}
340+
341+
func (h *SimpleServerHandler) Double(in int) int {
342+
return in * 2
343+
}
344+
345+
// create a new server instance
346+
rpcServer := jsonrpc.NewServer()
347+
348+
// create a handler instance and register it
349+
serverHandler := &SimpleServerHandler{}
350+
rpcServer.Register("SimpleServerHandler", serverHandler)
351+
352+
// create an alias for the Double method. This will allow you to call the server's Double method
353+
// with the name "rand_myRandomAlias" in the JSON-RPC request.
354+
rpcServer.AliasMethod("rand_myRandomAlias", "SimpleServerHandler.Double")
355+
356+
```
357+
358+
#### Usage of method name alias with client handlers
359+
360+
```go
361+
// setup the client handler
362+
type ReverseHandler struct {}
363+
364+
func (h *ReverseHandler) DoubleOnClient(in int) int {
365+
return in * 2
366+
}
367+
368+
// create a new client instance with the client handler + method name alias
369+
closer, err := jsonrpc.NewMergeClient(
370+
context.Background(),
371+
"http://example.com",
372+
"SimpleServerHandler",
373+
[]any{&client},
374+
nil,
375+
jsonrpc.WithClientHandler("Client", &ReverseHandler{}),
376+
// this allows the server to call the client's DoubleOnClient method using the name "rand_theClientRandomAlias" in the JSON-RPC request.
377+
jsonrpc.WithClientHandlerAlias("rand_theClientRandomAlias", "Client.DoubleOnClient"),
378+
)
379+
```
380+
381+
#### Usage of a struct tag to define method name alias
382+
383+
There are two cases where you can also use the `rpc_method` struct tag to define method name alias:
384+
in the client struct and in the reverse handler struct in the server.
385+
386+
In the client struct:
387+
```go
388+
// setup the client struct
389+
var client struct {
390+
AddInt func(int) int `rpc_method:"rand_aRandomAlias"`
391+
}
392+
393+
// create a new client instance with the client struct that has the `rpc_method` struct tag
394+
closer, err := jsonrpc.NewMergeClient(
395+
context.Background(),
396+
"http://example.com",
397+
"SimpleServerHandler",
398+
[]any{&client},
399+
nil,
400+
)
401+
402+
// since we defined the method name alias in the client struct, this will send a JSON-RPC request with "rand_aRandomAlias" as the method name to the
403+
// server instead of "SimpleServerHandler.AddInt".
404+
result, err := client.AddInt(10)
405+
406+
```
407+
408+
In the server's reverse handler struct:
409+
410+
```go
411+
// Define the client handler interface
412+
type ClientHandler struct {
413+
CallOnClient func(int) (int, error) `rpc_method:"rand_theClientRandomAlias"`
414+
}
415+
416+
// Define the server handler
417+
type ServerHandler struct {}
418+
419+
func (h *ServerHandler) Call(ctx context.Context) (int, error) {
420+
revClient, _ := jsonrpc.ExtractReverseClient[ClientHandler](ctx)
421+
422+
// Reverse call to the client.
423+
// Since we defined the method name alias in the client handler struct tag, this
424+
// will send a JSON-RPC request with "rand_theClientRandomAlias" as the method name to the
425+
// client instead of "Client.CallOnClient".
426+
result, err := revClient.CallOnClient(7)
427+
428+
// ...
429+
}
430+
431+
// Setup server with reverse client capability
432+
rpcServer := jsonrpc.NewServer(jsonrpc.WithReverseClient[ClientHandler]("Client"))
433+
rpcServer.Register("ServerHandler", &ServerHandler{})
434+
```
435+
331436

332437
## Contribute
333438

0 commit comments

Comments
 (0)