Skip to content

Commit

Permalink
refactor!: make Option an interface
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <[email protected]>
  • Loading branch information
sagikazarmark committed May 11, 2021
1 parent 9aef7ab commit 8f90bdf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion health.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func New(opts ...Option) Health {
lock: sync.RWMutex{},
}
for _, opt := range append(opts, WithDefaults()) {
opt(h)
opt.apply(h)
}
return h
}
Expand Down
26 changes: 20 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
package gosundheit

type Option func(*health)
// Option configures a health checker using the functional options paradigm
// popularized by Rob Pike and Dave Cheney.
// If you're unfamiliar with this style, see:
// - https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html
// - https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis.
// - https://sagikazarmark.hu/blog/functional-options-on-steroids/
type Option interface {
apply(*health)
}

type optionFunc func(*health)

func (fn optionFunc) apply(h *health) {
fn(h)
}

// WithCheckListeners allows you to listen to check start/end events
func WithCheckListeners(listener ...CheckListener) Option {
return func(h *health) {
return optionFunc(func(h *health) {
h.checksListener = listener
}
})
}

// WithHealthListeners allows you to listen to overall results change
func WithHealthListeners(listener ...HealthListener) Option {
return func(h *health) {
return optionFunc(func(h *health) {
h.healthListener = listener
}
})
}

// WithDefaults sets all the Health object settings. It's not required to use this as no options is always default
// This is a simple placeholder for any future defaults
func WithDefaults() Option {
return func(h *health) {}
return optionFunc(func(h *health) {})
}

0 comments on commit 8f90bdf

Please sign in to comment.