mirror of
https://github.com/nikoksr/notify.git
synced 2025-01-22 03:09:35 +02:00
feat(notify): add notifier options
Introducing Option functions. Options are meant to provide an extendable way of instanciating a Notifier with a set of options. Currently, only Enable()/Disable() are supported. WithOptions() alters an already instanciated Notifier and NewWithOptions() returns a new Notifier instance that was initialized with the given options. Options are passed in as a variadic list, thus, they're "optional".. no pun intended. Calling New() results in an internal call to NewWithOptions() without options passed, which again results in a default Notifier being returned.
This commit is contained in:
parent
ed8c8250f3
commit
20ec590e87
63
notify.go
63
notify.go
@ -1,27 +1,72 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
import "github.com/pkg/errors"
|
||||
|
||||
const defaultDisabled = false // Notifier is enabled by default
|
||||
// Compile-time check to ensure Notify implements Notifier.
|
||||
var _ Notifier = (*Notify)(nil)
|
||||
|
||||
// ErrSendNotification signals that the notifier failed to send a notification.
|
||||
var ErrSendNotification = errors.New("send notification")
|
||||
|
||||
// Notify is the central struct for managing notification services and sending messages to them.
|
||||
type Notify struct {
|
||||
Disabled bool
|
||||
notifiers []Notifier
|
||||
}
|
||||
|
||||
// ErrSendNotification signals that the notifier failed to send a notification.
|
||||
var ErrSendNotification = errors.New("send notification")
|
||||
// Option is a function that can be used to configure a Notify instance. It is used by the WithOptions and
|
||||
// NewWithOptions functions. It's a function because it's a bit more flexible than using a struct. The only required
|
||||
// parameter is the Notify instance itself.
|
||||
type Option func(*Notify)
|
||||
|
||||
// New returns a new instance of Notify. Defaulting to being not disabled.
|
||||
func New() *Notify {
|
||||
return &Notify{Disabled: defaultDisabled, notifiers: []Notifier{}}
|
||||
// Enable is an Option function that enables the Notify instance. This is the default behavior.
|
||||
func Enable(n *Notify) {
|
||||
if n != nil {
|
||||
n.Disabled = false
|
||||
}
|
||||
}
|
||||
|
||||
// Disable is an Option function that disables the Notify instance. It is enabled by default.
|
||||
func Disable(n *Notify) {
|
||||
if n != nil {
|
||||
n.Disabled = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithOptions applies the given options to the Notify instance. If no options are provided, it returns the Notify
|
||||
// instance unchanged.
|
||||
func (n *Notify) WithOptions(options ...Option) *Notify {
|
||||
if options == nil {
|
||||
return n
|
||||
}
|
||||
|
||||
for _, option := range options {
|
||||
if option != nil {
|
||||
option(n)
|
||||
}
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
// NewWithOptions returns a new instance of Notify with the given options. If no options are provided, it returns a new
|
||||
// Notify instance with default options. By default, the Notify instance is enabled.
|
||||
func NewWithOptions(options ...Option) *Notify {
|
||||
n := &Notify{
|
||||
Disabled: false, // Enabled by default.
|
||||
notifiers: make([]Notifier, 0), // Avoid nil list.
|
||||
}
|
||||
|
||||
return n.WithOptions(options...)
|
||||
}
|
||||
|
||||
// New returns a new instance of Notify. It returns a new Notify instance with default options. By default, the Notify
|
||||
// instance is enabled.
|
||||
func New() *Notify {
|
||||
return NewWithOptions()
|
||||
}
|
||||
|
||||
// Create the package level Notify instance.
|
||||
var std = New()
|
||||
|
||||
// Default returns the standard Notify instance used by the package-level send function.
|
||||
|
Loading…
x
Reference in New Issue
Block a user