Skip to content

Commit 3d49bd5

Browse files
committed
internal/lsp: publish protocol.UnmarshalJSON
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 <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 35d8b1b commit 3d49bd5

File tree

6 files changed

+103
-111
lines changed

6 files changed

+103
-111
lines changed

gopls/internal/cache/diagnostics.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,13 @@ func bundleQuickFixes(sd *Diagnostic) bool {
164164
// BundledQuickFixes extracts any bundled codeActions from the
165165
// diag.Data field.
166166
func BundledQuickFixes(diag protocol.Diagnostic) []protocol.CodeAction {
167-
// Clients may express "no fixes" in a variety of ways (#64503).
168-
if diag.Data == nil ||
169-
len(*diag.Data) == 0 ||
170-
len(*diag.Data) == 4 && string(*diag.Data) == "null" {
171-
return nil
172-
}
173167
var fix quickFixesJSON
174-
if err := json.Unmarshal(*diag.Data, &fix); err != nil {
175-
bug.Reportf("unmarshalling quick fix: %v", err)
176-
return nil
168+
if diag.Data != nil {
169+
err := protocol.UnmarshalJSON(*diag.Data, &fix)
170+
if err != nil {
171+
bug.Reportf("unmarshalling quick fix: %v", err)
172+
return nil
173+
}
177174
}
178175

179176
var actions []protocol.CodeAction

gopls/internal/protocol/generate/output.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func genCase(method string, param, result *Type, dir string) {
101101
nm = "ParamConfiguration" // gopls compatibility
102102
}
103103
fmt.Fprintf(out, "\t\tvar params %s\n", nm)
104-
fmt.Fprintf(out, "\t\tif err := unmarshalParams(r.Params(), &params); err != nil {\n")
104+
fmt.Fprintf(out, "\t\tif err := UnmarshalJSON(r.Params(), &params); err != nil {\n")
105105
fmt.Fprintf(out, "\t\t\treturn true, sendParseError(ctx, reply, err)\n\t\t}\n")
106106
p = ", &params"
107107
}

gopls/internal/protocol/protocol.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler {
241241
return handler(ctx, replyWithDetachedContext, req)
242242
}
243243
var params CancelParams
244-
if err := unmarshalParams(req.Params(), &params); err != nil {
244+
if err := UnmarshalJSON(req.Params(), &params); err != nil {
245245
return sendParseError(ctx, reply, err)
246246
}
247247
if n, ok := params.ID.(float64); ok {
@@ -271,16 +271,14 @@ func cancelCall(ctx context.Context, sender connSender, id jsonrpc2.ID) {
271271
sender.Notify(ctx, "$/cancelRequest", &CancelParams{ID: &id})
272272
}
273273

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

286284
func sendParseError(ctx context.Context, reply jsonrpc2.Replier, err error) error {

gopls/internal/protocol/tsclient.go

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)