goflow

package module
v0.0.1-beta Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: Apache-2.0 Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

goflow

golang工作流 支持脚本:lua

快速开始
package main

import (
	"fmt"

	"github.com/kappere/goflow"
)

func main() {
	// 节点图: n4 -> n3 -> n2 -> n1
	// 默认运行lua函数run(param),其中param为初始入参,前一节点运行结果会放入参数param["prev"]中
	// lua返回结果必须包含字段data(返回数据),success(是否运行成功),message(失败消息)

	// 创建4个节点:n1,n2,n3,n4
	n1 := goflow.NewNode([]*goflow.FlowNode{}, goflow.LuaScript{`
	function run(param)
		return {
			["data"] = "n1",
			["success"] = true,
			["message"] = "ok"
		};
	end
	`})
	n2 := goflow.NewNode([]*goflow.FlowNode{n1}, goflow.LuaScript{`
	function run(param)
		return {
			["data"] = "n2",
			["success"] = true,
			["message"] = "ok"
		};
	end
	`})
	n3 := goflow.NewNode([]*goflow.FlowNode{n2}, goflow.LuaScript{`
	function run(param)
		return {
			["data"] = "n3",
			["success"] = true,
			["message"] = "ok"
		};
	end
	`})
	n4 := goflow.NewNode([]*goflow.FlowNode{n3}, goflow.LuaScript{`
	function run(param)
		return {
			["data"] = "n4",
			["success"] = true,
			["message"] = "ok"
		};
	end
	`})
	// 创建flow,true参数表示node出现错误停止flow运行
	flow := goflow.NewFlow(n4, true)
	result := flow.Run(map[string]interface{}{
		"v1": "456",
	})
	fmt.Printf("n4: %v\n", result[n4].Data)
	fmt.Printf("n3: %v\n", result[n3].Data)
	fmt.Printf("n2: %v\n", result[n2].Data)
	fmt.Printf("n1: %v\n", result[n1].Data)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Flow

type Flow struct {
	Root         *FlowNode
	BreakOnError bool
}

func NewFlow

func NewFlow(root *FlowNode, breakOnError bool) *Flow

func (*Flow) Run

func (flow *Flow) Run(param map[string]interface{}) map[*FlowNode]*NodeResult

type FlowNode

type FlowNode struct {
	Prev   []*FlowNode
	Next   []*FlowNode
	Script Script
	In     int
}

func NewNode

func NewNode(next []*FlowNode, script Script) *FlowNode

type LuaScript

type LuaScript struct {
	Data string
}

func (LuaScript) Run

func (s LuaScript) Run(param map[string]interface{}) (result interface{}, err error)

func (LuaScript) String

func (s LuaScript) String() string

type NodeResult

type NodeResult struct {
	Data  interface{}
	Error error
}

type Script

type Script interface {
	String() string
	Run(map[string]interface{}) (interface{}, error)
}

Jump to

Keyboard shortcuts

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