-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
104 lines (88 loc) · 1.83 KB
/
options.go
File metadata and controls
104 lines (88 loc) · 1.83 KB
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
package dndm
import (
"context"
"log/slog"
"slices"
"github.com/itohio/dndm/errors"
)
type Option func(*Options) error
type Options struct {
ctx context.Context
logger *slog.Logger
endpoints []Endpoint
size int
}
func defaultOptions() Options {
return Options{
ctx: context.Background(),
logger: slog.Default(),
endpoints: []Endpoint{},
size: 1,
}
}
func (o *Options) Config(opts ...Option) error {
for _, opt := range opts {
if err := opt(o); err != nil {
return err
}
}
return nil
}
func (o *Options) addEndpoint(t Endpoint) error {
if t == nil {
return errors.ErrInvalidEndpoint
}
if slices.Contains(o.endpoints, t) {
return errors.ErrDuplicate
}
o.endpoints = append(o.endpoints, t)
return nil
}
// WithContext option configures context.
func WithContext(ctx context.Context) Option {
return func(o *Options) error {
o.ctx = ctx
return nil
}
}
// WithLogger option configures Logger.
func WithLogger(l *slog.Logger) Option {
return func(o *Options) error {
if l == nil {
return errors.ErrBadArgument
}
o.logger = l
return nil
}
}
// WithQueueSize configures the size of send/receive/notification chan sizes.
func WithQueueSize(size int) Option {
return func(o *Options) error {
if size < 0 {
return errors.ErrBadArgument
}
o.size = size
return nil
}
}
// WithEndpoint adds an endpoint to the collection.
func WithEndpoint(t Endpoint) Option {
return func(o *Options) error {
return o.addEndpoint(t)
}
}
// WithEndpoints sets the collection to specified endpoints.
func WithEndpoints(t ...Endpoint) Option {
return func(o *Options) error {
o.endpoints = t
return nil
}
}
// Subscription options
type SubOptStruct struct {
}
type SubOpt func(*SubOptStruct) error
// Publishing options
type PubOptStruct struct {
}
type PubOpt func(*PubOptStruct) error