zntg/action.go

46 lines
923 B
Go
Raw Normal View History

package zntg
// Action is a method without arguments or return values.
type Action func()
// Err converts the Action to an ActionErr.
func (a Action) Err() ActionErr {
return func() error {
a()
return nil
}
}
// ActionErr is a method that only returns an error.
type ActionErr func() error
// Actions is a slice of ActionErr's.
type Actions []ActionErr
// Add adds an Action and returns the new list of Actions.
func (a Actions) Add(fn Action) Actions {
return a.AddErr(fn.Err())
}
// AddErr adds an ActionErr and return the new list of Actions.
func (a Actions) AddErr(fn ActionErr) Actions {
return append(a, fn)
}
// Do executes all actions.
func (a Actions) Do() {
for _, a := range a {
a()
}
}
// DoErr executes all actions but stops on the first action that returns an error.
func (a Actions) DoErr() error {
for _, a := range a {
if err := a(); err != nil {
return err
}
}
return nil
}