Skip to content
This repository was archived by the owner on Jan 14, 2022. It is now read-only.

Commit cad04d9

Browse files
Delete a bunch of redundant functions
1 parent 26fc34b commit cad04d9

25 files changed

+378
-1481
lines changed

client_connection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ func (c *Client) Connect() error {
2020

2121
// We can't use SendRequest yet because we haven't started polling.
2222

23-
reqGAR := GetAuthRequiredRequest{
23+
reqGAR := GetAuthRequiredRequest{_request{
2424
ID_: getMessageID(),
2525
Type_: "GetAuthRequired",
26-
}
26+
}}
2727
if err = c.conn.WriteJSON(reqGAR); err != nil {
2828
return errors.Wrap(err, "write Authenticate")
2929
}

codegen/protocol.py

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
doc = "https://github.com/Palakis/obs-websocket/blob/master/docs/generated/protocol.md"
1212

13-
disclaimer = """\
13+
disclaimer = """
1414
// This file is automatically generated.
15-
// https://github.com/christopher-dG/go-obs-websocket/blob/master/codegen/protocol.py\
15+
// https://github.com/christopher-dG/go-obs-websocket/blob/master/codegen/protocol.py
1616
"""
1717

1818
# TODO: Test the less clear ones.
@@ -44,10 +44,16 @@ def process_json(d: Dict):
4444
def gen_category(prefix: str, category: str, data: Dict):
4545
"""Generate all events or requests in one category."""
4646
func = gen_event if prefix == "events" else gen_request
47-
content = "\n\n".join(func(event) for event in data)
47+
content = "\n".join(
48+
filter(
49+
lambda s: not s.isspace(),
50+
"\n".join(func(thing) for thing in data).split("\n"),
51+
)
52+
)
53+
4854
with open(f"{prefix}_{category}.go".replace(" ", "_"), "w") as f:
4955
f.write(
50-
f"""\
56+
f"""
5157
package {package}
5258
5359
{disclaimer}
@@ -65,15 +71,14 @@ def gen_events(events: Dict):
6571

6672
def gen_event(data: Dict) -> str:
6773
"""Write Go code with a type definition and interface functions."""
68-
if data.get("returns"):
69-
struct = f"""\
70-
type {data["name"]}Event struct {{
71-
{go_struct_variables(go_variables(data["returns"]))}
72-
_event `json:",squash"`
73-
}}\
74-
"""
75-
else:
76-
struct = f"type {data['name']}Event _event"
74+
struct = f"""
75+
type {data["name"]}Event struct {{
76+
{go_struct_variables(go_variables(data.get("returns", [])))}
77+
_event `json:",squash"`
78+
}}
79+
"""
80+
# else:
81+
# struct = f"type {data['name']}Event _event"
7782

7883
description = newlinify(f"{data['name']}Event : {data['description']}")
7984
if not description.endswith("."):
@@ -83,19 +88,10 @@ def gen_event(data: Dict) -> str:
8388
f"\n// Since obs-websocket version: {data['since'].capitalize()}."
8489
)
8590

86-
return f"""\
91+
return f"""
8792
{description}
8893
// {doc}#{data["heading"]["text"].lower()}
8994
{struct}
90-
91-
// Type returns the event's update type.
92-
func (e {data["name"]}Event) Type() string {{ return e.Type_ }}
93-
94-
// StreamTimecode returns the event's stream timecode.
95-
func (e {data["name"]}Event) StreamTimecode() string {{ return e.StreamTimecode_ }}
96-
97-
// RecTimecode returns the event's recording timecode.
98-
func (e {data["name"]}Event) RecTimecode() string {{ return e.RecTimecode_ }}
9995
"""
10096

10197

@@ -108,14 +104,14 @@ def gen_requests(requests: Dict):
108104
def gen_request(data: Dict) -> str:
109105
"""Write Go code with type definitions and interface functions."""
110106
if data.get("params"):
111-
struct = f"""\
107+
struct = f"""
112108
type {data["name"]}Request struct {{
113109
{go_struct_variables(go_variables(data["params"]))}
114110
_request `json:",squash"`
115111
}}
116112
"""
117113
else:
118-
struct = f"type {data['name']}Request _request"
114+
struct = f"type {data['name']}Request struct {{ _request }}"
119115

120116
description = newlinify(f"{data['name']}Request : {data['description']}")
121117
if description and not description.endswith("."):
@@ -125,19 +121,13 @@ def gen_request(data: Dict) -> str:
125121
f"\n// Since obs-websocket version: {data['since'].capitalize()}."
126122
)
127123

128-
request = f"""\
124+
request = f"""
129125
{description}
130126
// {doc}#{data["heading"]["text"].lower()}
131127
{struct}
132128
133129
{gen_request_new(data)}
134130
135-
// ID returns the request's message ID.
136-
func (r {data["name"]}Request) ID() string {{ return r.ID_ }}
137-
138-
// Type returns the request's message type.
139-
func (r {data["name"]}Request) Type() string {{ return r.Type_ }}
140-
141131
// Send sends the request and returns a channel to which the response will be sent.
142132
func (r {data["name"]}Request) Send(c Client) (chan {data["name"]}Response, error) {{
143133
generic, err := c.SendRequest(r)
@@ -151,49 +141,47 @@ def gen_request(data: Dict) -> str:
151141
"""
152142

153143
if data.get("returns"):
154-
struct = f"""\
144+
struct = f"""
155145
type {data["name"]}Response struct {{
156146
{go_struct_variables(go_variables(data["returns"]))}
157147
_response `json:",squash"`
158148
}}
159149
"""
160150
else:
161-
struct = f"type {data['name']}Response _response"
151+
struct = (
152+
f"""type {data["name"]}Response struct {{ _response `json:",squash"`}}"""
153+
)
162154

163155
description = f"// {data['name']}Response : Response for {data['name']}Request."
164156
if data.get("since"):
165157
description += (
166158
f"\n// Since obs-websocket version: {data['since'].capitalize()}."
167159
)
168160

169-
response = f"""\
161+
response = f"""
170162
{description}
171163
// {doc}#{data["heading"]["text"].lower()}
172164
{struct}
173-
174-
// ID returns the response's message ID.
175-
func (r {data["name"]}Response) ID() string {{ return r.ID_ }}
176-
177-
// Status returns the response's status.
178-
func (r {data["name"]}Response) Status() string {{ return r.Status_ }}
179-
180-
// Error returns the response's error.
181-
func (r {data["name"]}Response) Error() string {{ return r.Error_ }}
182165
"""
183166

184167
return f"{request}\n\n{response}"
185168

186169

187170
def gen_request_new(request: Dict):
188171
"""Generate Go code with a New___Request function for a request type."""
189-
base = f"""\
172+
base = f"""
190173
// New{request["name"]}Request returns a new {request["name"]}Request.
191174
func New{request["name"]}Request(\
192175
"""
193176
variables = go_variables(request.get("params", []), export=False)
194177
if not variables:
195-
sig = f"{base}) {request['name']}Request {{"
196-
constructor_args = f'{{ID_: getMessageID(), Type_: "{request["name"]}"}}'
178+
sig = f"{base}) {request['name']}Request"
179+
constructor_args = f"""\
180+
{{_request{{
181+
ID_: getMessageID(),
182+
Type_: "{request["name"]}",
183+
}}}}
184+
"""
197185
else:
198186
args = "\n".join(
199187
f"{'_type' if var['name'] == 'type' else var['name']} {var['type']},"
@@ -214,18 +202,14 @@ def gen_request_new(request: Dict):
214202
"""
215203
)
216204
if len(variables) == 1:
217-
sig = f"{base}{args}) {request['name']}Request {{"
205+
sig = f"{base}{args}) {request['name']}Request"
218206
else:
219-
sig = f"""\
207+
sig = f"""
220208
{base}
221209
{args}
222-
) {request["name"]}Request {{\
210+
) {request["name"]}Request\
223211
"""
224-
return f"""\
225-
{sig}
226-
return {request["name"]}Request{constructor_args}
227-
}}\
228-
"""
212+
return f"{sig} {{ return {request['name']}Request{constructor_args} }}"
229213

230214

231215
def gen_typeswitches(data: Dict):
@@ -270,7 +254,7 @@ def gen_typeswitches(data: Dict):
270254

271255
with open("typeswitches.go", "w") as f:
272256
f.write(
273-
f"""\
257+
f"""
274258
package {package}
275259
276260
{disclaimer}

events.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ type _event struct {
1414
RecTimecode_ string `json:"rec-timecode"`
1515
}
1616

17+
// Type returns the event's update type.
1718
func (e _event) Type() string { return e.Type_ }
1819

20+
// StreamTimeode returns the event's stream timecode.
1921
func (e _event) StreamTimecode() string { return e.StreamTimecode_ }
2022

23+
// RecTimecode returns the event's recording timecode.
2124
func (e _event) RecTimecode() string { return e.RecTimecode_ }

events_general.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,3 @@ type HeartbeatEvent struct {
4141
TotalRecordFrames int `json:"total-record-frames"`
4242
_event `json:",squash"`
4343
}
44-
45-
// Type returns the event's update type.
46-
func (e HeartbeatEvent) Type() string { return e.Type_ }
47-
48-
// StreamTimecode returns the event's stream timecode.
49-
func (e HeartbeatEvent) StreamTimecode() string { return e.StreamTimecode_ }
50-
51-
// RecTimecode returns the event's recording timecode.
52-
func (e HeartbeatEvent) RecTimecode() string { return e.RecTimecode_ }

events_other.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ package obsws
66
// ExitingEvent : OBS is exiting.
77
// Since obs-websocket version: 0.3.
88
// https://github.com/Palakis/obs-websocket/blob/master/docs/generated/protocol.md#exiting
9-
type ExitingEvent _event
10-
11-
// Type returns the event's update type.
12-
func (e ExitingEvent) Type() string { return e.Type_ }
13-
14-
// StreamTimecode returns the event's stream timecode.
15-
func (e ExitingEvent) StreamTimecode() string { return e.StreamTimecode_ }
16-
17-
// RecTimecode returns the event's recording timecode.
18-
func (e ExitingEvent) RecTimecode() string { return e.RecTimecode_ }
9+
type ExitingEvent struct {
10+
_event `json:",squash"`
11+
}

events_profiles.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,13 @@ package obsws
66
// ProfileChangedEvent : Triggered when switching to another profile or when renaming the current profile.
77
// Since obs-websocket version: 4.0.0.
88
// https://github.com/Palakis/obs-websocket/blob/master/docs/generated/protocol.md#profilechanged
9-
type ProfileChangedEvent _event
10-
11-
// Type returns the event's update type.
12-
func (e ProfileChangedEvent) Type() string { return e.Type_ }
13-
14-
// StreamTimecode returns the event's stream timecode.
15-
func (e ProfileChangedEvent) StreamTimecode() string { return e.StreamTimecode_ }
16-
17-
// RecTimecode returns the event's recording timecode.
18-
func (e ProfileChangedEvent) RecTimecode() string { return e.RecTimecode_ }
9+
type ProfileChangedEvent struct {
10+
_event `json:",squash"`
11+
}
1912

2013
// ProfileListChangedEvent : Triggered when a profile is created, added, renamed, or removed.
2114
// Since obs-websocket version: 4.0.0.
2215
// https://github.com/Palakis/obs-websocket/blob/master/docs/generated/protocol.md#profilelistchanged
23-
type ProfileListChangedEvent _event
24-
25-
// Type returns the event's update type.
26-
func (e ProfileListChangedEvent) Type() string { return e.Type_ }
27-
28-
// StreamTimecode returns the event's stream timecode.
29-
func (e ProfileListChangedEvent) StreamTimecode() string { return e.StreamTimecode_ }
30-
31-
// RecTimecode returns the event's recording timecode.
32-
func (e ProfileListChangedEvent) RecTimecode() string { return e.RecTimecode_ }
16+
type ProfileListChangedEvent struct {
17+
_event `json:",squash"`
18+
}

events_recording.go

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,27 @@ package obsws
66
// RecordingStartingEvent : A request to start recording has been issued.
77
// Since obs-websocket version: 0.3.
88
// https://github.com/Palakis/obs-websocket/blob/master/docs/generated/protocol.md#recordingstarting
9-
type RecordingStartingEvent _event
10-
11-
// Type returns the event's update type.
12-
func (e RecordingStartingEvent) Type() string { return e.Type_ }
13-
14-
// StreamTimecode returns the event's stream timecode.
15-
func (e RecordingStartingEvent) StreamTimecode() string { return e.StreamTimecode_ }
16-
17-
// RecTimecode returns the event's recording timecode.
18-
func (e RecordingStartingEvent) RecTimecode() string { return e.RecTimecode_ }
9+
type RecordingStartingEvent struct {
10+
_event `json:",squash"`
11+
}
1912

2013
// RecordingStartedEvent : Recording started successfully.
2114
// Since obs-websocket version: 0.3.
2215
// https://github.com/Palakis/obs-websocket/blob/master/docs/generated/protocol.md#recordingstarted
23-
type RecordingStartedEvent _event
24-
25-
// Type returns the event's update type.
26-
func (e RecordingStartedEvent) Type() string { return e.Type_ }
27-
28-
// StreamTimecode returns the event's stream timecode.
29-
func (e RecordingStartedEvent) StreamTimecode() string { return e.StreamTimecode_ }
30-
31-
// RecTimecode returns the event's recording timecode.
32-
func (e RecordingStartedEvent) RecTimecode() string { return e.RecTimecode_ }
16+
type RecordingStartedEvent struct {
17+
_event `json:",squash"`
18+
}
3319

3420
// RecordingStoppingEvent : A request to stop recording has been issued.
3521
// Since obs-websocket version: 0.3.
3622
// https://github.com/Palakis/obs-websocket/blob/master/docs/generated/protocol.md#recordingstopping
37-
type RecordingStoppingEvent _event
38-
39-
// Type returns the event's update type.
40-
func (e RecordingStoppingEvent) Type() string { return e.Type_ }
41-
42-
// StreamTimecode returns the event's stream timecode.
43-
func (e RecordingStoppingEvent) StreamTimecode() string { return e.StreamTimecode_ }
44-
45-
// RecTimecode returns the event's recording timecode.
46-
func (e RecordingStoppingEvent) RecTimecode() string { return e.RecTimecode_ }
23+
type RecordingStoppingEvent struct {
24+
_event `json:",squash"`
25+
}
4726

4827
// RecordingStoppedEvent : Recording stopped successfully.
4928
// Since obs-websocket version: 0.3.
5029
// https://github.com/Palakis/obs-websocket/blob/master/docs/generated/protocol.md#recordingstopped
51-
type RecordingStoppedEvent _event
52-
53-
// Type returns the event's update type.
54-
func (e RecordingStoppedEvent) Type() string { return e.Type_ }
55-
56-
// StreamTimecode returns the event's stream timecode.
57-
func (e RecordingStoppedEvent) StreamTimecode() string { return e.StreamTimecode_ }
58-
59-
// RecTimecode returns the event's recording timecode.
60-
func (e RecordingStoppedEvent) RecTimecode() string { return e.RecTimecode_ }
30+
type RecordingStoppedEvent struct {
31+
_event `json:",squash"`
32+
}

0 commit comments

Comments
 (0)