Skip to content

Commit

Permalink
internal/lsp: publish protocol.UnmarshalJSON
Browse files Browse the repository at this point in the history
Missing or null arguments can be expressed in a variety of ways,
so provide a helper function to consolidate the necessary checks.

Change-Id: I595f71a6485d84883667f3c6adf5a87c41fbd6aa
Reviewed-on: https://go-review.googlesource.com/c/tools/+/556695
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
suzmue committed Jan 25, 2024
1 parent 35d8b1b commit 3d49bd5
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 111 deletions.
15 changes: 6 additions & 9 deletions gopls/internal/cache/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,13 @@ func bundleQuickFixes(sd *Diagnostic) bool {
// BundledQuickFixes extracts any bundled codeActions from the
// diag.Data field.
func BundledQuickFixes(diag protocol.Diagnostic) []protocol.CodeAction {
// Clients may express "no fixes" in a variety of ways (#64503).
if diag.Data == nil ||
len(*diag.Data) == 0 ||
len(*diag.Data) == 4 && string(*diag.Data) == "null" {
return nil
}
var fix quickFixesJSON
if err := json.Unmarshal(*diag.Data, &fix); err != nil {
bug.Reportf("unmarshalling quick fix: %v", err)
return nil
if diag.Data != nil {
err := protocol.UnmarshalJSON(*diag.Data, &fix)
if err != nil {
bug.Reportf("unmarshalling quick fix: %v", err)
return nil
}
}

var actions []protocol.CodeAction
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/protocol/generate/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func genCase(method string, param, result *Type, dir string) {
nm = "ParamConfiguration" // gopls compatibility
}
fmt.Fprintf(out, "\t\tvar params %s\n", nm)
fmt.Fprintf(out, "\t\tif err := unmarshalParams(r.Params(), &params); err != nil {\n")
fmt.Fprintf(out, "\t\tif err := UnmarshalJSON(r.Params(), &params); err != nil {\n")
fmt.Fprintf(out, "\t\t\treturn true, sendParseError(ctx, reply, err)\n\t\t}\n")
p = ", &params"
}
Expand Down
16 changes: 7 additions & 9 deletions gopls/internal/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler {
return handler(ctx, replyWithDetachedContext, req)
}
var params CancelParams
if err := unmarshalParams(req.Params(), &params); err != nil {
if err := UnmarshalJSON(req.Params(), &params); err != nil {
return sendParseError(ctx, reply, err)
}
if n, ok := params.ID.(float64); ok {
Expand Down Expand Up @@ -271,16 +271,14 @@ func cancelCall(ctx context.Context, sender connSender, id jsonrpc2.ID) {
sender.Notify(ctx, "$/cancelRequest", &CancelParams{ID: &id})
}

// unmarshalParams unmarshals msg into the variable pointed to by
// params. In JSONRPC, request.params is optional, so msg may may be
// UnmarshalJSON unmarshals msg into the variable pointed to by
// params. In JSONRPC, optional messages may be
// "null", in which case it is a no-op.
func unmarshalParams(msg json.RawMessage, params any) error {
if len(msg) > 0 && !bytes.Equal(msg, []byte("null")) {
if err := json.Unmarshal(msg, params); err != nil {
return err
}
func UnmarshalJSON(msg json.RawMessage, v any) error {
if len(msg) == 0 || bytes.Equal(msg, []byte("null")) {
return nil
}
return nil
return json.Unmarshal(msg, v)
}

func sendParseError(ctx context.Context, reply jsonrpc2.Replier, err error) error {
Expand Down
26 changes: 13 additions & 13 deletions gopls/internal/protocol/tsclient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3d49bd5

Please sign in to comment.