|
| 1 | +package wasmbus |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "gopkg.in/yaml.v2" |
| 10 | +) |
| 11 | + |
| 12 | +// LatticeRequest encodes the Roundtrip logic for a Bus Request |
| 13 | +// This is a generic implementation that can be used with any Bus |
| 14 | +// and any Request/Response pair. |
| 15 | +// Requests are encoded in json and Responses can be either json or yaml. |
| 16 | +// Use Pre & Post Request hooks to modify the request/response before/after. |
| 17 | +// The `T` and `Y` types are used to define the Request and Response types. |
| 18 | +// `T` should be passed by reference (pointer) and `Y` by value (struct). |
| 19 | +type LatticeRequest[T any, Y any] struct { |
| 20 | + // Request should be a reference to the request struct |
| 21 | + Request T |
| 22 | + // Response should be a struct that the response will be unmarshaled into |
| 23 | + // and should be passed by value |
| 24 | + Response Y |
| 25 | + Subject string |
| 26 | + Bus Bus |
| 27 | + PreRequest func(context.Context, T, *Message) error |
| 28 | + PostRequest func(context.Context, *Y, *Message) error |
| 29 | +} |
| 30 | + |
| 31 | +// NewLatticeRequest returns a `LatticeRequest` for a given type. |
| 32 | +// The `T` and `Y` types are used to define the Request and Response types. |
| 33 | +// `T` should be passed by reference (pointer) and `Y` by value (struct). |
| 34 | +func NewLatticeRequest[T any, Y any](bus Bus, subject string, in T, out Y) *LatticeRequest[T, Y] { |
| 35 | + return &LatticeRequest[T, Y]{ |
| 36 | + Request: in, |
| 37 | + Response: out, |
| 38 | + Bus: bus, |
| 39 | + Subject: subject, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Execute sends the request to the bus and returns the response. |
| 44 | +func (l *LatticeRequest[T, Y]) Execute(ctx context.Context) (*Y, error) { |
| 45 | + req, err := Encode(l.Subject, l.Request) |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("%w: %s", ErrEncode, err) |
| 48 | + } |
| 49 | + |
| 50 | + if l.PreRequest != nil { |
| 51 | + if err := l.PreRequest(ctx, l.Request, req); err != nil { |
| 52 | + return nil, fmt.Errorf("%w: %s", ErrOperation, err) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + rawResp, err := l.Bus.Request(ctx, req) |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("%w: %s", ErrTransport, err) |
| 59 | + } |
| 60 | + |
| 61 | + if err := Decode(rawResp, &l.Response); err != nil { |
| 62 | + return nil, fmt.Errorf("%w: %s", ErrDecode, err) |
| 63 | + } |
| 64 | + |
| 65 | + if l.PostRequest != nil { |
| 66 | + if err := l.PostRequest(ctx, &l.Response, rawResp); err != nil { |
| 67 | + return nil, fmt.Errorf("%w: %s", ErrOperation, err) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return &l.Response, nil |
| 72 | +} |
| 73 | + |
| 74 | +// Encode marshals the payload into a Message. |
| 75 | +// The payload is encoded as json. |
| 76 | +func Encode(subject string, payload any) (*Message, error) { |
| 77 | + wasmbusMsg := NewMessage(subject) |
| 78 | + wasmbusMsg.Header.Set("Content-Type", "application/json") |
| 79 | + |
| 80 | + if payload != nil { |
| 81 | + var err error |
| 82 | + wasmbusMsg.Data, err = json.Marshal(payload) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("%w: %s", ErrInternal, err) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return wasmbusMsg, nil |
| 89 | +} |
| 90 | + |
| 91 | +// Decode unmarshals the raw response data into the provided struct. |
| 92 | +// The content type is used to determine the unmarshaling format. |
| 93 | +// If the content type is not supported, an error is returned. |
| 94 | +// Acceptable content types are "application/json" and "application/yaml". |
| 95 | +func Decode(rawResp *Message, into any) error { |
| 96 | + if len(rawResp.Data) == 0 { |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + contentType := rawResp.Header.Get("Content-Type") |
| 101 | + switch contentType { |
| 102 | + case "application/json", "": |
| 103 | + return json.Unmarshal(rawResp.Data, into) |
| 104 | + case "application/yaml": |
| 105 | + return yaml.Unmarshal(rawResp.Data, into) |
| 106 | + default: |
| 107 | + return errors.New("unsupported content type") |
| 108 | + } |
| 109 | +} |
0 commit comments