-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
43 lines (33 loc) · 671 Bytes
/
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
package fnoptions
import (
"errors"
pkgerrors "github.com/pkg/errors"
)
type Option[T any] func(*T) error
func (o Option[T]) ToMustOption() MustOption[T] {
return func(t *T) {
err := o(t)
if err != nil {
panic(err)
}
}
}
type MustOption[T any] func(*T)
func (o MustOption[T]) ToOption() Option[T] {
return func(t *T) error {
o(t)
return nil
}
}
func Apply[T any](input T, options ...Option[T]) error {
var errs []error
for _, o := range options {
errs = append(errs, o(&input))
}
return pkgerrors.WithStack(errors.Join(errs...))
}
func MustApply[T any](input T, options ...MustOption[T]) {
for _, o := range options {
o(&input)
}
}