-
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
Merged
Merged
feat: method namer #130
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
15eee17
Merge pull request #1 from filecoin-project/master
chris-4chain fe5d436
feat: method namer
chris-4chain 57d1697
feat: adjust to MethodNameFormatter concept
chris-4chain a86ba49
feat: NewMethodNameFormatter concept
chris-4chain 19da84b
feat: client instantiation ordering
chris-4chain e72f58d
Update README.md
rvagg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package jsonrpc | ||
|
|
||
| import "strings" | ||
|
|
||
| // MethodNameFormatter is a function that takes a namespace and a method name and returns the full method name, sent via JSON-RPC. | ||
| // This is useful if you want to customize the default behaviour, e.g. send without the namespace or make it lowercase. | ||
| type MethodNameFormatter func(namespace, method string) string | ||
|
|
||
| // 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package jsonrpc | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "github.com/stretchr/testify/require" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestDifferentMethodNamers(t *testing.T) { | ||
| tests := map[string]struct { | ||
| namer MethodNameFormatter | ||
|
|
||
| requestedMethod string | ||
| }{ | ||
| "default namer": { | ||
| namer: DefaultMethodNameFormatter, | ||
| requestedMethod: "SimpleServerHandler.Inc", | ||
| }, | ||
| "lower fist char": { | ||
| namer: NewMethodNameFormatter(true, LowerFirstCharCase), | ||
| requestedMethod: "SimpleServerHandler.inc", | ||
| }, | ||
| "no namespace namer": { | ||
| namer: NewMethodNameFormatter(false, OriginalCase), | ||
| requestedMethod: "Inc", | ||
| }, | ||
| "no namespace & lower fist char": { | ||
| namer: NewMethodNameFormatter(false, LowerFirstCharCase), | ||
| requestedMethod: "inc", | ||
| }, | ||
| } | ||
| for name, test := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| rpcServer := NewServer(WithServerMethodNameFormatter(test.namer)) | ||
|
|
||
| serverHandler := &SimpleServerHandler{} | ||
| rpcServer.Register("SimpleServerHandler", serverHandler) | ||
|
|
||
| testServ := httptest.NewServer(rpcServer) | ||
| defer testServ.Close() | ||
|
|
||
| req := fmt.Sprintf(`{"jsonrpc": "2.0", "method": "%s", "params": [], "id": 1}`, test.requestedMethod) | ||
|
|
||
| res, err := http.Post(testServ.URL, "application/json", strings.NewReader(req)) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, http.StatusOK, res.StatusCode) | ||
| require.Equal(t, int32(1), serverHandler.n) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDifferentMethodNamersWithClient(t *testing.T) { | ||
| tests := map[string]struct { | ||
| namer MethodNameFormatter | ||
| urlPrefix string | ||
| }{ | ||
| "default namer & http": { | ||
| namer: DefaultMethodNameFormatter, | ||
| urlPrefix: "http://", | ||
| }, | ||
| "default namer & ws": { | ||
| namer: DefaultMethodNameFormatter, | ||
| urlPrefix: "ws://", | ||
| }, | ||
| "lower first char namer & http": { | ||
| namer: NewMethodNameFormatter(true, LowerFirstCharCase), | ||
| urlPrefix: "http://", | ||
| }, | ||
| "lower first char namer & ws": { | ||
| namer: NewMethodNameFormatter(true, LowerFirstCharCase), | ||
| urlPrefix: "ws://", | ||
| }, | ||
| "no namespace namer & http": { | ||
| namer: NewMethodNameFormatter(false, OriginalCase), | ||
| urlPrefix: "http://", | ||
| }, | ||
| "no namespace namer & ws": { | ||
| namer: NewMethodNameFormatter(false, OriginalCase), | ||
| urlPrefix: "ws://", | ||
| }, | ||
| "no namespace & lower first char & http": { | ||
| namer: NewMethodNameFormatter(false, LowerFirstCharCase), | ||
| urlPrefix: "http://", | ||
| }, | ||
| "no namespace & lower first char & ws": { | ||
| namer: NewMethodNameFormatter(false, LowerFirstCharCase), | ||
| urlPrefix: "ws://", | ||
| }, | ||
| } | ||
| for name, test := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| rpcServer := NewServer(WithServerMethodNameFormatter(test.namer)) | ||
|
|
||
| serverHandler := &SimpleServerHandler{} | ||
| rpcServer.Register("SimpleServerHandler", serverHandler) | ||
|
|
||
| testServ := httptest.NewServer(rpcServer) | ||
| defer testServ.Close() | ||
|
|
||
| var client struct { | ||
| AddGet func(int) int | ||
| } | ||
|
|
||
| closer, err := NewMergeClient( | ||
| context.Background(), | ||
| test.urlPrefix+testServ.Listener.Addr().String(), | ||
| "SimpleServerHandler", | ||
| []any{&client}, | ||
| nil, | ||
| WithHTTPClient(testServ.Client()), | ||
| WithMethodNameFormatter(test.namer), | ||
| ) | ||
| require.NoError(t, err) | ||
| defer closer() | ||
|
|
||
| n := client.AddGet(123) | ||
| require.Equal(t, 123, n) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.