Home

opt @main - refs - log -
-
https://git.jolheiser.com/opt.git
Functional options
opt / opt.go
- raw -
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
}