-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
base.go
96 lines (79 loc) · 2.26 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"fmt"
"github.com/segmentio/encoding/json"
)
// CancelParams params of cancelRequest.
type CancelParams struct {
// ID is the request id to cancel.
ID interface{} `json:"id"` // int32 | string
}
// ProgressParams params of Progress netification.
//
// @since 3.15.0.
type ProgressParams struct {
// Token is the progress token provided by the client or server.
Token ProgressToken `json:"token"`
// Value is the progress data.
Value interface{} `json:"value"`
}
// ProgressToken is the progress token provided by the client or server.
//
// @since 3.15.0.
type ProgressToken struct {
name string
number int32
}
// compile time check whether the ProgressToken implements a fmt.Formatter, fmt.Stringer, json.Marshaler and json.Unmarshaler interfaces.
var (
_ fmt.Formatter = (*ProgressToken)(nil)
_ fmt.Stringer = (*ProgressToken)(nil)
_ json.Marshaler = (*ProgressToken)(nil)
_ json.Unmarshaler = (*ProgressToken)(nil)
)
// NewProgressToken returns a new ProgressToken.
func NewProgressToken(s string) *ProgressToken {
return &ProgressToken{name: s}
}
// NewNumberProgressToken returns a new number ProgressToken.
func NewNumberProgressToken(n int32) *ProgressToken {
return &ProgressToken{number: n}
}
// Format writes the ProgressToken to the formatter.
//
// If the rune is q the representation is non ambiguous,
// string forms are quoted.
func (v ProgressToken) Format(f fmt.State, r rune) {
const numF = `%d`
strF := `%s`
if r == 'q' {
strF = `%q`
}
switch {
case v.name != "":
fmt.Fprintf(f, strF, v.name)
default:
fmt.Fprintf(f, numF, v.number)
}
}
// String returns a string representation of the ProgressToken.
func (v ProgressToken) String() string {
return fmt.Sprint(v) //nolint:gocritic
}
// MarshalJSON implements json.Marshaler.
func (v *ProgressToken) MarshalJSON() ([]byte, error) {
if v.name != "" {
return json.Marshal(v.name)
}
return json.Marshal(v.number)
}
// UnmarshalJSON implements json.Unmarshaler.
func (v *ProgressToken) UnmarshalJSON(data []byte) error {
*v = ProgressToken{}
if err := json.Unmarshal(data, &v.number); err == nil {
return nil
}
return json.Unmarshal(data, &v.name)
}