suzushiro

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2019 License: MIT Imports: 5 Imported by: 0

README

suzushiro

Package suzushiro provides a simple pipeline pettern implementation.

Example

// implements 'A' and 'B' types on the interface 'Actor'
type A struct{}

func (a *A) GetName() string {
	return "A"
}

func (a *A) Run(context *Context) error {
	context.AddStr("Key", "A")

	return nil
}

func (a *A) OnSucceeded(context *Context)         {}
func (a *A) OnFailed(context *Context, err error) {}

type B struct {
	name string
}

func (b *B) GetName() string {
	return b.name
}

func (b *B) Run(context *Context) error {
	str, err := context.GetStr("Key")
	if err != nil {
		return err
	}

	context.AddStr("Key", str+b.name)

	return nil
}

func (b *B) OnSucceeded(context *Context)         {}
func (b *B) OnFailed(context *Context, err error) {}

func TestSuzushiro_Example(t *testing.T) {

	// supports anonymous functions via NewAction function.
	c := NewAction(
		"C",
		func(context *Context) error {
			str, err := context.GetStr("Key")
			if err != nil {
				return err
			}

			context.AddStr("Key", str+"C")
			return nil
		},
		nil,
		nil,
	)
	suzu := NewSuzushiro()
	actions := []Actor{
		&A{},
		&B{
			name: "B",
		},
		c,
	}

	suzu.AddLane("lane", actions)

	context := NewContext(true)
	done := make(chan interface{})
	suzu.RunLaneAsync(
		"lane",
		context,
		func(context *Context) {
			defer close(done)

			str, err := context.GetStr("Key")
			if err != nil {
				return
			}

			// 'str' is 'ABC'!
			assert.Equal(t, str, "ABC")
		},
		func(context *Context, err error) {
			defer close(done)

			assert.Fail(t, err.Error())
		},
	)

	<-done
}

Enjoy!

License

MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

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

func NewAction

func NewAction(name string, run func(*Context) error, onSucceeded func(*Context), onFailed func(*Context, error)) *Action

func (*Action) GetName

func (t *Action) GetName() string

func (*Action) OnFailed

func (t *Action) OnFailed(context *Context, err error)

func (*Action) OnSucceeded

func (t *Action) OnSucceeded(context *Context)

func (*Action) Run

func (t *Action) Run(context *Context) error

type Actor

type Actor interface {
	Run(*Context) error
	OnSucceeded(*Context)
	OnFailed(*Context, error)
	GetName() string
}

type Context

type Context struct {
	Logger *Logger
	// contains filtered or unexported fields
}

func NewContext

func NewContext(stdoutEnabled bool) *Context

func (*Context) AddInt

func (c *Context) AddInt(key string, value int)

func (*Context) AddObject

func (c *Context) AddObject(key string, value interface{})

func (*Context) AddStr

func (c *Context) AddStr(key string, value string)

func (*Context) GetInt

func (c *Context) GetInt(key string) (int, error)

func (*Context) GetObject

func (c *Context) GetObject(key string) (interface{}, error)

func (*Context) GetStr

func (c *Context) GetStr(key string) (string, error)

type Logger

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

func (*Logger) Log

func (logger *Logger) Log(format string, a ...interface{})

func (*Logger) String

func (logger *Logger) String() string

type Suzushiro

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

func NewSuzushiro

func NewSuzushiro() *Suzushiro

func (*Suzushiro) AddLane

func (s *Suzushiro) AddLane(name string, actions []Actor) error

func (*Suzushiro) GetRunningLaneCount

func (s *Suzushiro) GetRunningLaneCount(name string) int

func (*Suzushiro) RunLaneAsync

func (s *Suzushiro) RunLaneAsync(name string, context *Context, onSucceeded func(*Context), onFailed func(*Context, error)) error

Jump to

Keyboard shortcuts

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