rara/packages/go/workflow/executor.go
toki fdc86c7ff4
Some checks are pending
ci / validate (push) Waiting to run
initial commit
2026-07-18 18:41:17 +09:00

56 lines
1.3 KiB
Go

package workflow
import (
"context"
"encoding/json"
"errors"
"sync"
)
var ErrExecutorNotFound = errors.New("workflow executor not found")
type Executor interface {
Execute(context.Context, json.RawMessage) (json.RawMessage, error)
}
type ExecutorFunc func(context.Context, json.RawMessage) (json.RawMessage, error)
func (function ExecutorFunc) Execute(ctx context.Context, input json.RawMessage) (json.RawMessage, error) {
return function(ctx, input)
}
type Registry struct {
mu sync.RWMutex
executors map[string]Executor
}
func NewRegistry() *Registry {
registry := &Registry{executors: make(map[string]Executor)}
registry.Register("noop", ExecutorFunc(func(_ context.Context, input json.RawMessage) (json.RawMessage, error) {
if len(input) == 0 {
return json.RawMessage(`{}`), nil
}
return input, nil
}))
return registry
}
func (registry *Registry) Register(operation string, executor Executor) {
registry.mu.Lock()
defer registry.mu.Unlock()
registry.executors[operation] = executor
}
func (registry *Registry) Execute(
ctx context.Context,
operation string,
input json.RawMessage,
) (json.RawMessage, error) {
registry.mu.RLock()
executor, exists := registry.executors[operation]
registry.mu.RUnlock()
if !exists {
return nil, ErrExecutorNotFound
}
return executor.Execute(ctx, input)
}