Skip to content

Commit

Permalink
Update Request reply signature, it is now return json.RawMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
alinz committed May 18, 2024
1 parent 04cf73a commit 23ce938
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
3 changes: 2 additions & 1 deletion examples/request-reply-single/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ func main() {
for range 1000 {
req := &Req{A: 4, B: 2}
resp := &Resp{}
err := fn(ctx, req, resp)
rawResp, err := fn(ctx, req)
if err != nil {
fmt.Printf("%s = %s\n", req, err)
} else {
json.Unmarshal(rawResp, resp)
fmt.Printf("%s = %s\n", req, resp)
}
}
Expand Down
4 changes: 3 additions & 1 deletion examples/request-reply/request/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"fmt"

"ella.to/bus"
Expand All @@ -24,10 +25,11 @@ func main() {
for range 1000 {
req := &data.Req{A: 4, B: 2}
resp := &data.Resp{}
err := fn(ctx, req, resp)
rawResp, err := fn(ctx, req)
if err != nil {
fmt.Printf("%s = %s\n", req, err)
} else {
json.Unmarshal(rawResp, resp)
fmt.Printf("%s = %s\n", req, resp)
}
}
Expand Down
25 changes: 10 additions & 15 deletions request_reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ import (
"time"
)

type RequestFunc func(ctx context.Context, req any, resp any) error
type RequestFunc func(ctx context.Context, req any) (json.RawMessage, error)
type ReplyFunc func(ctx context.Context, req json.RawMessage) (any, error)

func Request(stream Stream, subject string) RequestFunc {
return func(ctx context.Context, req any, resp any) (err error) {
return func(ctx context.Context, req any) (out json.RawMessage, err error) {
evt, err := NewEvent(
WithSubject(subject),
WithReply(),
WithJsonData(req),
WithExpiresAt(30*time.Second),
)
if err != nil {
return err
return nil, err
}

err = stream.Put(ctx, evt)
if err != nil {
return err
return nil, err
}

for msgs, err := range stream.Get(
Expand All @@ -34,11 +34,11 @@ func Request(stream Stream, subject string) RequestFunc {
WithFromOldest(),
) {
if err != nil {
return err
return nil, err
}

if len(msgs.Events) != 1 {
return fmt.Errorf("expected one event but got %d", len(msgs.Events))
return nil, fmt.Errorf("expected one event but got %d", len(msgs.Events))
}

evt := msgs.Events[0]
Expand All @@ -50,24 +50,19 @@ func Request(stream Stream, subject string) RequestFunc {

err = json.Unmarshal(evt.Data, &replyMsg)
if err != nil {
return err
return nil, err
}

if replyMsg.Type == "error" {
var errMsg string
err = json.Unmarshal(replyMsg.Payload, &errMsg)
if err != nil {
return err
return nil, err
}
return fmt.Errorf(errMsg)
return nil, fmt.Errorf(errMsg)
}

err = json.Unmarshal(replyMsg.Payload, resp)
if err != nil {
return err
}

return nil
return replyMsg.Payload, nil
}

return
Expand Down
7 changes: 5 additions & 2 deletions request_reply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ func TestRequestReply(t *testing.T) {

req := &Req{A: 4, B: 2}
resp := &Resp{}
err := fn(context.Background(), req, resp)
rawResp, err := fn(context.Background(), req)
err = json.Unmarshal(rawResp, resp)
assert.NoError(t, err)

assert.NoError(t, err)
assert.Equal(t, 2, resp.Result)

req = &Req{A: 4, B: 0}
resp = &Resp{}
err = fn(context.Background(), req, resp)
rawResp, err = fn(context.Background(), req)
assert.Nil(t, rawResp)
assert.Error(t, err)
assert.Equal(t, "division by zero", err.Error())
}

0 comments on commit 23ce938

Please sign in to comment.