Skip to content

Commit 6c677af

Browse files
authored
Improve go/rpc API (github#905)
* improve go/rpc API * fix TestMultiClient * code review feedback
1 parent 4088739 commit 6c677af

File tree

5 files changed

+233
-259
lines changed

5 files changed

+233
-259
lines changed

go/internal/e2e/multi_client_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ import (
1616
func TestMultiClient(t *testing.T) {
1717
// Use TCP mode so a second client can connect to the same CLI process
1818
ctx := testharness.NewTestContext(t)
19-
client1 := copilot.NewClient(&copilot.ClientOptions{
20-
CLIPath: ctx.CLIPath,
21-
Cwd: ctx.WorkDir,
22-
Env: ctx.Env(),
23-
UseStdio: copilot.Bool(false),
19+
client1 := ctx.NewClient(func(opts *copilot.ClientOptions) {
20+
opts.UseStdio = copilot.Bool(false)
2421
})
2522
t.Cleanup(func() { client1.ForceStop() })
2623

go/internal/e2e/testharness/context.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ func (c *TestContext) Env() []string {
158158
}
159159

160160
// NewClient creates a CopilotClient configured for this test context.
161-
func (c *TestContext) NewClient() *copilot.Client {
161+
// Optional overrides can be applied to the default ClientOptions via the opts function.
162+
func (c *TestContext) NewClient(opts ...func(*copilot.ClientOptions)) *copilot.Client {
162163
options := &copilot.ClientOptions{
163164
CLIPath: c.CLIPath,
164165
Cwd: c.WorkDir,
@@ -170,6 +171,10 @@ func (c *TestContext) NewClient() *copilot.Client {
170171
options.GitHubToken = "fake-token-for-e2e-tests"
171172
}
172173

174+
for _, opt := range opts {
175+
opt(options)
176+
}
177+
173178
return copilot.NewClient(options)
174179
}
175180

go/internal/jsonrpc2/jsonrpc2.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,23 @@ func (c *Client) Request(method string, params any) (json.RawMessage, error) {
214214
}
215215
}
216216

217-
paramsData, err := json.Marshal(params)
218-
if err != nil {
219-
return nil, fmt.Errorf("failed to marshal params: %w", err)
217+
var paramsData json.RawMessage
218+
if params == nil {
219+
paramsData = json.RawMessage("{}")
220+
} else {
221+
var err error
222+
paramsData, err = json.Marshal(params)
223+
if err != nil {
224+
return nil, fmt.Errorf("failed to marshal params: %w", err)
225+
}
220226
}
221227

222228
// Send request
223229
request := Request{
224230
JSONRPC: "2.0",
225231
ID: json.RawMessage(`"` + requestID + `"`),
226232
Method: method,
227-
Params: json.RawMessage(paramsData),
233+
Params: paramsData,
228234
}
229235

230236
if err := c.sendMessage(request); err != nil {
@@ -261,15 +267,19 @@ func (c *Client) Request(method string, params any) (json.RawMessage, error) {
261267

262268
// Notify sends a JSON-RPC notification (no response expected)
263269
func (c *Client) Notify(method string, params any) error {
264-
paramsData, err := json.Marshal(params)
265-
if err != nil {
266-
return fmt.Errorf("failed to marshal params: %w", err)
270+
var paramsData json.RawMessage
271+
if params != nil {
272+
var err error
273+
paramsData, err = json.Marshal(params)
274+
if err != nil {
275+
return fmt.Errorf("failed to marshal params: %w", err)
276+
}
267277
}
268278

269279
notification := Request{
270280
JSONRPC: "2.0",
271281
Method: method,
272-
Params: json.RawMessage(paramsData),
282+
Params: paramsData,
273283
}
274284
return c.sendMessage(notification)
275285
}

0 commit comments

Comments
 (0)