Skip to content
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(protocol): Add custom request handling #32

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tris203
Copy link

@tris203 tris203 commented Jun 28, 2024

This commit introduces the ability to handle custom requests in the protocol package. A new struct, CustomRequestHandler, has been added which contains a Method and a Func. The Func is a function that takes a context and params and returns an error.

The Handler struct now includes a slice of CustomRequestHandlers, and the Handle method has been updated to handle custom requests by unmarshalling the params into the handler's params and then calling the handler's Func.

A new method, FindCustomRequestHandler, has been added to the Handler struct. This method takes a method string and returns the corresponding CustomRequestHandler and a boolean indicating whether the handler was found.

The server's handle method has also been updated to handle errors from the Handler's Handle method.

Usage example:

	CustomRequests := []protocol.CustomRequestHandler{
		{
			Func:   server.TestHandler,
			Method: "test/test",
		},
	}

	handler = protocol.Handler{
		Initialize:            server.initialize,
		Initialized:           server.initialized,
		Shutdown:              server.shutdown,
		SetTrace:              server.setTrace,
		TextDocumentDidOpen:   server.didOpen,
		TextDocumentDidChange: server.didChange,
		TextDocumentDidClose:  server.didClose,
		CustomRequest:         CustomRequests,
	}

then server.TestHandler as below:

func (s *Server) TestHandler(context *glsp.Context, params json.RawMessage) error {
	type TestParams struct {
		Nah string `json:"nah"`
	}

	unmarshaledParams := TestParams{}
	unmarshalErr := json.Unmarshal(params, &unmarshaledParams)
	if unmarshalErr != nil {
		fmt.Println("Error unmarshaling params")
	}
	fmt.Println("TestHandler")
	fmt.Printf("Params: %v", unmarshaledParams.Nah)
	return nil
}

Closes #31

This commit introduces the ability to handle custom requests in the
protocol package. A new struct, CustomRequestHandler, has been added
which contains a Method and a Func. The Func is a function that takes
a context and params and returns an error.

The Handler struct now includes a slice of CustomRequestHandlers, and
the Handle method has been updated to handle custom requests by
unmarshalling the params into the handler's params and then calling
the handler's Func.

A new method, FindCustomRequestHandler, has been added to the Handler
struct. This method takes a method string and returns the corresponding
CustomRequestHandler and a boolean indicating whether the handler was
found.

The server's handle method has also been updated to handle errors from
the Handler's Handle method.
The return type of CustomRequestFunc in both protocol_3_16 and
protocol_3_17 has been changed from error to (any, error). This
allows the function to return a result alongside an error. The
Handler's Handle method has been updated to accommodate this
change.
@tris203
Copy link
Author

tris203 commented Jun 28, 2024

The only problem at the moment, is that because the params are private, it cause issues in the 317 library version as it gets confused over the version, but if i use the 316 version I cant access the params field
I have made the Params field public, although I dont like it. I cant think of a better solution.
Any suggestions on the most go like way of solving this?

@tris203
Copy link
Author

tris203 commented Jun 28, 2024

Also seeing this when there are custom routes implemented, I will investigate tomorrow

exit log ``` 2024/06/28 23:45:03 jsonrpc2 handler: notification "exit" handling error: server not initialized ```
this is due to now returning the error in server/handler.go as the exit notifcation comes after the shutdown and resolved in latest commits

The 'params' field in the CustomRequestHandler struct has been renamed
to 'Params' and made public. This change is reflected in the associated
methods where 'params' was used. The 'protocol_3_17/custom.go' file has
been deleted. The 'FindCustomRequestHandler' method
now returns a 'protocol316.CustomRequestHandler' instead of a
'CustomRequestHandler'.
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.

1 participant