aboutsummaryrefslogtreecommitdiff
path: root/maybedoer/maybedoer.go
blob: 0ae8a9011c2c353b92c0baf31b6f5f68e616b654 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Package maybedoer contains a pipeline of actions that might fail. If any action
// in the chain fails, no further actions take place and the error becomes the pipeline
// error.
package maybedoer

// Impl sequences a set of actions to be performed via calls to
// `Maybe` such that any previous error prevents new actions from being
// performed.
//
// This is, conceptually, just a go-ification of the Maybe monad.
type Impl struct {
	err error
}

// Maybe performs `f` if no previous call to a Maybe'd action resulted
// in an error
func (c *Impl) Maybe(f func() error) {
	if c.err == nil {
		c.err = f()
	}
}

// Error returns the first error encountered in the Error chain.
func (c *Impl) Error() error {
	return c.err
}