mirror of
https://github.com/golang/go
synced 2024-11-18 16:44:43 -07:00
434f7a8fef
The previous implementation was exposing the details of the wire format and resulted in non idomatic go, detecting the presence of absence of values in fields to deterimine the message type. Now the messages are distinct types and we use type switches instead. Request still exists as an interface to expose the shared behaviour of Call and Notification, as this is the type accepted by handlers. The set of messages is deliberately closed by using a private methods on the interfaces. Change-Id: I2cf15ee3923ef4688670c62896f81f760c77fe04 Reviewed-on: https://go-review.googlesource.com/c/tools/+/228719 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
81 lines
2.4 KiB
Go
81 lines
2.4 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package protocol
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
"golang.org/x/tools/internal/telemetry/event"
|
|
"golang.org/x/tools/internal/xcontext"
|
|
)
|
|
|
|
var (
|
|
// RequestCancelledError should be used when a request is cancelled early.
|
|
RequestCancelledError = jsonrpc2.NewError(-32800, "JSON RPC cancelled")
|
|
)
|
|
|
|
// ClientDispatcher returns a Client that dispatches LSP requests across the
|
|
// given jsonrpc2 connection.
|
|
func ClientDispatcher(conn *jsonrpc2.Conn) Client {
|
|
return &clientDispatcher{Conn: conn}
|
|
}
|
|
|
|
// ServerDispatcher returns a Server that dispatches LSP requests across the
|
|
// given jsonrpc2 connection.
|
|
func ServerDispatcher(conn *jsonrpc2.Conn) Server {
|
|
return &serverDispatcher{Conn: conn}
|
|
}
|
|
|
|
func Handlers(handler jsonrpc2.Handler) jsonrpc2.Handler {
|
|
return CancelHandler(
|
|
CancelHandler(
|
|
jsonrpc2.AsyncHandler(
|
|
jsonrpc2.MustReplyHandler(handler))))
|
|
}
|
|
|
|
func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler {
|
|
handler, canceller := jsonrpc2.CancelHandler(handler)
|
|
return func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
|
|
if req.Method() != "$/cancelRequest" {
|
|
return handler(ctx, reply, req)
|
|
}
|
|
var params CancelParams
|
|
if err := json.Unmarshal(req.Params(), ¶ms); err != nil {
|
|
return sendParseError(ctx, reply, err)
|
|
}
|
|
if n, ok := params.ID.(float64); ok {
|
|
canceller(*jsonrpc2.NewIntID(int64(n)))
|
|
} else if s, ok := params.ID.(string); ok {
|
|
canceller(*jsonrpc2.NewStringID(s))
|
|
} else {
|
|
return sendParseError(ctx, reply, fmt.Errorf("request ID %v malformed", params.ID))
|
|
}
|
|
return reply(ctx, nil, nil)
|
|
}
|
|
}
|
|
|
|
func Call(ctx context.Context, conn *jsonrpc2.Conn, method string, params interface{}, result interface{}) error {
|
|
id, err := conn.Call(ctx, method, params, result)
|
|
if ctx.Err() != nil {
|
|
cancelCall(ctx, conn, id)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func cancelCall(ctx context.Context, conn *jsonrpc2.Conn, id jsonrpc2.ID) {
|
|
ctx = xcontext.Detach(ctx)
|
|
ctx, done := event.StartSpan(ctx, "protocol.canceller")
|
|
defer done()
|
|
// Note that only *jsonrpc2.ID implements json.Marshaler.
|
|
conn.Notify(ctx, "$/cancelRequest", &CancelParams{ID: &id})
|
|
}
|
|
|
|
func sendParseError(ctx context.Context, reply jsonrpc2.Replier, err error) error {
|
|
return reply(ctx, nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err))
|
|
}
|