forked from StyraInc/regal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Language server refactoring (StyraInc#1349)
While there's more to do here, we've got to start somewhere. - Simplify handlers by extracting shared logic - Use "global" vars for static slices to avoid needless allocations - No named return values - Add GetContentAndModule convenience function to reduce boilerplate - Various fixes This should not change behavior of anything, just nake server.go a little more manageable. Signed-off-by: Anders Eknert <[email protected]>
- Loading branch information
1 parent
d7f522e
commit a5d1742
Showing
5 changed files
with
201 additions
and
377 deletions.
There are no files selected for viewing
This file contains 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 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 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,44 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/anderseknert/roast/pkg/encoding" | ||
"github.com/sourcegraph/jsonrpc2" | ||
) | ||
|
||
type handlerFunc[T any] func(T) (any, error) | ||
|
||
type handlerContextFunc[T any] func(context.Context, T) (any, error) | ||
|
||
var ErrInvalidParams = &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams} | ||
|
||
func Decode[T any](req *jsonrpc2.Request, params *T) error { | ||
if req.Params == nil { | ||
return ErrInvalidParams | ||
} | ||
|
||
if err := encoding.JSON().Unmarshal(*req.Params, ¶ms); err != nil { | ||
return &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams, Message: err.Error()} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func WithParams[T any](req *jsonrpc2.Request, h handlerFunc[T]) (any, error) { | ||
var params T | ||
if err := Decode(req, ¶ms); err != nil { | ||
return nil, err | ||
} | ||
|
||
return h(params) | ||
} | ||
|
||
func WithContextAndParams[T any](ctx context.Context, req *jsonrpc2.Request, h handlerContextFunc[T]) (any, error) { | ||
var params T | ||
if err := Decode(req, ¶ms); err != nil { | ||
return nil, err | ||
} | ||
|
||
return h(ctx, params) | ||
} |
Oops, something went wrong.