1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package component
- import (
- "errors"
- "git.sxidc.com/go-tools/utils/strutils"
- "sync"
- )
- type Component interface {
- GetType() string
- GetName() string
- Run(globalRunParams *GlobalRunParams, dynamicParams map[string]any) (any, error)
- }
- type Builder interface {
- ProductType() string
- Build(name string, buildParams map[string]any, runParams map[string]any) (Component, error)
- }
- var nodeBuilderMap = sync.Map{}
- func RegisterComponentBuilders(builders ...Builder) error {
- if builders == nil || len(builders) == 0 {
- return nil
- }
- for _, builder := range builders {
- nodeType := builder.ProductType()
- if strutils.IsStringEmpty(nodeType) {
- return errors.New("节点类型名称不能为空")
- }
- _, ok := nodeBuilderMap.Load(nodeType)
- if ok {
- return errors.New(nodeType + "的builder已注册")
- }
- nodeBuilderMap.Store(nodeType, builder)
- }
- return nil
- }
- func UnRegisterComponents(typeNames []string) {
- if typeNames == nil || len(typeNames) == 0 {
- return
- }
- for _, typeName := range typeNames {
- nodeBuilderMap.Delete(typeName)
- }
- }
- func BuildComponent(typeName string, name string, buildParams map[string]any, runParams map[string]any) (Component, error) {
- if strutils.IsStringEmpty(typeName) {
- return nil, errors.New("没有传递类型名称")
- }
- if strutils.IsStringEmpty(name) {
- return nil, errors.New("没有传递名称")
- }
- value, ok := nodeBuilderMap.Load(typeName)
- if !ok {
- return nil, errors.New(typeName + "未注册")
- }
- builder, ok := value.(Builder)
- if !ok {
- return nil, errors.New(typeName + "类型转换失败")
- }
- newComponent, err := builder.Build(name, buildParams, runParams)
- if err != nil {
- return nil, err
- }
- if newComponent == nil {
- return nil, errors.New(typeName + "的builder返回的对象为空")
- }
- return newComponent, nil
- }
|