Skip to content

Commit ac8980c

Browse files
committed
gopls/internal/protocol: modernize to use any
Replace 'interface{}' by 'any' almost everywhere. And there's one slices.Clone() too. Change-Id: I156e3e026cd7f46aff35f5dfe2adee204a40c03a Reviewed-on: https://go-review.googlesource.com/c/tools/+/641055 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 2ad5c90 commit ac8980c

File tree

12 files changed

+153
-152
lines changed

12 files changed

+153
-152
lines changed

gopls/internal/protocol/command/command_gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopls/internal/protocol/command/gen/gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopls/internal/protocol/command/util.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (c Command) String() string { return string(c) }
2121
// Example usage:
2222
//
2323
// jsonArgs, err := MarshalArgs(1, "hello", true, StructuredArg{42, 12.6})
24-
func MarshalArgs(args ...interface{}) ([]json.RawMessage, error) {
24+
func MarshalArgs(args ...any) ([]json.RawMessage, error) {
2525
var out []json.RawMessage
2626
for _, arg := range args {
2727
argJSON, err := json.Marshal(arg)
@@ -34,7 +34,7 @@ func MarshalArgs(args ...interface{}) ([]json.RawMessage, error) {
3434
}
3535

3636
// MustMarshalArgs is like MarshalArgs, but panics on error.
37-
func MustMarshalArgs(args ...interface{}) []json.RawMessage {
37+
func MustMarshalArgs(args ...any) []json.RawMessage {
3838
msg, err := MarshalArgs(args...)
3939
if err != nil {
4040
panic(err)
@@ -54,7 +54,7 @@ func MustMarshalArgs(args ...interface{}) []json.RawMessage {
5454
// structured StructuredArg
5555
// )
5656
// err := UnmarshalArgs(args, &num, &str, &bul, &structured)
57-
func UnmarshalArgs(jsonArgs []json.RawMessage, args ...interface{}) error {
57+
func UnmarshalArgs(jsonArgs []json.RawMessage, args ...any) error {
5858
if len(args) != len(jsonArgs) {
5959
return fmt.Errorf("DecodeArgs: expected %d input arguments, got %d JSON arguments", len(args), len(jsonArgs))
6060
}

gopls/internal/protocol/generate/generate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func propStar(name string, t NameType, gotype string) (string, string) {
6464
star = "" // passed by reference, so no need for *
6565
} else {
6666
switch gotype {
67-
case "bool", "uint32", "int32", "string", "interface{}":
67+
case "bool", "uint32", "int32", "string", "interface{}", "any":
6868
star = "" // gopls compatibility if t.Optional
6969
}
7070
}

gopls/internal/protocol/generate/main_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestParseContents(t *testing.T) {
4040
if err != nil {
4141
t.Fatal(err)
4242
}
43-
var our interface{}
43+
var our any
4444
if err := json.Unmarshal(out, &our); err != nil {
4545
t.Fatal(err)
4646
}
@@ -50,7 +50,7 @@ func TestParseContents(t *testing.T) {
5050
if err != nil {
5151
t.Fatalf("could not read metaModel.json: %v", err)
5252
}
53-
var raw interface{}
53+
var raw any
5454
if err := json.Unmarshal(buf, &raw); err != nil {
5555
t.Fatal(err)
5656
}

gopls/internal/protocol/generate/output.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"fmt"
1010
"log"
11+
"slices"
1112
"sort"
1213
"strings"
1314
)
@@ -219,8 +220,8 @@ func genStructs(model *Model) {
219220
fmt.Fprintf(out, "//\n")
220221
out.WriteString(lspLink(model, camelCase(s.Name)))
221222
fmt.Fprintf(out, "type %s struct {%s\n", nm, linex(s.Line))
222-
// for gpls compatibilitye, embed most extensions, but expand the rest some day
223-
props := append([]NameType{}, s.Properties...)
223+
// for gopls compatibility, embed most extensions, but expand the rest some day
224+
props := slices.Clone(s.Properties)
224225
if s.Name == "SymbolInformation" { // but expand this one
225226
for _, ex := range s.Extends {
226227
fmt.Fprintf(out, "\t// extends %s\n", ex.Name)
@@ -242,7 +243,7 @@ func genStructs(model *Model) {
242243

243244
// base types
244245
// (For URI and DocumentURI, see ../uri.go.)
245-
types["LSPAny"] = "type LSPAny = interface{}\n"
246+
types["LSPAny"] = "type LSPAny = any\n"
246247
// A special case, the only previously existing Or type
247248
types["DocumentDiagnosticReport"] = "type DocumentDiagnosticReport = Or_DocumentDiagnosticReport // (alias) \n"
248249

@@ -318,7 +319,7 @@ func genGenTypes() {
318319
sort.Strings(names)
319320
fmt.Fprintf(out, "// created for Or %v\n", names)
320321
fmt.Fprintf(out, "type %s struct {%s\n", nm, linex(nt.line+1))
321-
fmt.Fprintf(out, "\tValue interface{} `json:\"value\"`\n")
322+
fmt.Fprintf(out, "\tValue any `json:\"value\"`\n")
322323
case "and":
323324
fmt.Fprintf(out, "// created for And\n")
324325
fmt.Fprintf(out, "type %s struct {%s\n", nm, linex(nt.line+1))

gopls/internal/protocol/generate/tables.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,32 @@ var usedGoplsStar = make(map[prop]bool)
5757

5858
// For gopls compatibility, use a different, typically more restrictive, type for some fields.
5959
var renameProp = map[prop]string{
60-
{"CancelParams", "id"}: "interface{}",
60+
{"CancelParams", "id"}: "any",
6161
{"Command", "arguments"}: "[]json.RawMessage",
6262
{"CodeAction", "data"}: "json.RawMessage", // delay unmarshalling commands
63-
{"Diagnostic", "code"}: "interface{}",
63+
{"Diagnostic", "code"}: "any",
6464
{"Diagnostic", "data"}: "json.RawMessage", // delay unmarshalling quickfixes
6565

66-
{"DocumentDiagnosticReportPartialResult", "relatedDocuments"}: "map[DocumentURI]interface{}",
66+
{"DocumentDiagnosticReportPartialResult", "relatedDocuments"}: "map[DocumentURI]any",
6767

6868
{"ExecuteCommandParams", "arguments"}: "[]json.RawMessage",
6969
{"FoldingRange", "kind"}: "string",
7070
{"Hover", "contents"}: "MarkupContent",
7171
{"InlayHint", "label"}: "[]InlayHintLabelPart",
7272

73-
{"RelatedFullDocumentDiagnosticReport", "relatedDocuments"}: "map[DocumentURI]interface{}",
74-
{"RelatedUnchangedDocumentDiagnosticReport", "relatedDocuments"}: "map[DocumentURI]interface{}",
73+
{"RelatedFullDocumentDiagnosticReport", "relatedDocuments"}: "map[DocumentURI]any",
74+
{"RelatedUnchangedDocumentDiagnosticReport", "relatedDocuments"}: "map[DocumentURI]any",
7575

7676
// PJW: this one is tricky.
77-
{"ServerCapabilities", "codeActionProvider"}: "interface{}",
77+
{"ServerCapabilities", "codeActionProvider"}: "any",
7878

79-
{"ServerCapabilities", "inlayHintProvider"}: "interface{}",
79+
{"ServerCapabilities", "inlayHintProvider"}: "any",
8080
// slightly tricky
81-
{"ServerCapabilities", "renameProvider"}: "interface{}",
81+
{"ServerCapabilities", "renameProvider"}: "any",
8282
// slightly tricky
83-
{"ServerCapabilities", "semanticTokensProvider"}: "interface{}",
83+
{"ServerCapabilities", "semanticTokensProvider"}: "any",
8484
// slightly tricky
85-
{"ServerCapabilities", "textDocumentSync"}: "interface{}",
85+
{"ServerCapabilities", "textDocumentSync"}: "any",
8686
{"TextDocumentSyncOptions", "save"}: "SaveOptions",
8787
{"WorkspaceEdit", "documentChanges"}: "[]DocumentChange",
8888
}
@@ -122,26 +122,26 @@ var goplsType = map[string]string{
122122
"ConfigurationParams": "ParamConfiguration",
123123
"DocumentUri": "DocumentURI",
124124
"InitializeParams": "ParamInitialize",
125-
"LSPAny": "interface{}",
125+
"LSPAny": "any",
126126

127127
"Lit_SemanticTokensOptions_range_Item1": "PRangeESemanticTokensOptions",
128128

129129
"Or_Declaration": "[]Location",
130130
"Or_DidChangeConfigurationRegistrationOptions_section": "OrPSection_workspace_didChangeConfiguration",
131131
"Or_InlayHintLabelPart_tooltip": "OrPTooltipPLabel",
132132
"Or_InlayHint_tooltip": "OrPTooltip_textDocument_inlayHint",
133-
"Or_LSPAny": "interface{}",
133+
"Or_LSPAny": "any",
134134

135135
"Or_ParameterInformation_documentation": "string",
136136
"Or_ParameterInformation_label": "string",
137137
"Or_PrepareRenameResult": "PrepareRenamePlaceholder",
138-
"Or_ProgressToken": "interface{}",
138+
"Or_ProgressToken": "any",
139139
"Or_Result_textDocument_completion": "CompletionList",
140140
"Or_Result_textDocument_declaration": "Or_textDocument_declaration",
141141
"Or_Result_textDocument_definition": "[]Location",
142-
"Or_Result_textDocument_documentSymbol": "[]interface{}",
142+
"Or_Result_textDocument_documentSymbol": "[]any",
143143
"Or_Result_textDocument_implementation": "[]Location",
144-
"Or_Result_textDocument_semanticTokens_full_delta": "interface{}",
144+
"Or_Result_textDocument_semanticTokens_full_delta": "any",
145145
"Or_Result_textDocument_typeDefinition": "[]Location",
146146
"Or_Result_workspace_symbol": "[]SymbolInformation",
147147
"Or_TextDocumentContentChangeEvent": "TextDocumentContentChangePartial",
@@ -152,7 +152,7 @@ var goplsType = map[string]string{
152152

153153
"Tuple_ParameterInformation_label_Item1": "UIntCommaUInt",
154154
"WorkspaceFoldersServerCapabilities": "WorkspaceFolders5Gn",
155-
"[]LSPAny": "[]interface{}",
155+
"[]LSPAny": "[]any",
156156

157157
"[]Or_Result_textDocument_codeAction_Item0_Elem": "[]CodeAction",
158158
"[]PreviousResultId": "[]PreviousResultID",

gopls/internal/protocol/mapper_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,9 @@ func getPrePost(content []byte, offset int) (string, string) {
318318
// -- these are the historical lsppos tests --
319319

320320
type testCase struct {
321-
content string // input text
322-
substrOrOffset interface{} // explicit integer offset, or a substring
323-
wantLine, wantChar int // expected LSP position information
321+
content string // input text
322+
substrOrOffset any // explicit integer offset, or a substring
323+
wantLine, wantChar int // expected LSP position information
324324
}
325325

326326
// offset returns the test case byte offset

gopls/internal/protocol/protocol.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ type ClientCloser interface {
3333
type connSender interface {
3434
io.Closer
3535

36-
Notify(ctx context.Context, method string, params interface{}) error
37-
Call(ctx context.Context, method string, params, result interface{}) error
36+
Notify(ctx context.Context, method string, params any) error
37+
Call(ctx context.Context, method string, params, result any) error
3838
}
3939

4040
type clientDispatcher struct {
@@ -59,11 +59,11 @@ func (c clientConn) Close() error {
5959
return c.conn.Close()
6060
}
6161

62-
func (c clientConn) Notify(ctx context.Context, method string, params interface{}) error {
62+
func (c clientConn) Notify(ctx context.Context, method string, params any) error {
6363
return c.conn.Notify(ctx, method, params)
6464
}
6565

66-
func (c clientConn) Call(ctx context.Context, method string, params interface{}, result interface{}) error {
66+
func (c clientConn) Call(ctx context.Context, method string, params any, result any) error {
6767
id, err := c.conn.Call(ctx, method, params, result)
6868
if ctx.Err() != nil {
6969
cancelCall(ctx, c, id)
@@ -83,11 +83,11 @@ func (c clientConnV2) Close() error {
8383
return c.conn.Close()
8484
}
8585

86-
func (c clientConnV2) Notify(ctx context.Context, method string, params interface{}) error {
86+
func (c clientConnV2) Notify(ctx context.Context, method string, params any) error {
8787
return c.conn.Notify(ctx, method, params)
8888
}
8989

90-
func (c clientConnV2) Call(ctx context.Context, method string, params interface{}, result interface{}) error {
90+
func (c clientConnV2) Call(ctx context.Context, method string, params any, result any) error {
9191
call := c.conn.Call(ctx, method, params)
9292
err := call.Await(ctx, result)
9393
if ctx.Err() != nil {
@@ -126,16 +126,16 @@ func ClientHandler(client Client, handler jsonrpc2.Handler) jsonrpc2.Handler {
126126
}
127127

128128
func ClientHandlerV2(client Client) jsonrpc2_v2.Handler {
129-
return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (interface{}, error) {
129+
return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (any, error) {
130130
if ctx.Err() != nil {
131131
return nil, RequestCancelledErrorV2
132132
}
133133
req1 := req2to1(req)
134134
var (
135-
result interface{}
135+
result any
136136
resErr error
137137
)
138-
replier := func(_ context.Context, res interface{}, err error) error {
138+
replier := func(_ context.Context, res any, err error) error {
139139
if err != nil {
140140
resErr = err
141141
return nil
@@ -166,16 +166,16 @@ func ServerHandler(server Server, handler jsonrpc2.Handler) jsonrpc2.Handler {
166166
}
167167

168168
func ServerHandlerV2(server Server) jsonrpc2_v2.Handler {
169-
return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (interface{}, error) {
169+
return jsonrpc2_v2.HandlerFunc(func(ctx context.Context, req *jsonrpc2_v2.Request) (any, error) {
170170
if ctx.Err() != nil {
171171
return nil, RequestCancelledErrorV2
172172
}
173173
req1 := req2to1(req)
174174
var (
175-
result interface{}
175+
result any
176176
resErr error
177177
)
178-
replier := func(_ context.Context, res interface{}, err error) error {
178+
replier := func(_ context.Context, res any, err error) error {
179179
if err != nil {
180180
resErr = err
181181
return nil
@@ -232,7 +232,7 @@ func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler {
232232
// be careful about racing between the two paths.
233233
// TODO(iancottrell): Add a test that watches the stream and verifies the response
234234
// for the cancelled request flows.
235-
replyWithDetachedContext := func(ctx context.Context, resp interface{}, err error) error {
235+
replyWithDetachedContext := func(ctx context.Context, resp any, err error) error {
236236
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#cancelRequest
237237
if ctx.Err() != nil && err == nil {
238238
err = RequestCancelledError
@@ -257,7 +257,7 @@ func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler {
257257
}
258258
}
259259

260-
func Call(ctx context.Context, conn jsonrpc2.Conn, method string, params interface{}, result interface{}) error {
260+
func Call(ctx context.Context, conn jsonrpc2.Conn, method string, params any, result any) error {
261261
id, err := conn.Call(ctx, method, params, result)
262262
if ctx.Err() != nil {
263263
cancelCall(ctx, clientConn{conn}, id)

gopls/internal/protocol/tsclient.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)