Skip to content

Commit 442cf1d

Browse files
committed
feat: SendRequest and SendNotification now uses ellipsis for parameters
1 parent e0c48df commit 442cf1d

File tree

8 files changed

+28
-21
lines changed

8 files changed

+28
-21
lines changed

examples/generic_sock_client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func main() {
5959
args = append(args, arg)
6060
}
6161
}
62-
reqResult, reqError, err := conn.SendRequest(context.Background(), method, args)
62+
reqResult, reqError, err := conn.SendRequest(context.Background(), method, args...)
6363
if err != nil {
6464
fmt.Println("Error sending request:", err)
6565
return

examples/mult_server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func main() {
6161

6262
// Register the ping method
6363
ctx := context.Background()
64-
_, reqErr, err := conn.SendRequest(ctx, "$/register", []any{"mult"})
64+
_, reqErr, err := conn.SendRequest(ctx, "$/register", "mult")
6565
if err != nil {
6666
slog.Error("Failed to send register request for ping method", "err", err)
6767
return

examples/ping_client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func main() {
3434
go conn.Run()
3535

3636
// Client
37-
reqResult, reqError, err := conn.SendRequest(context.Background(), "ping", []any{"HELLO", 1, true, 5.0})
37+
reqResult, reqError, err := conn.SendRequest(context.Background(), "ping", "HELLO", 1, true, 5.0)
3838
if err != nil {
3939
panic(err)
4040
}

examples/ping_server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func main() {
5050

5151
// Register the ping method
5252
ctx := context.Background()
53-
_, reqErr, err := conn.SendRequest(ctx, "$/register", []any{"ping"})
53+
_, reqErr, err := conn.SendRequest(ctx, "$/register", "ping")
5454
if err != nil {
5555
slog.Error("Failed to send register request for ping method", "err", reqErr)
5656
return

internal/msgpackrouter/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (r *Router) connectionLoop(conn io.ReadWriteCloser) {
129129
}
130130

131131
// Forward the call to the registered client
132-
reqResult, reqError, err := client.SendRequest(ctx, method, params)
132+
reqResult, reqError, err := client.SendRequest(ctx, method, params...)
133133
if err != nil {
134134
slog.Error("Failed to send request", "method", method, "err", err)
135135
return nil, routerError(ErrCodeFailedToSendRequests, fmt.Sprintf("failed to send request: %s", err))
@@ -157,7 +157,7 @@ func (r *Router) connectionLoop(conn io.ReadWriteCloser) {
157157
}
158158

159159
// Forward the notification to the registered client
160-
if err := client.SendNotification(method, params); err != nil {
160+
if err := client.SendNotification(method, params...); err != nil {
161161
slog.Error("Failed to send notification", "method", method, "err", err)
162162
return
163163
}

internal/msgpackrouter/router_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,59 +98,59 @@ func TestBasicRouterFunctionality(t *testing.T) {
9898

9999
{
100100
// Register a method on the first client
101-
result, reqErr, err := cl1.SendRequest(context.Background(), "$/register", []any{"ping"})
101+
result, reqErr, err := cl1.SendRequest(context.Background(), "$/register", "ping")
102102
require.Equal(t, true, result)
103103
require.Nil(t, reqErr)
104104
require.NoError(t, err)
105105
}
106106
{
107107
// Try to re-register the same method
108-
result, reqErr, err := cl1.SendRequest(context.Background(), "$/register", []any{"ping"})
108+
result, reqErr, err := cl1.SendRequest(context.Background(), "$/register", "ping")
109109
require.Nil(t, result)
110110
require.Equal(t, []any{int8(msgpackrouter.ErrCodeRouteAlreadyExists), "route already exists: ping"}, reqErr)
111111
require.NoError(t, err)
112112
}
113113
{
114114
// Register a method on the second client
115-
result, reqErr, err := cl2.SendRequest(context.Background(), "$/register", []any{"temperature"})
115+
result, reqErr, err := cl2.SendRequest(context.Background(), "$/register", "temperature")
116116
require.Equal(t, true, result)
117117
require.Nil(t, reqErr)
118118
require.NoError(t, err)
119119
}
120120
{
121121
// Call from client2 the registered method on client1
122-
result, reqErr, err := cl2.SendRequest(context.Background(), "ping", []any{"1", 2, true})
122+
result, reqErr, err := cl2.SendRequest(context.Background(), "ping", "1", 2, true)
123123
require.Equal(t, []any{"1", int8(2), true}, result)
124124
require.Nil(t, reqErr)
125125
require.NoError(t, err)
126126
}
127127
{
128128
// Self-call from client1
129-
result, reqErr, err := cl1.SendRequest(context.Background(), "ping", []any{"c", 12, false})
129+
result, reqErr, err := cl1.SendRequest(context.Background(), "ping", "c", 12, false)
130130
require.Equal(t, []any{"c", int8(12), false}, result)
131131
require.Nil(t, reqErr)
132132
require.NoError(t, err)
133133
}
134134
{
135135
// Call from client2 an un-registered method
136-
result, reqErr, err := cl2.SendRequest(context.Background(), "not-existent-method", []any{"1", 2, true})
136+
result, reqErr, err := cl2.SendRequest(context.Background(), "not-existent-method", "1", 2, true)
137137
require.Nil(t, result)
138138
require.Equal(t, []any{int8(msgpackrouter.ErrCodeMethodNotAvailable), "method not-existent-method not available"}, reqErr)
139139
require.NoError(t, err)
140140
}
141141
{
142142
// Send notification to client1
143-
err := cl2.SendNotification("ping", []any{"a", int16(4), false})
143+
err := cl2.SendNotification("ping", "a", int16(4), false)
144144
require.NoError(t, err)
145145
}
146146
{
147147
// Send notification to unregistered method
148-
err := cl2.SendNotification("notexistent", []any{"a", int16(4), false})
148+
err := cl2.SendNotification("notexistent", "a", int16(4), false)
149149
require.NoError(t, err)
150150
}
151151
{
152152
// Self-send notification
153-
err := cl1.SendNotification("ping", []any{"b", int16(14), true, true})
153+
err := cl1.SendNotification("ping", "b", int16(14), true, true)
154154
require.NoError(t, err)
155155
}
156156
time.Sleep(100 * time.Millisecond) // Give some time for the notifications to be processed
@@ -190,7 +190,7 @@ func TestMessageForwarderCongestionControl(t *testing.T) {
190190

191191
{
192192
// Register a method on the first client
193-
result, reqErr, err := cl1.SendRequest(context.Background(), "$/register", []any{"test"})
193+
result, reqErr, err := cl1.SendRequest(context.Background(), "$/register", "test")
194194
require.Equal(t, true, result)
195195
require.Nil(t, reqErr)
196196
require.NoError(t, err)
@@ -201,7 +201,7 @@ func TestMessageForwarderCongestionControl(t *testing.T) {
201201
var wg sync.WaitGroup
202202
for range batchSize {
203203
wg.Go(func() {
204-
_, _, err := cl2.SendRequest(t.Context(), "test", []any{})
204+
_, _, err := cl2.SendRequest(t.Context(), "test")
205205
require.NoError(t, err)
206206
})
207207
}

msgpackrpc/connection.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,10 @@ func (c *Connection) Close() {
327327
_ = c.out.Close()
328328
}
329329

330-
func (c *Connection) SendRequest(ctx context.Context, method string, params []any) (reqResult any, reqError any, err error) {
330+
func (c *Connection) SendRequest(ctx context.Context, method string, params ...any) (reqResult any, reqError any, err error) {
331+
if params == nil {
332+
params = []any{}
333+
}
331334
id := MessageID(c.lastOutRequestsIndex.Add(1))
332335

333336
c.loggerMutex.Lock()
@@ -364,7 +367,7 @@ func (c *Connection) SendRequest(ctx context.Context, method string, params []an
364367
c.logger.LogOutgoingCancelRequest(id)
365368
c.loggerMutex.Unlock()
366369

367-
_ = c.SendNotification("$/cancelRequest", []any{id}) // ignore error (it won't matter anyway)
370+
_ = c.SendNotification("$/cancelRequest", id) // ignore error (it won't matter anyway)
368371
}
369372

370373
// After cancelation wait for result...
@@ -378,7 +381,11 @@ func (c *Connection) SendRequest(ctx context.Context, method string, params []an
378381
return result.reqResult, result.reqError, nil
379382
}
380383

381-
func (c *Connection) SendNotification(method string, params []any) error {
384+
func (c *Connection) SendNotification(method string, params ...any) error {
385+
if params == nil {
386+
params = []any{}
387+
}
388+
382389
c.loggerMutex.Lock()
383390
c.logger.LogOutgoingNotification(method, params)
384391
c.loggerMutex.Unlock()

msgpackrpc/connection_rpc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func TestRPCConnection(t *testing.T) {
130130
wg.Add(1)
131131
go func() {
132132
defer wg.Done()
133-
respRes, respErr, err := conn.SendRequest(t.Context(), "helloworld", []any{true})
133+
respRes, respErr, err := conn.SendRequest(t.Context(), "helloworld", true)
134134
require.NoError(t, err)
135135
require.Nil(t, respErr)
136136
require.Equal(t, map[string]any{"fakedata": int8(99)}, respRes)

0 commit comments

Comments
 (0)