forked from actatum/stormrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
126 lines (104 loc) · 2.83 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package stormrpc
import (
"encoding/json"
"fmt"
"github.com/nats-io/nats.go"
"github.com/vmihailenco/msgpack/v5"
"google.golang.org/protobuf/proto"
)
// Request is stormRPC's wrapper around a nats.Msg and is used by both clients and servers.
type Request struct {
*nats.Msg
}
// NewRequest constructs a new request with the given parameters. It also handles encoding the request body.
func NewRequest(subject string, body any, opts ...RequestOption) (Request, error) {
options := requestOptions{
encodeProto: false,
encodeMsgpack: false,
}
for _, o := range opts {
o.apply(&options)
}
var data []byte
var err error
var contentType string
switch {
case options.encodeProto:
switch m := body.(type) {
case proto.Message:
data, err = proto.Marshal(m)
contentType = "application/protobuf"
default:
return Request{}, fmt.Errorf("failed to encode proto message: invalid type: %T", m)
}
case options.encodeMsgpack:
data, err = msgpack.Marshal(body)
contentType = "application/msgpack"
default:
data, err = json.Marshal(body)
contentType = "application/json"
}
if err != nil {
return Request{}, err
}
headers := nats.Header{}
headers.Set("Content-Type", contentType)
msg := &nats.Msg{
Data: data,
Subject: subject,
Header: headers,
}
return Request{
Msg: msg,
}, nil
}
type requestOptions struct {
encodeProto bool
encodeMsgpack bool
}
// RequestOption represents functional options for configuring a request.
type RequestOption interface {
apply(options *requestOptions)
}
type encodeProtoOption bool
func (p encodeProtoOption) apply(opts *requestOptions) {
opts.encodeProto = bool(p)
}
// WithEncodeProto is a RequestOption to encode the request body using the proto.Marshal method.
func WithEncodeProto() RequestOption {
return encodeProtoOption(true)
}
type encodeMsgpackOption bool
func (p encodeMsgpackOption) apply(opts *requestOptions) {
opts.encodeMsgpack = bool(p)
}
// WithEncodeMsgpack is a RequestOption to encode the request body using the msgpack.Marshal method.
func WithEncodeMsgpack() RequestOption {
return encodeMsgpackOption(true)
}
// Decode de-serializes the body into the passed in object. The de-serialization method is based on
// the request's Content-Type header.
func (r *Request) Decode(v any) error {
var err error
switch r.Header.Get("Content-Type") {
case "application/msgpack":
err = msgpack.Unmarshal(r.Data, v)
case "application/protobuf":
switch m := v.(type) {
case proto.Message:
err = proto.Unmarshal(r.Data, m)
default:
return fmt.Errorf("failed to decode proto message: invalid type :%T", v)
}
default:
err = json.Unmarshal(r.Data, v)
}
if err != nil {
return fmt.Errorf("failed to decode request: %w", err)
}
return nil
}
// Subject returns the underlying nats.Msg subject.
func (r *Request) Subject() string {
return r.Msg.Subject
}