Skip to content

Conversation

@raul0ligma
Copy link
Contributor

@raul0ligma raul0ligma commented Mar 18, 2025

helps create eth_* friendly rpc methods by allowing custom options
example:

rpcServer := jsonrpc.NewServer(
		jsonrpc.WithNamespaceSeparator("_"),
		jsonrpc.WithMethodNameTransformer(func(s string) string {
			r := []rune(s)
			r[0] = unicode.ToLower(r[0])
			return string(r)
		}),
	)

	rpcServer.Register("eth", ethRpcService)	
        ...
        
        // this would become eth_blockNumber 
        (r *EthRpc) BlockNumber(ctx context.Context) 

@raul0ligma raul0ligma changed the title add options to set custom namespace separator and a method transformer add options to set custom namespace separator and a method name transformer Mar 18, 2025
Copy link
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a reasonable improvement. Thanks!

handler.go Outdated
Comment on lines 134 to 138
if s.methodNameTransformer != nil {
method.Name = s.methodNameTransformer(method.Name)
}

s.methods[namespace+"."+method.Name] = methodHandler{
s.methods[namespace+s.separator+method.Name] = methodHandler{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about letting the user control everything with a "method name formatter". I.e.:

  1. Define a WithMethodNameFormatter option that takes a func(namespace, method string) string.
  2. Default to func(namespace, method) string { return namespace + "." + name }.

That way the user can combine the method name and namespace in any way they want.

Also, please don't modify method.Name (it's probably safe, but I would avoid it).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E.g., in case the user wants something like method.namepace or /namespace/method etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

			handlerFunc: method.Func,
			receiver:    val,

			hasCtx:       hasCtx,
			hasRawParams: hasRawParams,

			errOut: errOut,
			valOut: valOut,
		}

add a formatter handler now and defaults to namespace.method

@raul0ligma raul0ligma requested a review from Stebalien March 21, 2025 17:02
Copy link
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Stebalien Stebalien merged commit 8e8f524 into filecoin-project:master Mar 21, 2025
7 checks passed
@Stebalien Stebalien mentioned this pull request Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants