- 
                Notifications
    You must be signed in to change notification settings 
- Fork 98
feat: method namer #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: method namer #130
Conversation
| On  
 | 
| Thanks @chris-4chain for your contribution. I'll let @rvagg chime in on next steps or assign another reviewer. | 
| Gah. Sorry, I saw and merged #131 first, suggesting your exact design. However, that PR doesn't implement the client side. Mind rebasing your PR? | 
add options to set custom namespace separator and a method name trans…
cb17873    to
    57d1697      
    Compare
  
    | @Stebalien No worries 👍 I'm glad to see this feature moving forward, regardless of who creates the PR 🙈 
 | 
        
          
                README.md
              
                Outdated
          
        
      | There are three predefined formatters: | ||
| - `jsonrpc.DefaultMethodNameFormatter` - default method name formatter, e.g. `SimpleServerHandler.AddGet` | ||
| - `jsonrpc.NoNamespaceMethodNameFormatter` - method name formatter without namespace, e.g. `AddGet` | ||
| - `jsonrpc.NoNamespaceDecapitalizedMethodNameFormatter` - method name formatter without namespace and decapitalized, e.g. `addGet` | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love this name, but NoNamespaceLowerCamelCaseMethodNameFormatter would be getting a bit out of hand I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ I've adjusted it according to your idea with NewMethodNameFormatter
        
          
                client.go
              
                Outdated
          
        
      | methodNameFormatter: config.methodNamer, | ||
| paramEncoders: config.paramEncoders, | ||
| errors: config.errors, | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| methodNameFormatter: config.methodNamer, | |
| paramEncoders: config.paramEncoders, | |
| errors: config.errors, | |
| paramEncoders: config.paramEncoders, | |
| errors: config.errors, | |
| methodNameFormatter: config.methodNamer, | 
retain ordering of the fields as defined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Done
        
          
                client.go
              
                Outdated
          
        
      | paramEncoders: config.paramEncoders, | ||
| errors: config.errors, | ||
| namespace: namespace, | ||
| methodNameFormatter: config.methodNamer, | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Done
        
          
                client.go
              
                Outdated
          
        
      | paramEncoders: config.paramEncoders, | ||
| errors: config.errors, | ||
| namespace: namespace, | ||
| methodNameFormatter: config.methodNamer, | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Done
        
          
                method_formatter.go
              
                Outdated
          
        
      | // DefaultMethodNameFormatter joins the namespace and method name with a dot. | ||
| func DefaultMethodNameFormatter(namespace, method string) string { | ||
| return namespace + "." + method | ||
| } | ||
|  | ||
| // NoNamespaceMethodNameFormatter returns the method name as is, without the namespace. | ||
| func NoNamespaceMethodNameFormatter(_, method string) string { | ||
| return method | ||
| } | ||
|  | ||
| // NoNamespaceDecapitalizedMethodNameFormatter returns the method name as is, without the namespace, and decapitalizes the first letter. | ||
| // e.g. "Inc" -> "inc" | ||
| func NoNamespaceDecapitalizedMethodNameFormatter(_, method string) string { | ||
| if len(method) == 0 { | ||
| return "" | ||
| } | ||
| return strings.ToLower(method[:1]) + method[1:] | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // DefaultMethodNameFormatter joins the namespace and method name with a dot. | |
| func DefaultMethodNameFormatter(namespace, method string) string { | |
| return namespace + "." + method | |
| } | |
| // NoNamespaceMethodNameFormatter returns the method name as is, without the namespace. | |
| func NoNamespaceMethodNameFormatter(_, method string) string { | |
| return method | |
| } | |
| // NoNamespaceDecapitalizedMethodNameFormatter returns the method name as is, without the namespace, and decapitalizes the first letter. | |
| // e.g. "Inc" -> "inc" | |
| func NoNamespaceDecapitalizedMethodNameFormatter(_, method string) string { | |
| if len(method) == 0 { | |
| return "" | |
| } | |
| return strings.ToLower(method[:1]) + method[1:] | |
| } | |
| // CaseStyle represents the case style for method names. | |
| type CaseStyle int | |
| const ( | |
| OriginalCase CaseStyle = iota | |
| LowerFirstCharCase | |
| ) | |
| // NewMethodNameFormatter creates a new method name formatter based on the provided options. | |
| func NewMethodNameFormatter(includeNamespace bool, nameCase CaseStyle) MethodNameFormatter { | |
| return func(namespace, method string) string { | |
| formattedMethod := method | |
| if nameCase == LowerFirstCharCase && len(method) > 0 { | |
| formattedMethod = strings.ToLower(method[:1]) + method[1:] | |
| } | |
| if includeNamespace { | |
| return namespace + "." + formattedMethod | |
| } | |
| return formattedMethod | |
| } | |
| } | |
| // DefaultMethodNameFormatter is a pass-through formatter with default options. | |
| var DefaultMethodNameFormatter = NewMethodNameFormatter(true, OriginalCase) | 
How about something like this to solve the weird naming problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea 💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great, thanks! sorry it's taken so long on our end
I'm proposing a
MethodNamercustom configuration option (for both client & server) to make it configurable what string is sent asmethodfield inJSON-RPC.Definition:
Example:
It solves issues: Add support for lowercase methods and Add support for no namespace
NoNamespaceMethodNamer- returns the method name as is, without the namespace.NoNamespaceDecapitalizedMethodNamer- returns the method name as is, without the namespace, and decapitalizes the first letter.