procedure

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FuncNameSetInput    = "set_input"
	FuncNameGetInput    = "get_input"
	FuncNameSetResult   = "set_result"
	FuncNameGetResult   = "get_result"
	FuncNameNewMap      = "new_map"
	FuncNameSetMapValue = "set_map_value"
	FuncNameLogInfo     = "info"
	FuncNameLogWarning  = "warning"
	FuncNameLogError    = "error"
)
View Source
const (
	CtxKeyNodeName = "__node_name"
)

Variables

View Source
var (
	ErrInvalidParameter  = errors.New("invalid parameter")
	ErrNodeExecuteFailed = errors.New("node execute failed")
)

Functions

func BindGHTTPRouter

func BindGHTTPRouter(ctx context.Context, group *ghttp.RouterGroup, lc LifeCycle, responseHandler func(r *ghttp.Request, res any, err error), proto ...*Proto) (err error)

func ExecuteNode

func ExecuteNode(ctx context.Context, root *Node, nlc NodeLifeCycle, input map[string]any, async ...bool) (output map[string]any, err error)

func SetMapValue

func SetMapValue(m any, index string, v any) any

Types

type GoFrameHTTPInputHandler

type GoFrameHTTPInputHandler struct{}

func (GoFrameHTTPInputHandler) HandleInput

func (g GoFrameHTTPInputHandler) HandleInput(ctx context.Context, proto *Proto) (input map[string]any, err error)

type LifeCycle

type LifeCycle interface {
	NodeLifeCycle
	// HandleInput defines how to handle input
	HandleInput(ctx context.Context, proto *Proto) (input map[string]any, err error)
	// HandleOutput defines how to handle output
	HandleOutput(ctx context.Context, proto *Proto, raw map[string]any) (out any, err error)
}

LifeCycle interface

type Meta

type Meta map[string]any

func (Meta) Get

func (m Meta) Get(key string, def ...any) (val *gvar.Var)

type Node

type Node struct {
	// Name of node, output will be overwritten if
	// using same name.
	Name string `json:"name"`
	// Meta data
	Meta Meta `json:"meta"`
	// Must execute success flag.
	// checks NodeLifeCycle.Execute return error
	Must bool `json:"must"`
	// Children nodes
	Children []*Node `json:"children"`
	// InputFilter of node, supported alias usage,
	// e.g. "aaa as bbb" empty means no filter,
	// all input will be passed
	InputFilter []string `json:"input_filter" yaml:"input_filter"`
	// Script in go template syntax
	Script Script `json:"script"`
	// contains filtered or unexported fields
}

Node of procedure, defines call flow and behavior. nodes will be executed in level order synchronously or asynchronously by NodeHandleFunc within input.

type NodeLifeCycle

type NodeLifeCycle interface {
	// BeforeExecute defines aspect before execute
	// input is copy of ExecuteNode input and filtered.
	BeforeExecute(ctx context.Context, node *Node, input map[string]any) (err error)
	// Execute defines how to execute a node.
	// input is copy of ExecuteNode input and
	// filtered. after execute, output will be
	// saved to ExecuteNode output in key node.Name
	Execute(ctx context.Context, node *Node, input map[string]any) (output any, err error)
	// AfterExecute defines aspect after execute.
	AfterExecute(ctx context.Context, node *Node, output any, err error)
	// BeforeScript defines parameters to build the
	// template.Template, returns the template.FuncMap
	// and the template execute input.
	// this method called after AfterExecute only if
	// node.Script is not empty.
	BeforeScript(ctx context.Context, node *Node, input map[string]any, output any) (fm template.FuncMap)
}

NodeLifeCycle defines node life cycle

type Parameter

type Parameter struct {
	// Name of parameter
	Name string `json:"name"`
	// Validate validator define of parameter
	Validate *ParameterValidate `json:"validate,omitempty"`
	// Meta of parameter
	Meta Meta `json:"meta"`
}

Parameter defines parameter

type ParameterValidate

type ParameterValidate struct {
	// validator rule
	// e.g. "required|length:6,16|same:password2"
	Rule string `json:"rule"`
	// error message accept map or string
	// e.g. "message": "xxx"
	// e.g. "message": {"required": "xxx", "same": "xxx"}
	Message any `json:"message"`
}

ParameterValidate defines parameter validate rules and error messages. see https://goframe.org/docs/core/gvalid

type Proto

type Proto struct {
	// LifeCycle of proto
	LifeCycle `json:"-" yaml:"-"`
	// Name of proto
	Name string `json:"name"`
	// Meta data
	Meta Meta `json:"meta"`
	// Parameters define
	Parameters []*Parameter `json:"parameters"`
	// Node of procedure
	Node *Node `json:"node"`
	// Async is a flag that indicates whether
	// the node is executed asynchronously
	Async bool `json:"async"`
	// Script in go template syntax
	Script Script `json:"script"`
	// contains filtered or unexported fields
}

Proto of procedure

func NewProtoFromYaml

func NewProtoFromYaml(data []byte) (p *Proto, err error)

func (*Proto) BuildValidator

func (p *Proto) BuildValidator() (v *gvalid.Validator)

func (*Proto) Check

func (p *Proto) Check(ctx context.Context) (err error)

func (*Proto) Execute

func (p *Proto) Execute(ctx context.Context) (out any, err error)

type Script

type Script string

Script in go template syntax

func (Script) Execute

func (s Script) Execute(fm template.FuncMap, data any) (res *gvar.Var, err error)

type ScriptExecuteResult

type ScriptExecuteResult struct {
	OK     bool           `json:"ok"`
	Error  string         `json:"error"`
	Result map[string]any `json:"result"`
}

Jump to

Keyboard shortcuts

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