25 lines
523 B
Go
25 lines
523 B
Go
package opt
|
|
|
|
// Func is a functional option for A
|
|
type Func[A any] func(*A)
|
|
|
|
// Apply applies all options to A
|
|
func Apply[A any](a *A, opts ...Func[A]) {
|
|
for _, opt := range opts {
|
|
opt(a)
|
|
}
|
|
}
|
|
|
|
// ErrorFunc is a functional option for A that can return an error
|
|
type ErrorFunc[A any] func(*A) error
|
|
|
|
// ApplyError applies all options to A, returning the first error
|
|
func ApplyError[A any](a *A, opts ...ErrorFunc[A]) error {
|
|
for _, opt := range opts {
|
|
if err := opt(a); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|