Skip to content

Commit d25f7e0

Browse files
authored
Merge pull request #3 from myleshyson/add-utils
add some helpful utils for decoding a jsonrpc message into a struc
2 parents 8784b96 + 9646943 commit d25f7e0

File tree

8 files changed

+1214
-360
lines changed

8 files changed

+1214
-360
lines changed

lspgenerator/go/go_generator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .enums import generate_enums
99
from .notifications import generate_notifications
1010
from .or_types import generate_or_types
11+
from .registry import generate_registry
1112
from .requests import generate_requests
1213
from .structs import generate_structs
1314
from .tests import generate_tests
@@ -40,6 +41,7 @@ def generate_from_spec(
4041
+ or_types,
4142
),
4243
"types_test.go": generate_tests(spec),
44+
"registry.go": generate_registry(spec),
4345
}
4446
output_path = pathlib.Path(output_dir)
4547
test_path = pathlib.Path(test_dir)

lspgenerator/go/notifications.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ def generate_notifications(
4646
f'\tParams {param_type} `json:"params"`',
4747
]
4848
struct.append("}")
49-
struct.append(f"func (t *{notification.typeName}) isMessage() {{}}")
49+
struct.append(f"func (t {notification.typeName}) isMessage() {{}}")
5050
struct.append(
51-
f"func (t *{notification.typeName}) isNotification() {{}}"
51+
f"func (t {notification.typeName}) isNotification() {{}}",
5252
)
5353
struct += [
5454
f"func (t *{notification.typeName}) UnmarshalJSON(x []byte) error {{",

lspgenerator/go/registry.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from generator import model
2+
3+
from .utils import join
4+
5+
6+
def generate_registry(spec: model.LSPModel) -> str:
7+
result = [
8+
"package protocol",
9+
"import (",
10+
' "encoding/json"',
11+
")",
12+
"var MessageRegistry = map[string]func([]byte) (Message, error) {",
13+
]
14+
15+
result.extend(
16+
[
17+
join(
18+
[
19+
f' "{request.method}": func(data []byte) (Message, error) {{',
20+
f" var message {request.typeName}",
21+
" if err := json.Unmarshal(data, &message); err != nil {",
22+
" return nil, err",
23+
" }",
24+
" return message, nil",
25+
" },",
26+
],
27+
)
28+
for request in spec.requests
29+
],
30+
)
31+
result.extend(
32+
[
33+
join(
34+
[
35+
f' "{notification.method}": func(data []byte) (Message, error) {{',
36+
f" var message {notification.typeName}",
37+
" if err := json.Unmarshal(data, &message); err != nil {",
38+
" return nil, err",
39+
" }",
40+
" return message, nil",
41+
" },",
42+
],
43+
)
44+
for notification in spec.notifications
45+
],
46+
)
47+
result.append("}")
48+
49+
return join(result)

lspgenerator/go/requests.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +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() {{}}")
53+
struct.append(f"func (t {request.typeName}) isMessage() {{}}")
54+
struct.append(f"func (t {request.typeName}) isRequest() {{}}")
5555
struct += [
5656
f"func (t *{request.typeName}) UnmarshalJSON(x []byte) error {{",
5757
" var m map[string]any",
@@ -119,7 +119,7 @@ def generate_requests(
119119
" return nil",
120120
"}",
121121
]
122-
struct.append(f"func (t *{response_name}) isMessage() {{}}")
123-
struct.append(f"func (t *{response_name}) isResponse() {{}}")
122+
struct.append(f"func (t {response_name}) isMessage() {{}}")
123+
struct.append(f"func (t {response_name}) isResponse() {{}}")
124124
result.append(join(struct))
125125
return result

0 commit comments

Comments
 (0)