From 4f9a6ec6f3ed81e070fdbcbfa07d7b1fad93c258 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Sun, 24 Apr 2022 17:36:31 +0000 Subject: [PATCH] maybedoer Signed-off-by: Xe Iaso --- maybedoer/maybedoer.go | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 maybedoer/maybedoer.go diff --git a/maybedoer/maybedoer.go b/maybedoer/maybedoer.go new file mode 100644 index 0000000..d824b56 --- /dev/null +++ b/maybedoer/maybedoer.go @@ -0,0 +1,56 @@ +// 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 + +import "context" + +// Doer is a function that implements a fallible action that can be done. +type Doer func(context.Context) error + +// 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 monoid, but +// defined to the error type in Go. +type Impl struct { + Doers []Doer + err error +} + +// Do executes the list of doers, right-folding the functions and seeing if one +// returns an error. This is semantically identical to Data.Monoid.First in +// Haskell, but specific to the error type in Go. Ideally this could be generalized +// to any pointer-like datatype in Go, but Rob Pike says we can't have nice things. +// +// See the Haskell documentation for Data.Monad.First for more information: +// http://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Monoid.html#t:First +func (c *Impl) Do(ctx context.Context) error { + for _, doer := range c.Doers { + select { + case <-ctx.Done(): + return ctx.Err() + default: + c.Maybe(ctx, doer) + if c.err != nil { + return c.err + } + } + } + + return nil +} + +// Maybe performs `f` if no previous call to a Maybe'd action resulted +// in an error +func (c *Impl) Maybe(ctx context.Context, f func(ctx context.Context) error) { + if c.err == nil { + c.err = f(ctx) + } +} + +// Error returns the first error encountered in the Error chain. +func (c *Impl) Error() error { + return c.err +}