undo

package
v1.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package undo contains abstractions for working with a stack of state changes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fn

type Fn func() error

Fn is a function with no arguments which returns an error or nil.

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack models a simple undo stack which consumers can use to store undo stateful functions, such as a function to teardown API state if something goes wrong during procedural commands, for example deleting a Fastly service after it's been created.

func NewStack

func NewStack() *Stack

NewStack constructs a new Stack.

func (*Stack) Len

func (s *Stack) Len() int

Len method returns the number of elements in the Stack.

func (*Stack) Pop

func (s *Stack) Pop() Fn

Pop method pops last added Fn element off the stack and returns it. If stack is empty Pop() returns nil.

func (*Stack) Push

func (s *Stack) Push(elem Fn)

Push method pushes an element onto the Stack.

func (*Stack) RunIfError

func (s *Stack) RunIfError(w io.Writer, err error)

RunIfError unwinds the stack if a non-nil error is passed, by serially calling each Fn function state in FIFO order. If any Fn returns an error, it gets logged to the provided writer. Should be deferrerd, such as:

undoStack := undo.NewStack()
defer func() { undoStack.RunIfError(w, err) }()

type Stacker

type Stacker interface {
	Pop() Fn
	Push(elem Fn)
	Len() int
	RunIfError(w io.Writer, err error)
}

Stacker represents the API of a Stack.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL