Skip to content

Commit 6232189

Browse files
committed
types: fix types logic
1 parent e5be7e7 commit 6232189

File tree

2 files changed

+133
-62
lines changed

2 files changed

+133
-62
lines changed

types.go

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ import (
1111
"strconv"
1212

1313
"github.com/francoispqt/gojay"
14-
jsoniter "github.com/json-iterator/go"
1514
)
1615

16+
const Version = "2.0"
17+
1718
// ID is a Request identifier.
1819
// Only one of either the Name or Number members will be set, using the
1920
// number form if the Name is the empty string.
2021
type ID struct {
21-
Name string
22-
Number int64
22+
Name string `json:"name,omitempty"`
23+
Number int64 `json:"number,omitempty"`
2324
}
2425

2526
// String returns a string representation of the ID.
@@ -32,35 +33,35 @@ func (id *ID) String() string {
3233
if id.Name != "" {
3334
return strconv.Quote(id.Name)
3435
}
36+
3537
return "#" + strconv.FormatInt(id.Number, 10)
3638
}
3739

3840
// MarshalJSON implements json.MarshalJSON.
3941
func (id *ID) MarshalJSON() ([]byte, error) {
4042
if id.Name != "" {
41-
return jsoniter.ConfigFastest.Marshal(id.Name)
43+
return json.Marshal(id.Name)
4244
}
43-
return jsoniter.ConfigFastest.Marshal(id.Number)
45+
46+
return json.Marshal(id.Number)
4447
}
4548

4649
// UnmarshalJSON implements json.UnmarshalJSON.
4750
func (id *ID) UnmarshalJSON(data []byte) error {
4851
*id = ID{}
49-
if err := jsoniter.ConfigFastest.Unmarshal(data, &id.Number); err == nil {
52+
if err := json.Unmarshal(data, &id.Number); err == nil {
5053
return nil
5154
}
52-
return jsoniter.ConfigFastest.Unmarshal(data, &id.Name)
53-
}
5455

55-
// Message is a general message as defined by JSON-RPC. The language server protocol always uses "2.0" as the jsonrpc version.
56-
type Message struct {
57-
JSONRPC string `json:"jsonrpc"`
56+
return json.Unmarshal(data, &id.Name)
5857
}
5958

6059
// RawMessage is the same as json.RawMessage.
61-
type RawMessage struct {
62-
*gojay.EmbeddedJSON
63-
}
60+
type RawMessage gojay.EmbeddedJSON
61+
62+
var _ io.Reader = (*RawMessage)(nil)
63+
var _ json.Marshaler = (*RawMessage)(nil)
64+
var _ json.Unmarshaler = (*RawMessage)(nil)
6465

6566
// Read implements io.Reader.
6667
func (m *RawMessage) Read(p []byte) (n int, err error) {
@@ -71,7 +72,7 @@ func (m *RawMessage) Read(p []byte) (n int, err error) {
7172
return 0, io.EOF
7273
}
7374

74-
n = copy(p, *m.EmbeddedJSON)
75+
n = copy(p, *m)
7576

7677
return n, nil
7778
}
@@ -80,11 +81,11 @@ func (m *RawMessage) Read(p []byte) (n int, err error) {
8081
//
8182
// MarshalJSON returns m as the JSON encoding of m.
8283
func (m RawMessage) MarshalJSON() ([]byte, error) {
83-
if m.EmbeddedJSON == nil {
84+
if &m == nil {
8485
return []byte("null"), nil
8586
}
8687

87-
return *m.EmbeddedJSON, nil
88+
return m, nil
8889
}
8990

9091
// UnmarshalJSON implements json.Unmarshaler.
@@ -95,20 +96,17 @@ func (m *RawMessage) UnmarshalJSON(data []byte) error {
9596
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
9697
}
9798

98-
*m.EmbeddedJSON = append((*m.EmbeddedJSON)[0:0], data...)
99+
*m = append((*m)[0:0], data...)
99100

100101
return nil
101102
}
102103

103-
var _ io.Reader = (*RawMessage)(nil)
104-
var _ json.Marshaler = (*RawMessage)(nil)
105-
var _ json.Unmarshaler = (*RawMessage)(nil)
106-
107104
// Request is a request message to describe a request between the client and the server.
108105
//
109106
// Every processed request must send a response back to the sender of the request.
110107
type Request struct {
111-
Message
108+
// JSONRPC is a general message as defined by JSON-RPC.
109+
JSONRPC string `json:"jsonrpc"`
112110

113111
// The request id.
114112
ID *ID `json:"id"`
@@ -131,7 +129,8 @@ func (r *Request) IsNotify() bool {
131129
// conform to the JSON RPC specification.
132130
// The result property of the ResponseMessage should be set to null in this case to signal a successful request.
133131
type Response struct {
134-
Message
132+
// JSONRPC is a general message as defined by JSON-RPC.
133+
JSONRPC string `json:"jsonrpc"`
135134

136135
// The request id.
137136
ID *ID `json:"id"`
@@ -144,11 +143,34 @@ type Response struct {
144143
Result *RawMessage `json:"result,omitempty"`
145144
}
146145

146+
// Combined represents a all the fields of both Request and Response.
147+
type Combined struct {
148+
// JSONRPC is a general message as defined by JSON-RPC.
149+
JSONRPC string `json:"jsonrpc"`
150+
151+
// The request id.
152+
ID *ID `json:"id,omitempty"`
153+
154+
// The method to be invoked.
155+
Method string `json:"method"`
156+
157+
// The method's params.
158+
Params *RawMessage `json:"params,omitempty"`
159+
160+
// The error object in case a request fails.
161+
Error *Error `json:"error,omitempty"`
162+
163+
// The result of a request. This member is REQUIRED on success.
164+
// This member MUST NOT exist if there was an error invoking the method.
165+
Result *RawMessage `json:"result,omitempty"`
166+
}
167+
147168
// NotificationMessage is a notification message.
148169
//
149170
// A processed notification message must not send a response back. They work like events.
150171
type NotificationMessage struct {
151-
Message
172+
// JSONRPC is a general message as defined by JSON-RPC.
173+
JSONRPC string `json:"jsonrpc"`
152174

153175
// Method is the method to be invoked.
154176
Method string `json:"method"`

types_gen.go

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

0 commit comments

Comments
 (0)