-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mark Sagi-Kazar <[email protected]>
- Loading branch information
1 parent
9aef7ab
commit 8f90bdf
Showing
2 changed files
with
21 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {}) | ||
} |