Skip to content

Allow protocol extensions by exposing server.Notify and client.Handler #53

@qvalentin

Description

@qvalentin

Thanks for this awesome package.

One thing I'm currently missing is the ability to use protocol extensions.

  1. One can already call custom server methods with the Request(ctx context.Context, method string, params interface{}) (result interface{}, err error) function. But custom Notifications are not supported by the server interface.

  2. Also the client implementation (NewClient) does not allow adding a custom fallback handler but will just use jsonrpc2.MethodNotFoundHandler.

My current workaround is:

  1. Store the jsonrpc2.Conn when creating the server and call Notifiy directly on it.
  2. A custom NewClient method, that includes a custom handler:
// CustomNewClient returns the context in which Client is embedded, jsonrpc2.Conn, and the Server.
func (yamllsConnector Connector) CustomNewClient(ctx context.Context, client protocol.Client, stream jsonrpc2.Stream, logger *zap.Logger) (context.Context, jsonrpc2.Conn, protocol.Server) {
	ctx = protocol.WithClient(ctx, client)

	conn := jsonrpc2.NewConn(stream)
	conn.Go(ctx,
		protocol.Handlers(
			protocol.ClientHandler(client, yamllsConnector.CustomHandler),
		),
	)
	server := protocol.ServerDispatcher(conn, logger.Named("server"))

	return ctx, conn, server
}

func (yamllsConnector Connector) CustomHandler(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
	switch req.Method() {
	case "custom/schema/request":
		return reply(ctx, nil, nil)
	}

	return jsonrpc2.MethodNotFoundHandler(ctx, reply, req)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions