Skip to content

Commit c071fd8

Browse files
authored
Merge pull request #26 from go-language-server/categorize-and-doc
Categorize and doc
2 parents 0aebfda + 7751c71 commit c071fd8

File tree

94 files changed

+12977
-11527
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+12977
-11527
lines changed

.golangci.yml

+135-90
Large diffs are not rendered by default.

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ tools/bin/%: ${TOOLS_DIR}/go.mod ${TOOLS_DIR}/go.sum
120120
for t in ${TOOLS}; do \
121121
if [ -z '$*' ] || [ $$(basename $$t) = '$*' ]; then \
122122
echo "Install $$t ..." >&2; \
123-
GOBIN=${TOOLS_BIN} CGO_ENABLED=0 go install -v -mod=mod ${GO_FLAGS} "$${t}"; \
123+
GOBIN=${TOOLS_BIN} CGO_ENABLED=0 go install -mod=mod ${GO_FLAGS} "$${t}"; \
124124
fi \
125125
done
126126

base.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-FileCopyrightText: Copyright 2021 The Go Language Server Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
package protocol
5+
6+
// CancelParams params of cancelRequest.
7+
type CancelParams struct {
8+
// ID is the request id to cancel.
9+
ID interface{} `json:"id"` // int32 | string
10+
}
11+
12+
// ProgressParams params of Progress netification.
13+
//
14+
// @since 3.15.0.
15+
type ProgressParams struct {
16+
// Token is the progress token provided by the client or server.
17+
Token ProgressToken `json:"token"`
18+
19+
// Value is the progress data.
20+
Value interface{} `json:"value"`
21+
}

base_gojay.go

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// SPDX-FileCopyrightText: Copyright 2021 The Go Language Server Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build gojay
5+
// +build gojay
6+
7+
package protocol
8+
9+
import (
10+
"github.com/francoispqt/gojay"
11+
)
12+
13+
// MarshalJSONObject implements gojay.MarshalerJSONObject.
14+
func (v *CancelParams) MarshalJSONObject(enc *gojay.Encoder) {
15+
enc.AddInterfaceKey(keyID, v.ID)
16+
}
17+
18+
// IsNil implements gojay.MarshalerJSONObject.
19+
func (v *CancelParams) IsNil() bool { return v == nil }
20+
21+
// UnmarshalJSONObject implements gojay.UnmarshalerJSONObject.
22+
func (v *CancelParams) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
23+
if k == keyID {
24+
return dec.Interface(&v.ID)
25+
}
26+
return nil
27+
}
28+
29+
// NKeys implements gojay.UnmarshalerJSONObject.
30+
func (v *CancelParams) NKeys() int { return 1 }
31+
32+
// compile time check whether the CancelParams implements a gojay.MarshalerJSONObject and gojay.UnmarshalerJSONObject interfaces.
33+
var (
34+
_ gojay.MarshalerJSONObject = (*CancelParams)(nil)
35+
_ gojay.UnmarshalerJSONObject = (*CancelParams)(nil)
36+
)
37+
38+
// ProgressToken is the progress token provided by the client or server.
39+
//
40+
// @since 3.15.0.
41+
type ProgressToken interface{}
42+
43+
// NewProgressToken returns a new ProgressToken.
44+
//nolint:gocritic
45+
func NewProgressToken(s string) *ProgressToken {
46+
var iface interface{} = s
47+
return (*ProgressToken)(&iface)
48+
}
49+
50+
// NewNumberProgressToken returns a new number ProgressToken.
51+
//nolint:gocritic
52+
func NewNumberProgressToken(n int32) *ProgressToken {
53+
var iface interface{} = n
54+
return (*ProgressToken)(&iface)
55+
}
56+
57+
//nolint:gocritic
58+
func encodeProgressToken(enc *gojay.Encoder, key string, v *ProgressToken) {
59+
if v == nil {
60+
return
61+
}
62+
enc.AddInterfaceKey(key, (interface{})(*v))
63+
}
64+
65+
//nolint:gocritic
66+
func decodeProgressToken(dec *gojay.Decoder, k, key string, v *ProgressToken) error {
67+
if v == nil || k != key {
68+
return nil
69+
}
70+
return dec.Interface((*interface{})(v))
71+
}
72+
73+
// MarshalJSONObject implements gojay.MarshalerJSONObject.
74+
func (v *ProgressParams) MarshalJSONObject(enc *gojay.Encoder) {
75+
encodeProgressToken(enc, keyToken, &v.Token)
76+
enc.AddInterfaceKey(keyValue, v.Value)
77+
}
78+
79+
// IsNil implements gojay.MarshalerJSONObject.
80+
func (v *ProgressParams) IsNil() bool { return v == nil }
81+
82+
// UnmarshalJSONObject implements gojay.UnmarshalerJSONObject.
83+
func (v *ProgressParams) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
84+
switch k {
85+
case keyToken:
86+
return decodeProgressToken(dec, k, keyToken, &v.Token)
87+
case keyValue:
88+
return dec.Interface(&v.Value)
89+
}
90+
return nil
91+
}
92+
93+
// NKeys implements gojay.UnmarshalerJSONObject.
94+
func (v *ProgressParams) NKeys() int { return 2 }
95+
96+
// compile time check whether the ProgressParams implements a gojay.MarshalerJSONObject and gojay.UnmarshalerJSONObject interfaces.
97+
var (
98+
_ gojay.MarshalerJSONObject = (*ProgressParams)(nil)
99+
_ gojay.UnmarshalerJSONObject = (*ProgressParams)(nil)
100+
)

base_gojay_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-FileCopyrightText: Copyright 2021 The Go Language Server Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build gojay
5+
// +build gojay
6+
7+
package protocol
8+
9+
import (
10+
"testing"
11+
12+
"github.com/francoispqt/gojay"
13+
)
14+
15+
func TestCancelParams(t *testing.T) {
16+
t.Parallel()
17+
18+
testCancelParams(t, gojay.Marshal, gojay.Unsafe.Unmarshal)
19+
}
20+
21+
func TestProgressParams(t *testing.T) {
22+
t.Parallel()
23+
24+
testProgressParams(t, gojay.Marshal, gojay.Unsafe.Unmarshal)
25+
}

progress_json.go renamed to base_json.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// Copyright 2021 The Go Language Server Authors. All rights reserved.
2-
// Use of this source code is governed by a BSD-style
3-
// license that can be found in the LICENSE file.
1+
// SPDX-FileCopyrightText: Copyright 2021 The Go Language Server Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
43

54
//go:build !gojay
65
// +build !gojay
@@ -67,6 +66,7 @@ func (v *ProgressToken) MarshalJSON() ([]byte, error) {
6766
if v.name != "" {
6867
return json.Marshal(v.name)
6968
}
69+
7070
return json.Marshal(v.number)
7171
}
7272

@@ -76,5 +76,6 @@ func (v *ProgressToken) UnmarshalJSON(data []byte) error {
7676
if err := json.Unmarshal(data, &v.number); err == nil {
7777
return nil
7878
}
79+
7980
return json.Unmarshal(data, &v.name)
8081
}

base_json_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-FileCopyrightText: Copyright 2021 The Go Language Server Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build !gojay
5+
// +build !gojay
6+
7+
package protocol
8+
9+
import (
10+
"encoding/json"
11+
"testing"
12+
)
13+
14+
func TestCancelParams(t *testing.T) {
15+
t.Parallel()
16+
17+
testCancelParams(t, json.Marshal, json.Unmarshal)
18+
}
19+
20+
func TestProgressParams(t *testing.T) {
21+
t.Parallel()
22+
23+
testProgressParams(t, json.Marshal, json.Unmarshal)
24+
}

base_test.go

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// SPDX-FileCopyrightText: Copyright 2021 The Go Language Server Authors
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
package protocol
5+
6+
import (
7+
"fmt"
8+
"reflect"
9+
"testing"
10+
11+
"github.com/google/go-cmp/cmp"
12+
"github.com/google/go-cmp/cmp/cmpopts"
13+
)
14+
15+
func testCancelParams(t *testing.T, marshal marshalFunc, unmarshal unmarshalFunc) {
16+
const want = `{"id":"testID"}`
17+
wantType := CancelParams{
18+
ID: "testID",
19+
}
20+
21+
t.Run("Marshal", func(t *testing.T) {
22+
t.Parallel()
23+
24+
tests := []struct {
25+
name string
26+
field CancelParams
27+
want string
28+
wantMarshalErr bool
29+
wantErr bool
30+
}{
31+
{
32+
name: "Valid",
33+
field: wantType,
34+
want: want,
35+
wantMarshalErr: false,
36+
wantErr: false,
37+
},
38+
}
39+
40+
for _, tt := range tests {
41+
tt := tt
42+
t.Run(tt.name, func(t *testing.T) {
43+
t.Parallel()
44+
45+
got, err := marshal(&tt.field)
46+
if (err != nil) != tt.wantMarshalErr {
47+
t.Fatal(err)
48+
}
49+
50+
if diff := cmp.Diff(tt.want, string(got)); (diff != "") != tt.wantErr {
51+
t.Errorf("%s: wantErr: %t\n(-want +got)\n%s", tt.name, tt.wantErr, diff)
52+
}
53+
})
54+
}
55+
})
56+
57+
t.Run("Unmarshal", func(t *testing.T) {
58+
t.Parallel()
59+
60+
tests := []struct {
61+
name string
62+
field string
63+
want CancelParams
64+
wantUnmarshalErr bool
65+
wantErr bool
66+
}{
67+
{
68+
name: "Valid",
69+
field: want,
70+
want: wantType,
71+
wantUnmarshalErr: false,
72+
wantErr: false,
73+
},
74+
}
75+
76+
for _, tt := range tests {
77+
tt := tt
78+
t.Run(tt.name, func(t *testing.T) {
79+
t.Parallel()
80+
81+
var got CancelParams
82+
if err := unmarshal([]byte(tt.field), &got); (err != nil) != tt.wantUnmarshalErr {
83+
t.Fatal(err)
84+
}
85+
86+
if diff := cmp.Diff(tt.want, got); (diff != "") != tt.wantErr {
87+
t.Errorf("%s: wantErr: %t\n(-want +got)\n%s", tt.name, tt.wantErr, diff)
88+
}
89+
})
90+
}
91+
})
92+
}
93+
94+
func testProgressParams(t *testing.T, marshal marshalFunc, unmarshal unmarshalFunc) {
95+
const wantWorkDoneToken = "156edea9-9d8d-422f-b7ee-81a84594afbb"
96+
const want = `{"token":"` + wantWorkDoneToken + `","value":"testValue"}`
97+
98+
token := NewProgressToken(wantWorkDoneToken)
99+
wantType := ProgressParams{
100+
Token: *token,
101+
Value: "testValue",
102+
}
103+
104+
t.Run("Marshal", func(t *testing.T) {
105+
t.Parallel()
106+
107+
tests := []struct {
108+
name string
109+
field ProgressParams
110+
want string
111+
wantMarshalErr bool
112+
wantErr bool
113+
}{
114+
{
115+
name: "Valid",
116+
field: wantType,
117+
want: want,
118+
wantMarshalErr: false,
119+
wantErr: false,
120+
},
121+
}
122+
123+
for _, tt := range tests {
124+
tt := tt
125+
t.Run(tt.name, func(t *testing.T) {
126+
t.Parallel()
127+
128+
got, err := marshal(&tt.field)
129+
if (err != nil) != tt.wantMarshalErr {
130+
t.Fatal(err)
131+
}
132+
133+
if diff := cmp.Diff(tt.want, string(got)); (diff != "") != tt.wantErr {
134+
t.Errorf("%s: wantErr: %t\n(-want +got)\n%s", tt.name, tt.wantErr, diff)
135+
}
136+
})
137+
}
138+
})
139+
140+
t.Run("Unmarshal", func(t *testing.T) {
141+
t.Parallel()
142+
143+
tests := []struct {
144+
name string
145+
field string
146+
want ProgressParams
147+
wantUnmarshalErr bool
148+
wantErr bool
149+
}{
150+
{
151+
name: "Valid",
152+
field: want,
153+
want: wantType,
154+
wantUnmarshalErr: false,
155+
wantErr: false,
156+
},
157+
}
158+
159+
for _, tt := range tests {
160+
tt := tt
161+
t.Run(tt.name, func(t *testing.T) {
162+
t.Parallel()
163+
164+
var got ProgressParams
165+
if err := unmarshal([]byte(tt.field), &got); (err != nil) != tt.wantUnmarshalErr {
166+
t.Fatal(err)
167+
}
168+
169+
if diff := cmp.Diff(tt.want, got, cmpopts.IgnoreFields(ProgressParams{}, "Token")); (diff != "") != tt.wantErr {
170+
t.Errorf("%s: wantErr: %t\n(-want +got)\n%s", tt.name, tt.wantErr, diff)
171+
}
172+
173+
if token := got.Token; !reflect.ValueOf(token).IsZero() {
174+
if diff := cmp.Diff(fmt.Sprint(token), wantWorkDoneToken); (diff != "") != tt.wantErr {
175+
t.Errorf("%s: wantErr: %t\n(-want +got)\n%s", tt.name, tt.wantErr, diff)
176+
}
177+
}
178+
})
179+
}
180+
})
181+
}

0 commit comments

Comments
 (0)