Skip to content

Commit c488852

Browse files
authored
Merge pull request #37 from etclabscore/feat/server-method-alias
jsonrpc: add RPCServer.AliasMethod method
2 parents f4b2d34 + 931e4b3 commit c488852

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

handler.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,16 @@ func (s *RPCServer) handle(ctx context.Context, req request, w func(func(io.Writ
194194

195195
handler, ok := s.methods[req.Method]
196196
if !ok {
197-
rpcError(w, &req, rpcMethodNotFound, fmt.Errorf("method '%s' not found", req.Method))
198-
stats.Record(ctx, metrics.RPCInvalidMethod.M(1))
199-
done(false)
200-
return
197+
aliasTo, ok := s.aliasedMethods[req.Method]
198+
if ok {
199+
handler, ok = s.methods[aliasTo]
200+
}
201+
if !ok {
202+
rpcError(w, &req, rpcMethodNotFound, fmt.Errorf("method '%s' not found", req.Method))
203+
stats.Record(ctx, metrics.RPCInvalidMethod.M(1))
204+
done(false)
205+
return
206+
}
201207
}
202208

203209
if len(req.Params) != handler.nParams {

server.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ const (
1919

2020
// RPCServer provides a jsonrpc 2.0 http server handler
2121
type RPCServer struct {
22-
methods map[string]rpcHandler
22+
methods map[string]rpcHandler
23+
24+
// aliasedMethods contains a map of alias:original method names.
25+
// These are used as fallbacks if a method is not found by the given method name.
26+
aliasedMethods map[string]string
2327

2428
paramDecoders map[reflect.Type]ParamDecoder
2529

@@ -35,6 +39,7 @@ func NewServer(opts ...ServerOption) *RPCServer {
3539

3640
return &RPCServer{
3741
methods: map[string]rpcHandler{},
42+
aliasedMethods: map[string]string{},
3843
paramDecoders: config.paramDecoders,
3944
maxRequestSize: config.maxRequestSize,
4045
}
@@ -123,4 +128,8 @@ func (s *RPCServer) Register(namespace string, handler interface{}) {
123128
s.register(namespace, handler)
124129
}
125130

131+
func (s *RPCServer) AliasMethod(alias, original string) {
132+
s.aliasedMethods[alias] = original
133+
}
134+
126135
var _ error = &respError{}

0 commit comments

Comments
 (0)