Skip to content

Commit 8784b96

Browse files
authored
Merge pull request #2 from myleshyson/add-message-interfaces
add message interfaces
2 parents 4836497 + ca6fdb6 commit 8784b96

File tree

5 files changed

+401
-24
lines changed

5 files changed

+401
-24
lines changed

lspgenerator/go/base_types.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def generate_base_types(
110110
),
111111
)
112112
result.append(
113-
"\n".join(
113+
join(
114114
[
115115
"type ResponseError struct {",
116116
'\tCode int32 `json:"code"`',
@@ -120,5 +120,23 @@ def generate_base_types(
120120
],
121121
),
122122
)
123+
result.append(
124+
join(
125+
[
126+
"type Message interface {",
127+
" isMessage()",
128+
"}",
129+
"type Request interface {",
130+
" isRequest()",
131+
"}",
132+
"type Notification interface {",
133+
" isNotification()",
134+
"}",
135+
"type Response interface {",
136+
" isResponse()",
137+
"}",
138+
],
139+
),
140+
)
123141
result.append("\n")
124142
return result

lspgenerator/go/notifications.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def generate_notifications(
4646
f'\tParams {param_type} `json:"params"`',
4747
]
4848
struct.append("}")
49+
struct.append(f"func (t *{notification.typeName}) isMessage() {{}}")
50+
struct.append(
51+
f"func (t *{notification.typeName}) isNotification() {{}}"
52+
)
4953
struct += [
5054
f"func (t *{notification.typeName}) UnmarshalJSON(x []byte) error {{",
5155
" var m map[string]any",

lspgenerator/go/requests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def generate_requests(
5050
]
5151

5252
struct.append("}")
53+
struct.append(f"func (t *{request.typeName}) isMessage() {{}}")
54+
struct.append(f"func (t *{request.typeName}) isRequest() {{}}")
5355
struct += [
5456
f"func (t *{request.typeName}) UnmarshalJSON(x []byte) error {{",
5557
" var m map[string]any",
@@ -117,5 +119,7 @@ def generate_requests(
117119
" return nil",
118120
"}",
119121
]
122+
struct.append(f"func (t *{response_name}) isMessage() {{}}")
123+
struct.append(f"func (t *{response_name}) isResponse() {{}}")
120124
result.append(join(struct))
121125
return result

protocol/interfaces_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package protocol
2+
3+
// Compile-time interface compliance checks
4+
var (
5+
_ Message = (*InitializeRequest)(nil)
6+
_ Message = (*InitializedNotification)(nil)
7+
_ Message = (*InitializeResponse)(nil)
8+
_ Request = (*InitializeRequest)(nil)
9+
_ Notification = (*InitializedNotification)(nil)
10+
_ Response = (*InitializeResponse)(nil)
11+
)

0 commit comments

Comments
 (0)