-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
111 lines (87 loc) · 3.61 KB
/
options.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
package restate
import (
"time"
"github.com/restatedev/sdk-go/encoding"
"github.com/restatedev/sdk-go/internal/options"
)
// re-export for use in generated code
type ClientOption = options.ClientOption
type ServiceDefinitionOption = options.ServiceDefinitionOption
type withCodec struct {
codec encoding.Codec
}
var _ options.GetOption = withCodec{}
var _ options.SetOption = withCodec{}
var _ options.RunOption = withCodec{}
var _ options.AwakeableOption = withCodec{}
var _ options.ResolveAwakeableOption = withCodec{}
var _ options.ClientOption = withCodec{}
func (w withCodec) BeforeGet(opts *options.GetOptions) { opts.Codec = w.codec }
func (w withCodec) BeforeSet(opts *options.SetOptions) { opts.Codec = w.codec }
func (w withCodec) BeforeRun(opts *options.RunOptions) { opts.Codec = w.codec }
func (w withCodec) BeforeAwakeable(opts *options.AwakeableOptions) { opts.Codec = w.codec }
func (w withCodec) BeforeResolveAwakeable(opts *options.ResolveAwakeableOptions) {
opts.Codec = w.codec
}
func (w withCodec) BeforeClient(opts *options.ClientOptions) { opts.Codec = w.codec }
// WithCodec is an option that can be provided to many different functions that perform (de)serialisation
// in order to specify a custom codec with which to (de)serialise instead of the default of JSON.
//
// See also [WithProto], [WithBinary], [WithJSON].
func WithCodec(codec encoding.Codec) withCodec {
return withCodec{codec}
}
type withPayloadCodec struct {
withCodec
codec encoding.PayloadCodec
}
var _ options.HandlerOption = withPayloadCodec{}
var _ options.ServiceDefinitionOption = withPayloadCodec{}
func (w withPayloadCodec) BeforeHandler(opts *options.HandlerOptions) {
opts.Codec = w.codec
}
func (w withPayloadCodec) BeforeServiceDefinition(opts *options.ServiceDefinitionOptions) {
opts.DefaultCodec = w.codec
}
// WithPayloadCodec is an option that can be provided to handler/service options
// in order to specify a custom [encoding.PayloadCodec] with which to (de)serialise and
// set content-types instead of the default of JSON.
//
// See also [WithProto], [WithBinary], [WithJSON].
func WithPayloadCodec(codec encoding.PayloadCodec) withPayloadCodec {
return withPayloadCodec{withCodec{codec}, codec}
}
// WithProto is an option to specify the use of [encoding.ProtoCodec] for (de)serialisation
var WithProto = WithPayloadCodec(encoding.ProtoCodec)
// WithProtoJSON is an option to specify the use of [encoding.ProtoJSONCodec] for (de)serialisation
var WithProtoJSON = WithPayloadCodec(encoding.ProtoJSONCodec)
// WithBinary is an option to specify the use of [encoding.BinaryCodec] for (de)serialisation
var WithBinary = WithPayloadCodec(encoding.BinaryCodec)
// WithJSON is an option to specify the use of [encoding.JsonCodec] for (de)serialisation
var WithJSON = WithPayloadCodec(encoding.JSONCodec)
type withHeaders struct {
headers map[string]string
}
var _ options.RequestOption = withHeaders{}
var _ options.SendOption = withHeaders{}
func (w withHeaders) BeforeRequest(opts *options.RequestOptions) {
opts.Headers = w.headers
}
func (w withHeaders) BeforeSend(opts *options.SendOptions) {
opts.Headers = w.headers
}
// WithHeaders is an option to specify outgoing headers when making a call
func WithHeaders(headers map[string]string) withHeaders {
return withHeaders{headers}
}
type withDelay struct {
delay time.Duration
}
var _ options.SendOption = withDelay{}
func (w withDelay) BeforeSend(opts *options.SendOptions) {
opts.Delay = w.delay
}
// WithDelay is an [SendOption] to specify the duration to delay the request
func WithDelay(delay time.Duration) withDelay {
return withDelay{delay}
}