kis-flow/function/kis_base_function.go

181 lines
3.8 KiB
Go
Raw Normal View History

2023-12-31 18:04:28 +08:00
package function
import (
"context"
"errors"
2024-04-16 15:11:23 +08:00
"sync"
2024-03-26 14:54:50 +08:00
"github.com/aceld/kis-flow/common"
"github.com/aceld/kis-flow/config"
"github.com/aceld/kis-flow/id"
"github.com/aceld/kis-flow/kis"
2023-12-31 18:04:28 +08:00
)
type BaseFunction struct {
2024-04-15 17:50:02 +08:00
// Id, the instance ID of KisFunction, used to differentiate different instance objects within KisFlow
2024-01-03 10:16:54 +08:00
Id string
2023-12-31 18:04:28 +08:00
Config *config.KisFuncConfig
2024-01-03 10:16:54 +08:00
// flow
2024-04-15 17:50:02 +08:00
flow kis.Flow // Context environment KisFlow
2024-01-09 17:30:58 +08:00
// connector
connector kis.Connector
2023-12-31 18:04:28 +08:00
2024-04-15 17:50:02 +08:00
// Custom temporary data of Function
2024-01-26 17:27:29 +08:00
metaData map[string]interface{}
2024-04-15 17:50:02 +08:00
// Manage the read-write lock of metaData
2024-01-26 17:27:29 +08:00
mLock sync.RWMutex
2024-01-03 10:16:54 +08:00
// link
2024-04-15 17:50:02 +08:00
N kis.Function // Next flow computing Function
P kis.Function // Previous flow computing Function
2023-12-31 18:04:28 +08:00
}
// Call
2024-04-15 17:50:02 +08:00
// BaseFunction is an empty implementation, designed to allow other specific types of KisFunction,
// such as KisFunction_V, to inherit BaseFuncion and override this method
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
}
2024-04-15 17:50:02 +08:00
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 {
2024-04-15 17:50:02 +08:00
// Function is the first node
return common.FunctionIDFirstVirtual
2023-12-31 18:04:28 +08:00
}
2024-04-15 17:50:02 +08:00
return base.P.GetID()
2023-12-31 18:04:28 +08:00
}
func (base *BaseFunction) GetNextId() string {
if base.N == nil {
2024-04-15 17:50:02 +08:00
// Function is the last node
return common.FunctionIDLastVirtual
2023-12-31 18:04:28 +08:00
}
2024-04-15 17:50:02 +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
}
2024-04-15 17:50:02 +08:00
// AddConnector adds a Connector to the current Function instance
2024-01-09 17:30:58 +08:00
func (base *BaseFunction) AddConnector(conn kis.Connector) error {
if conn == nil {
return errors.New("conn is nil")
}
base.connector = conn
return nil
}
2024-04-15 17:50:02 +08:00
// GetConnector gets the Connector associated with the current Function instance
2024-01-09 17:30:58 +08:00
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() {
2024-04-15 17:50:02 +08:00
base.Id = id.KisID(common.KisIDTypeFunction)
2023-12-31 18:04:28 +08:00
}
2024-01-01 17:49:27 +08:00
2024-04-15 17:50:02 +08:00
// NewKisFunction creates a new NsFunction
// flow: the current belonging flow instance
// s: the configuration strategy of the current function
2024-01-01 17:49:27 +08:00
func NewKisFunction(flow kis.Flow, config *config.KisFuncConfig) kis.Function {
var f kis.Function
2024-04-15 17:50:02 +08:00
// Factory produces generic objects
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:
2024-04-15 17:50:02 +08:00
// LOG ERROR
2024-01-01 17:49:27 +08:00
return nil
}
2024-04-15 17:50:02 +08:00
// Generate a random unique instance ID
2024-01-03 10:16:54 +08:00
f.CreateId()
2024-04-15 17:50:02 +08:00
// Set basic information attributes
2024-01-01 17:49:27 +08:00
if err := f.SetConfig(config); err != nil {
panic(err)
}
2024-04-15 17:50:02 +08:00
// Set 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
2024-04-15 17:50:02 +08:00
// GetMetaData gets the temporary data of the current Function
2024-01-26 17:27:29 +08:00
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
}
2024-04-15 17:50:02 +08:00
// SetMetaData sets the temporary data of the current Function
2024-01-26 17:27:29 +08:00
func (base *BaseFunction) SetMetaData(key string, value interface{}) {
base.mLock.Lock()
defer base.mLock.Unlock()
base.metaData[key] = value
}