kis-flow/function/kis_base_function.go

179 lines
3.5 KiB
Go
Raw Normal View History

2023-12-31 18:04:28 +08:00
package function
import (
"context"
"errors"
"kis-flow/common"
"kis-flow/config"
"kis-flow/id"
2024-01-01 17:49:27 +08:00
"kis-flow/kis"
2024-01-26 17:27:29 +08:00
"sync"
2023-12-31 18:04:28 +08:00
)
type BaseFunction struct {
2024-01-03 10:16:54 +08:00
// Id , KisFunction的实例ID用于KisFlow内部区分不同的实例对象
Id string
2023-12-31 18:04:28 +08:00
Config *config.KisFuncConfig
2024-01-03 10:16:54 +08:00
// flow
2024-01-09 17:30:58 +08:00
flow kis.Flow //上下文环境KisFlow
// connector
connector kis.Connector
2023-12-31 18:04:28 +08:00
2024-01-26 17:27:29 +08:00
// Function的自定义临时数据
metaData map[string]interface{}
// 管理metaData的读写锁
mLock sync.RWMutex
2024-01-03 10:16:54 +08:00
// link
2024-01-01 17:49:27 +08:00
N kis.Function //下一个流计算Function
P kis.Function //上一个流计算Function
2023-12-31 18:04:28 +08:00
}
// Call
// BaseFunction 为空实现目的为了让其他具体类型的KisFunction如KisFunction_V 来继承BaseFuncion来重写此方法
2024-01-01 17:49:27 +08:00
func (base *BaseFunction) Call(ctx context.Context, flow kis.Flow) error { return nil }
2023-12-31 18:04:28 +08:00
2024-01-01 17:49:27 +08:00
func (base *BaseFunction) Next() kis.Function {
2023-12-31 18:04:28 +08:00
return base.N
}
2024-01-01 17:49:27 +08:00
func (base *BaseFunction) Prev() kis.Function {
2023-12-31 18:04:28 +08:00
return base.P
}
2024-01-01 17:49:27 +08:00
func (base *BaseFunction) SetN(f kis.Function) {
2023-12-31 18:04:28 +08:00
base.N = f
}
2024-01-01 17:49:27 +08:00
func (base *BaseFunction) SetP(f kis.Function) {
2023-12-31 18:04:28 +08:00
base.P = f
}
func (base *BaseFunction) SetConfig(s *config.KisFuncConfig) error {
if s == nil {
return errors.New("KisFuncConfig is nil")
}
base.Config = s
return nil
}
func (base *BaseFunction) GetId() string {
2024-01-03 10:16:54 +08:00
return base.Id
2023-12-31 18:04:28 +08:00
}
func (base *BaseFunction) GetPrevId() string {
if base.P == nil {
//Function为首结点
return common.FunctionIdFirstVirtual
}
2024-01-03 10:16:54 +08:00
return base.P.GetId()
2023-12-31 18:04:28 +08:00
}
func (base *BaseFunction) GetNextId() string {
if base.N == nil {
//Function为尾结点
return common.FunctionIdLastVirtual
}
2024-01-03 10:16:54 +08:00
return base.N.GetId()
2023-12-31 18:04:28 +08:00
}
func (base *BaseFunction) GetConfig() *config.KisFuncConfig {
return base.Config
}
2024-01-01 17:49:27 +08:00
func (base *BaseFunction) SetFlow(f kis.Flow) error {
2023-12-31 18:04:28 +08:00
if f == nil {
return errors.New("KisFlow is nil")
}
2024-01-09 17:30:58 +08:00
base.flow = f
2023-12-31 18:04:28 +08:00
return nil
}
2024-01-01 17:49:27 +08:00
func (base *BaseFunction) GetFlow() kis.Flow {
2024-01-09 17:30:58 +08:00
return base.flow
}
// AddConnector 给当前Function实例添加一个Connector
func (base *BaseFunction) AddConnector(conn kis.Connector) error {
if conn == nil {
return errors.New("conn is nil")
}
base.connector = conn
return nil
}
// GetConnector 获取当前Function实例所关联的Connector
func (base *BaseFunction) GetConnector() kis.Connector {
return base.connector
2023-12-31 18:04:28 +08:00
}
2024-01-03 10:16:54 +08:00
func (base *BaseFunction) CreateId() {
base.Id = id.KisID(common.KisIdTypeFunction)
2023-12-31 18:04:28 +08:00
}
2024-01-01 17:49:27 +08:00
// NewKisFunction 创建一个NsFunction
// flow: 当前所属的flow实例
// s : 当前function的配置策略
func NewKisFunction(flow kis.Flow, config *config.KisFuncConfig) kis.Function {
var f kis.Function
//工厂生产泛化对象
2024-01-03 10:16:54 +08:00
switch common.KisMode(config.FMode) {
2024-01-01 17:49:27 +08:00
case common.V:
2024-01-26 17:27:29 +08:00
f = NewKisFunctionV()
2024-01-01 17:49:27 +08:00
case common.S:
2024-01-26 17:27:29 +08:00
f = NewKisFunctionS()
2024-01-01 17:49:27 +08:00
case common.L:
2024-01-26 17:27:29 +08:00
f = NewKisFunctionL()
2024-01-01 17:49:27 +08:00
case common.C:
2024-01-26 17:27:29 +08:00
f = NewKisFunctionC()
2024-01-01 17:49:27 +08:00
case common.E:
2024-01-26 17:27:29 +08:00
f = NewKisFunctionE()
2024-01-01 17:49:27 +08:00
default:
//LOG ERROR
return nil
}
2024-01-03 10:16:54 +08:00
// 生成随机实例唯一ID
f.CreateId()
2024-01-09 17:30:58 +08:00
// 设置基础信息属性
2024-01-01 17:49:27 +08:00
if err := f.SetConfig(config); err != nil {
panic(err)
}
2024-01-09 17:30:58 +08:00
// 设置Flow
2024-01-01 17:49:27 +08:00
if err := f.SetFlow(flow); err != nil {
panic(err)
}
return f
}
2024-01-26 17:27:29 +08:00
// GetMetaData 得到当前Function的临时数据
func (base *BaseFunction) GetMetaData(key string) interface{} {
base.mLock.RLock()
defer base.mLock.RUnlock()
data, ok := base.metaData[key]
if !ok {
return nil
}
return data
}
// SetMetaData 设置当前Function的临时数据
func (base *BaseFunction) SetMetaData(key string, value interface{}) {
base.mLock.Lock()
defer base.mLock.Unlock()
base.metaData[key] = value
}