-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopts.go
More file actions
66 lines (50 loc) · 1.23 KB
/
opts.go
File metadata and controls
66 lines (50 loc) · 1.23 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
package opts
type OptionContainer[T comparable] map[T]any
type Option[T comparable] func(o OptionContainer[T]) error
func CreateContainer[T comparable]() OptionContainer[T] {
return OptionContainer[T]{}
}
func CreateContainerWithOptions[T comparable](o []Option[T]) (OptionContainer[T], error) {
c := CreateContainer[T]()
if err := c.ApplyA(o); err != nil {
return nil, err
}
return c, nil
}
func CreateContainerWithOptionsS[T comparable](o []Option[T]) OptionContainer[T] {
c := CreateContainer[T]()
_ = c.ApplySilentA(o)
return c
}
func (c OptionContainer[T]) Set(k T, v any) OptionContainer[T] {
c[k] = v
return c
}
func (c OptionContainer[T]) Exist(k T) bool {
_, ok := c[k]
return ok
}
func (c OptionContainer[T]) Get(k T) any {
return c[k]
}
func (c OptionContainer[T]) Apply(opts ...Option[T]) error {
return c.ApplyA(opts)
}
func (c OptionContainer[T]) ApplySilent(opts ...Option[T]) error {
return c.ApplySilentA(opts)
}
func (c OptionContainer[T]) ApplyA(opts []Option[T]) error {
var err error
for _, opt := range opts {
if err = opt(c); err != nil {
return err
}
}
return err
}
func (c OptionContainer[T]) ApplySilentA(opts []Option[T]) error {
for _, opt := range opts {
opt(c)
}
return nil
}