Skip to content

Commit a86ba49

Browse files
committed
feat: NewMethodNameFormatter concept
1 parent 57d1697 commit a86ba49

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,11 @@ if err := client.Call(); err != nil {
250250

251251
`WithServerMethodNameFormatter` allows you to customize a function that formats the JSON-RPC method name, given namespace and method name.
252252

253-
There are three predefined formatters:
253+
There are four predefined options:
254254
- `jsonrpc.DefaultMethodNameFormatter` - default method name formatter, e.g. `SimpleServerHandler.AddGet`
255-
- `jsonrpc.NoNamespaceMethodNameFormatter` - method name formatter without namespace, e.g. `AddGet`
256-
- `jsonrpc.NoNamespaceDecapitalizedMethodNameFormatter` - method name formatter without namespace and decapitalized, e.g. `addGet`
255+
- `jsonrpc.NewMethodNameFormatter(true, jsonrpc.LowerFirstCharCase)` - method name formatter with namespace, e.g. `SimpleServerHandler.addGet`
256+
- `jsonrpc.NewMethodNameFormatter(false, jsonrpc.OriginalCase)` - method name formatter without namespace, e.g. `AddGet`
257+
- `jsonrpc.NewMethodNameFormatter(false, jsonrpc.LowerFirstCharCase)` - method name formatter without namespace and with the first char lowercased, e.g. `addGet`
257258

258259
> [!NOTE]
259260
> The default method name formatter concatenates the namespace and method name with a dot.
@@ -297,7 +298,7 @@ func main() {
297298
"SimpleServerHandler",
298299
[]any{&client},
299300
nil,
300-
WithMethodNameFormatter(test.namer),
301+
WithMethodNameFormatter(jsonrpc.NewMethodNameFormatter(false, OriginalCase)),
301302
)
302303
defer closer()
303304
}

method_formatter.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,27 @@ import "strings"
66
// This is useful if you want to customize the default behaviour, e.g. send without the namespace or make it lowercase.
77
type MethodNameFormatter func(namespace, method string) string
88

9-
// DefaultMethodNameFormatter joins the namespace and method name with a dot.
10-
func DefaultMethodNameFormatter(namespace, method string) string {
11-
return namespace + "." + method
12-
}
9+
// CaseStyle represents the case style for method names.
10+
type CaseStyle int
1311

14-
// NoNamespaceMethodNameFormatter returns the method name as is, without the namespace.
15-
func NoNamespaceMethodNameFormatter(_, method string) string {
16-
return method
17-
}
12+
const (
13+
OriginalCase CaseStyle = iota
14+
LowerFirstCharCase
15+
)
1816

19-
// NoNamespaceDecapitalizedMethodNameFormatter returns the method name as is, without the namespace, and decapitalizes the first letter.
20-
// e.g. "Inc" -> "inc"
21-
func NoNamespaceDecapitalizedMethodNameFormatter(_, method string) string {
22-
if len(method) == 0 {
23-
return ""
17+
// NewMethodNameFormatter creates a new method name formatter based on the provided options.
18+
func NewMethodNameFormatter(includeNamespace bool, nameCase CaseStyle) MethodNameFormatter {
19+
return func(namespace, method string) string {
20+
formattedMethod := method
21+
if nameCase == LowerFirstCharCase && len(method) > 0 {
22+
formattedMethod = strings.ToLower(method[:1]) + method[1:]
23+
}
24+
if includeNamespace {
25+
return namespace + "." + formattedMethod
26+
}
27+
return formattedMethod
2428
}
25-
return strings.ToLower(method[:1]) + method[1:]
2629
}
30+
31+
// DefaultMethodNameFormatter is a pass-through formatter with default options.
32+
var DefaultMethodNameFormatter = NewMethodNameFormatter(true, OriginalCase)

method_formatter_test.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ func TestDifferentMethodNamers(t *testing.T) {
2020
namer: DefaultMethodNameFormatter,
2121
requestedMethod: "SimpleServerHandler.Inc",
2222
},
23+
"lower fist char": {
24+
namer: NewMethodNameFormatter(true, LowerFirstCharCase),
25+
requestedMethod: "SimpleServerHandler.inc",
26+
},
2327
"no namespace namer": {
24-
namer: NoNamespaceMethodNameFormatter,
28+
namer: NewMethodNameFormatter(false, OriginalCase),
2529
requestedMethod: "Inc",
2630
},
27-
"no namespace & decapitalized namer": {
28-
namer: NoNamespaceDecapitalizedMethodNameFormatter,
31+
"no namespace & lower fist char": {
32+
namer: NewMethodNameFormatter(false, LowerFirstCharCase),
2933
requestedMethod: "inc",
3034
},
3135
}
@@ -63,20 +67,28 @@ func TestDifferentMethodNamersWithClient(t *testing.T) {
6367
namer: DefaultMethodNameFormatter,
6468
urlPrefix: "ws://",
6569
},
70+
"lower first char namer & http": {
71+
namer: NewMethodNameFormatter(true, LowerFirstCharCase),
72+
urlPrefix: "http://",
73+
},
74+
"lower first char namer & ws": {
75+
namer: NewMethodNameFormatter(true, LowerFirstCharCase),
76+
urlPrefix: "ws://",
77+
},
6678
"no namespace namer & http": {
67-
namer: NoNamespaceMethodNameFormatter,
79+
namer: NewMethodNameFormatter(false, OriginalCase),
6880
urlPrefix: "http://",
6981
},
7082
"no namespace namer & ws": {
71-
namer: NoNamespaceMethodNameFormatter,
83+
namer: NewMethodNameFormatter(false, OriginalCase),
7284
urlPrefix: "ws://",
7385
},
74-
"no namespace & decapitalized namer & http": {
75-
namer: NoNamespaceDecapitalizedMethodNameFormatter,
86+
"no namespace & lower first char & http": {
87+
namer: NewMethodNameFormatter(false, LowerFirstCharCase),
7688
urlPrefix: "http://",
7789
},
78-
"no namespace & decapitalized namer & ws": {
79-
namer: NoNamespaceDecapitalizedMethodNameFormatter,
90+
"no namespace & lower first char & ws": {
91+
namer: NewMethodNameFormatter(false, LowerFirstCharCase),
8092
urlPrefix: "ws://",
8193
},
8294
}

0 commit comments

Comments
 (0)