@@ -30,14 +30,12 @@ import (
3030 "strings"
3131 "sync"
3232
33- "github.com/seata/seata-go/pkg/saga/statemachine"
3433 "github.com/seata/seata-go/pkg/saga/statemachine/engine"
3534 "github.com/seata/seata-go/pkg/saga/statemachine/engine/expr"
3635 "github.com/seata/seata-go/pkg/saga/statemachine/engine/invoker"
3736 "github.com/seata/seata-go/pkg/saga/statemachine/engine/repo"
3837 "github.com/seata/seata-go/pkg/saga/statemachine/engine/sequence"
3938 "github.com/seata/seata-go/pkg/saga/statemachine/process_ctrl"
40- "github.com/seata/seata-go/pkg/saga/statemachine/statelang/parser"
4139 "github.com/seata/seata-go/pkg/saga/statemachine/store"
4240)
4341
@@ -62,9 +60,6 @@ type DefaultStateMachineConfig struct {
6260 rmReportSuccessEnable bool
6361 stateMachineResources []string
6462
65- // State machine definitions
66- stateMachineDefs map [string ]* statemachine.StateMachineObject
67-
6863 // Components
6964 processController process_ctrl.ProcessController
7065
@@ -284,8 +279,8 @@ func (c *DefaultStateMachineConfig) SetRmReportSuccessEnable(rmReportSuccessEnab
284279 c .rmReportSuccessEnable = rmReportSuccessEnable
285280}
286281
287- func (c * DefaultStateMachineConfig ) GetStateMachineDefinition ( name string ) * statemachine. StateMachineObject {
288- return c . stateMachineDefs [ name ]
282+ func (c * DefaultStateMachineConfig ) GetStateMachineConfig () engine. StateMachineConfig {
283+ return c
289284}
290285
291286func (c * DefaultStateMachineConfig ) GetExpressionFactory (expressionType string ) expr.ExpressionFactory {
@@ -364,28 +359,23 @@ func (c *DefaultStateMachineConfig) LoadConfig(configPath string) error {
364359 return fmt .Errorf ("failed to read config file: path=%s, error=%w" , configPath , err )
365360 }
366361
367- parser := parser .NewStateMachineConfigParser ()
368- smo , err := parser .Parse (content )
369- if err != nil {
370- return fmt .Errorf ("failed to parse state machine definition: path=%s, error=%w" , configPath , err )
371- }
372-
373362 var configFileParams ConfigFileParams
374- if err := json .Unmarshal (content , & configFileParams ); err != nil {
363+ ext := strings .ToLower (filepath .Ext (configPath ))
364+
365+ switch ext {
366+ case ".json" :
367+ if err := json .Unmarshal (content , & configFileParams ); err != nil {
368+ return fmt .Errorf ("failed to unmarshal config file as JSON: %w" , err )
369+ }
370+ case ".yaml" , ".yml" :
375371 if err := yaml .Unmarshal (content , & configFileParams ); err != nil {
376372 return fmt .Errorf ("failed to unmarshal config file as YAML: %w" , err )
377- } else {
378- c .applyConfigFileParams (& configFileParams )
379373 }
380- } else {
381- c .applyConfigFileParams (& configFileParams )
382- }
383-
384- if _ , exists := c .stateMachineDefs [smo .Name ]; exists {
385- return fmt .Errorf ("state machine definition with name %s already exists" , smo .Name )
374+ default :
375+ return fmt .Errorf ("unsupported config file type: path=%s, ext=%s (only .json/.yaml/.yml are supported)" , configPath , ext )
386376 }
387- c .stateMachineDefs [smo .Name ] = smo
388377
378+ c .applyConfigFileParams (& configFileParams )
389379 return nil
390380}
391381
@@ -517,6 +507,14 @@ func (c *DefaultStateMachineConfig) initServiceInvokers() error {
517507 c .RegisterServiceInvoker ("func" , invoker .NewFuncInvoker ())
518508 }
519509
510+ if c .scriptInvokerManager == nil {
511+ c .scriptInvokerManager = invoker .NewScriptInvokerManager ()
512+ }
513+
514+ if jsInvoker , err := c .scriptInvokerManager .GetInvoker ("javascript" ); err != nil || jsInvoker == nil {
515+ c .scriptInvokerManager .RegisterInvoker (invoker .NewJavaScriptScriptInvoker ())
516+ }
517+
520518 return nil
521519}
522520
@@ -533,6 +531,10 @@ func (c *DefaultStateMachineConfig) Validate() error {
533531 errs = append (errs , fmt .Errorf ("service invoker manager is nil" ))
534532 }
535533
534+ if c .scriptInvokerManager == nil {
535+ errs = append (errs , fmt .Errorf ("script invoker manager is nil" ))
536+ }
537+
536538 if c .transOperationTimeout <= 0 {
537539 errs = append (errs , fmt .Errorf ("invalid trans operation timeout: %d" , c .transOperationTimeout ))
538540 }
@@ -605,7 +607,7 @@ func (c *DefaultStateMachineConfig) EvaluateExpression(expressionStr string, con
605607 return result , nil
606608}
607609
608- func NewDefaultStateMachineConfig (opts ... Option ) * DefaultStateMachineConfig {
610+ func NewDefaultStateMachineConfig (opts ... Option ) ( * DefaultStateMachineConfig , error ) {
609611 ctx := context .Background ()
610612 defaultBP := process_ctrl .NewBusinessProcessor ()
611613
@@ -619,7 +621,6 @@ func NewDefaultStateMachineConfig(opts ...Option) *DefaultStateMachineConfig {
619621 sagaCompensatePersistModeUpdate : DefaultClientSagaCompensatePersistModeUpdate ,
620622 sagaBranchRegisterEnable : DefaultClientSagaBranchRegisterEnable ,
621623 rmReportSuccessEnable : DefaultClientReportSuccessEnable ,
622- stateMachineDefs : make (map [string ]* statemachine.StateMachineObject ),
623624 componentLock : & sync.Mutex {},
624625 seqGenerator : sequence .NewUUIDSeqGenerator (),
625626 statusDecisionStrategy : strategy .NewDefaultStatusDecisionStrategy (),
@@ -648,13 +649,11 @@ func NewDefaultStateMachineConfig(opts ...Option) *DefaultStateMachineConfig {
648649 opt (c )
649650 }
650651
651- if err := c .LoadConfig ("config.yaml" ); err == nil {
652- log .Printf ("Successfully loaded config from config.yaml" )
653- } else {
654- log .Printf ("Failed to load config file (using default/env values): %v" , err )
652+ if err := c .Init (); err != nil {
653+ return nil , fmt .Errorf ("failed to initialize state machine config: %w" , err )
655654 }
656655
657- return c
656+ return c , nil
658657}
659658
660659func parseEnvResources () []string {
@@ -732,3 +731,22 @@ func WithStateMachineRepository(machineRepo repo.StateMachineRepository) Option
732731 c .stateMachineRepository = machineRepo
733732 }
734733}
734+
735+ func WithConfigPath (path string ) Option {
736+ return func (c * DefaultStateMachineConfig ) {
737+ if path == "" {
738+ return
739+ }
740+ if err := c .LoadConfig (path ); err != nil {
741+ log .Printf ("Failed to load config from %s: %v" , path , err )
742+ } else {
743+ log .Printf ("Successfully loaded config from %s" , path )
744+ }
745+ }
746+ }
747+
748+ func WithScriptInvokerManager (scriptManager invoker.ScriptInvokerManager ) Option {
749+ return func (c * DefaultStateMachineConfig ) {
750+ c .scriptInvokerManager = scriptManager
751+ }
752+ }
0 commit comments