Code-Fight commented on code in PR #805: URL: https://github.com/apache/incubator-seata-go/pull/805#discussion_r2059637296
########## pkg/saga/statemachine/engine/core/testdata/invalid.json: ########## @@ -0,0 +1,3 @@ +{ Review Comment: Please ensure all newly added files include a license header. ########## pkg/saga/statemachine/engine/core/default_statemachine_config_test.go: ########## @@ -0,0 +1,202 @@ +package core + +import ( + "errors" + "github.com/seata/seata-go/pkg/saga/statemachine/statelang" + "github.com/stretchr/testify/assert" + "io" + "path/filepath" + "testing" +) + +func TestDefaultStateMachineConfig_LoadValidJSON(t *testing.T) { + config := NewDefaultStateMachineConfig() + testFile := filepath.Join("testdata", "order_saga.json") + + err := config.LoadConfig(testFile) + assert.NoError(t, err, "加载 JSON 配置应成功") Review Comment: Please use English consistently.Please check other places as well and make the necessary changes. Thank you! ########## pkg/saga/statemachine/engine/invoker/invoker.go: ########## @@ -45,6 +62,132 @@ type ServiceInvokerManagerImpl struct { mutex sync.Mutex } +type LocalServiceInvoker struct { + serviceRegistry map[string]interface{} + methodCache map[string]*reflect.Method + jsonParser JsonParser + mutex sync.RWMutex +} + +func NewLocalServiceInvoker() *LocalServiceInvoker { + return &LocalServiceInvoker{ + serviceRegistry: make(map[string]interface{}), + methodCache: make(map[string]*reflect.Method), + jsonParser: &DefaultJsonParser{}, + } +} + +func (l *LocalServiceInvoker) RegisterService(serviceName string, instance interface{}) { + l.mutex.Lock() + defer l.mutex.Unlock() + l.serviceRegistry[serviceName] = instance +} + +func (l *LocalServiceInvoker) Invoke(ctx context.Context, input []any, service state.ServiceTaskState) ([]reflect.Value, error) { + serviceName := service.ServiceName() + instance, exists := l.serviceRegistry[serviceName] + if !exists { + return nil, fmt.Errorf("service %s not registered", serviceName) + } + + methodName := service.ServiceMethod() + method, err := l.getMethod(serviceName, methodName, service.ParameterTypes()) + if err != nil { + return nil, err + } + + params, err := l.resolveParameters(input, method.Type) + if err != nil { + return nil, err + } + + return l.invokeMethod(instance, method, params), nil +} + +func (l *LocalServiceInvoker) getMethod(serviceName, methodName string, paramTypes []string) (*reflect.Method, error) { + key := fmt.Sprintf("%s.%s", serviceName, methodName) + l.mutex.RLock() + if method, ok := l.methodCache[key]; ok { + l.mutex.RUnlock() + return method, nil + } + l.mutex.RUnlock() + Review Comment: For lock usage, it is recommended to use defer to ensure consistency. ` func xxx(){ l.mutex.RLock() defer l.mutex.RUnlock() xxxx xxxx } ` ########## pkg/saga/statemachine/engine/core/default_statemachine_config_test.go: ########## @@ -0,0 +1,202 @@ +package core Review Comment: Please add license header -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org