flow

package module
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

README

flow

代码解耦神器,支持并行、串行、分支解耦,增强代码复用性

功能

  • 将你的业务逻辑定义成独立的ploy,通过ParallelFlow、PipeFlow、SwitchFlow将业务逻辑进行串联
  • ParallelFlow: 并行逻辑
  • PipeFlow:顺序串行逻辑
  • SwitchFlow:分支逻辑

快速上手

  • 这里通过一个购物场景介绍flow该如何使用。由于场景是强行怼的,所以请不要在意场景是否合理以及异常处理。
  • 更多更详细的用法参考examples
// 常规写法
func Buy(userId int32, orderId int32) (r error) {
	user, _ := model.GetUserById(userId)
	order, _ := model.GetOrderById(orderId)

	if reason := userAntiSpam(user) ; reason != nil {
		return fmt.Errorf("user exception, %s", reason)
	}
	if reason := orderAntiSpam(order); reason != nil {
		return fmt.Errorf("order exception, %s", reason)
	}

	switch order.ProductType {
	case "CAR":
		r = Settle(user, BuyCar(user, order))
	case "COMPUTER":
		r = Settle(user, BuyComputer(user, order))
	case "SNACKS":
		r = Settle(user, BuySnacks(user, order))
	}
	return
}

// flow 的写法
type PContext struct {
	flow.CoreContext
	User *User
	Order *Order
}

// PipeFlow 的用法,顺序运行填充user和验证user的ploy
userFlow := flow.NewPipeFlow().AddPloy(new(FillingUser), new(UserAntiSpam))
orderFlow := flow.NewPipeFlow().AddPloy(new(FillingOrder), new(OrderAntiSpam))

// SwitchFlow 的用法,根据OrderProductTypeSwitch来判断后续运行的flow
buyFlow := flow.NewSwitchFlow().
	SetSwitch(new(OrderProductTypeSwitch)).
	// Settle Ploy 的复用
	AddFlow("CAR", flow.NewPipeFlow().AddPloy(new(BuyCar), new(Settle))).
	AddFlow("COMPUTER", flow.NewPipeFlow().AddPloy(new(BuyCar), new(Settle))).
	AddFlow("SNACKS", flow.NewPipeFlow().AddPloy(new(BuySnacks), new(Settle)))

// 主flow,user和order并行获取和验证
// 根据产品类型运行不同的购买逻辑以及结算逻辑
mainFlow := flow.New().
	AddFlow(flow.NewParallelFlow().AddFlow(userFlow, orderFlow)).
	AddFlow(buyFlow)

ctx := &PContext{}
mainFlow.Run(ctx)

Documentation

Index

Constants

View Source
const DEFAULT_SWITCH_RUNNABLE = "default_switch_runnable"

Variables

View Source
var BreakError = errors.New("break error")

Functions

This section is empty.

Types

type CommonFlow

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

func NewCommonFlow

func NewCommonFlow() *CommonFlow

func (*CommonFlow) AddFlow

func (this *CommonFlow) AddFlow(flow Flow)

func (*CommonFlow) AddHook

func (this *CommonFlow) AddHook(hooks ...Hook)

func (*CommonFlow) AddPloy

func (this *CommonFlow) AddPloy(ploy Ploy)

type CoreContext

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

func (*CoreContext) AddError

func (this *CoreContext) AddError(err error)

func (*CoreContext) CleanErrors added in v0.2.6

func (this *CoreContext) CleanErrors()

func (*CoreContext) GetError

func (this *CoreContext) GetError() (r error)

func (*CoreContext) GetErrors

func (this *CoreContext) GetErrors() (r []error)

func (*CoreContext) HasError

func (this *CoreContext) HasError() (r bool)

func (*CoreContext) Lock

func (this *CoreContext) Lock()

func (*CoreContext) RLock

func (this *CoreContext) RLock()

func (*CoreContext) RUnLock

func (this *CoreContext) RUnLock()

func (*CoreContext) UnLock

func (this *CoreContext) UnLock()

type Flow

type Flow interface {
	Runnable
}

type FlowContext

type FlowContext interface {
	Lock()
	UnLock()
	RLock()
	RUnLock()
	AddError(err error)
	HasError() (r bool)
	GetError() (r error)
	GetErrors() (r []error)
	CleanErrors()
}

type Goto added in v0.2.0

type Goto interface {
	Run(ctx FlowContext) (r string, err error)
}

type GotoFlow added in v0.2.0

type GotoFlow struct {
	*CommonFlow
	// contains filtered or unexported fields
}

func NewGotoFlow added in v0.2.0

func NewGotoFlow() *GotoFlow

func (*GotoFlow) AddFlow added in v0.2.0

func (this *GotoFlow) AddFlow(cond string, flows ...Flow) *GotoFlow

func (*GotoFlow) AddHook added in v0.2.0

func (this *GotoFlow) AddHook(hooks ...Hook) *GotoFlow

func (*GotoFlow) AddPloy added in v0.2.0

func (this *GotoFlow) AddPloy(cond string, ploys ...Ploy) *GotoFlow

func (*GotoFlow) AddPloyFunc added in v0.2.0

func (this *GotoFlow) AddPloyFunc(cond string, fns ...func(ctx FlowContext)) *GotoFlow

func (*GotoFlow) Run added in v0.2.0

func (this *GotoFlow) Run(ctx FlowContext)

func (*GotoFlow) SetGoto added in v0.2.0

func (this *GotoFlow) SetGoto(flowGoto Goto) (r *GotoFlow)

func (*GotoFlow) SetGotoFunc added in v0.2.0

func (this *GotoFlow) SetGotoFunc(fn func(ctx FlowContext) (r string, err error)) (r *GotoFlow)

type GotoFunc added in v0.2.0

type GotoFunc func(ctx FlowContext) (r string, err error)

func (GotoFunc) Run added in v0.2.0

func (this GotoFunc) Run(ctx FlowContext) (r string, err error)

type Hook

type Hook interface {
	Before(ctx FlowContext, runable Runnable)
	After(ctx FlowContext, runable Runnable)
}

type ParallelFlow

type ParallelFlow struct {
	*CommonFlow
	// contains filtered or unexported fields
}

func NewParallelFlow

func NewParallelFlow() *ParallelFlow

func (*ParallelFlow) AddFlow

func (this *ParallelFlow) AddFlow(flows ...Flow) *ParallelFlow

func (*ParallelFlow) AddHook

func (this *ParallelFlow) AddHook(hooks ...Hook) *ParallelFlow

func (*ParallelFlow) AddPloy

func (this *ParallelFlow) AddPloy(ploys ...Ploy) *ParallelFlow

func (*ParallelFlow) AddPloyFunc

func (this *ParallelFlow) AddPloyFunc(fns ...func(ctx FlowContext)) *ParallelFlow

func (*ParallelFlow) Run

func (this *ParallelFlow) Run(ctx FlowContext)

func (*ParallelFlow) SetMaxProcess

func (this *ParallelFlow) SetMaxProcess(num int) (r *ParallelFlow)

type PipeFlow

type PipeFlow struct {
	*CommonFlow
}

func New

func New() *PipeFlow

func NewPipeFlow

func NewPipeFlow() *PipeFlow

func (*PipeFlow) AddFlow

func (this *PipeFlow) AddFlow(flows ...Flow) *PipeFlow

func (*PipeFlow) AddHook

func (this *PipeFlow) AddHook(hooks ...Hook) *PipeFlow

func (*PipeFlow) AddPloy

func (this *PipeFlow) AddPloy(ploys ...Ploy) *PipeFlow

func (*PipeFlow) AddPloyFunc

func (this *PipeFlow) AddPloyFunc(fns ...func(ctx FlowContext)) *PipeFlow

func (*PipeFlow) Run

func (this *PipeFlow) Run(ctx FlowContext)

type Ploy

type Ploy Runnable

type PloyFunc

type PloyFunc func(ctx FlowContext)

func (PloyFunc) Run

func (this PloyFunc) Run(ctx FlowContext)

type Runnable

type Runnable interface {
	Run(ctx FlowContext)
}

type Switch

type Switch interface {
	Run(ctx FlowContext) (r string, err error)
}

type SwitchFlow

type SwitchFlow struct {
	*CommonFlow
	// contains filtered or unexported fields
}

func NewSwitchFlow

func NewSwitchFlow() *SwitchFlow

func (*SwitchFlow) AddFlow

func (this *SwitchFlow) AddFlow(cond string, flows ...Flow) *SwitchFlow

func (*SwitchFlow) AddHook

func (this *SwitchFlow) AddHook(hooks ...Hook) *SwitchFlow

func (*SwitchFlow) AddPloy

func (this *SwitchFlow) AddPloy(cond string, ploys ...Ploy) *SwitchFlow

func (*SwitchFlow) AddPloyFunc

func (this *SwitchFlow) AddPloyFunc(cond string, fns ...func(ctx FlowContext)) *SwitchFlow

func (*SwitchFlow) Run

func (this *SwitchFlow) Run(ctx FlowContext)

func (*SwitchFlow) SetSwitch

func (this *SwitchFlow) SetSwitch(flowSwitch Switch) (r *SwitchFlow)

func (*SwitchFlow) SetSwitchFunc

func (this *SwitchFlow) SetSwitchFunc(fn func(ctx FlowContext) (r string, err error)) (r *SwitchFlow)

type SwitchFunc

type SwitchFunc func(ctx FlowContext) (r string, err error)

func (SwitchFunc) Run

func (this SwitchFunc) Run(ctx FlowContext) (r string, err error)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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