Documentation
¶
Index ¶
- Variables
- func ApplyTransitionByNameWithHistory(wf *workflow.Workflow, transitionName string, ...) error
- func ApplyTransitionWithHistory(wf *workflow.Workflow, to []workflow.Place, historyStore history.HistoryStore, ...) error
- func RegisterStorageBuilder(builder StorageConfigBuilder)
- func ResolveTemplateValue(value any, ctx context.Context, wf *workflow.Workflow) any
- func SetupStorageBuilders(storageConfig *StorageConfig, dbProvider func() (*sql.DB, error)) error
- func WithTemplateValue(ctx context.Context, key string, value any) context.Context
- type Config
- type Connection
- type ConnectionProvider
- type InitialMarkingConfig
- type Loader
- type PlaceConfig
- type SQLConnection
- type SQLiteStorageBuilder
- type StorageConfig
- type StorageConfigBuilder
- type StorageFactory
- type StorageInit
- type StorageSetupResult
- type TransitionConfig
- type WorkflowConfig
Constants ¶
This section is empty.
Variables ¶
var DefaultFactory = NewStorageFactory()
DefaultFactory is the default global factory instance.
Functions ¶
func ApplyTransitionByNameWithHistory ¶
func ApplyTransitionByNameWithHistory( wf *workflow.Workflow, transitionName string, historyStore history.HistoryStore, ctx context.Context, overrideNotes string, overrideActor string, overrideCustomFields map[string]any, ) error
ApplyTransitionByNameWithHistory applies a transition by name and saves history using metadata from the transition. This is the recommended approach for new code as it avoids ambiguity when multiple transitions lead to the same destination.
func ApplyTransitionWithHistory ¶
func ApplyTransitionWithHistory( wf *workflow.Workflow, to []workflow.Place, historyStore history.HistoryStore, ctx context.Context, overrideNotes string, overrideActor string, overrideCustomFields map[string]any, ) error
ApplyTransitionWithHistory applies a transition and saves history using metadata from the transition. This is the legacy version that uses target places. For new code, prefer ApplyTransitionByNameWithHistory.
func RegisterStorageBuilder ¶
func RegisterStorageBuilder(builder StorageConfigBuilder)
RegisterStorageBuilder registers a storage builder with the default factory.
func ResolveTemplateValue ¶
ResolveTemplateValue resolves template variables in custom field values. Supports:
- "now()" → current timestamp
- "{{variable}}" → value from context
- "{{object.property}}" → nested property access
- Literal strings without templates are returned as-is
func SetupStorageBuilders ¶
func SetupStorageBuilders(storageConfig *StorageConfig, dbProvider func() (*sql.DB, error)) error
SetupStorageBuilders registers storage builders based on the storage configuration. This allows the application to automatically register the required builders without explicitly knowing which storage type is being used.
Currently supports:
- "sqlite": Registers SQLiteStorageBuilder
If dbProvider is provided, it will be used for SQLite connections. If nil, SQLite builder will create connections from config.
func WithTemplateValue ¶
WithTemplateValue stores a value in context using a string key for template resolution. Template resolution requires string keys because template variables like {{reason}} extract the variable name as a string, and Go's context.Value() requires exact type matching.
Note: This function intentionally uses string keys (SA1029 warning) because template resolution extracts variable names as strings from syntax like {{variable}}.
Usage:
ctx = yaml.WithTemplateValue(ctx, "reason", "value")
ctx = yaml.WithTemplateValue(ctx, "request", map[string]any{"ip": "192.168.1.1"})
For non-template context values, you can use custom type keys following Go best practices:
type myKey string
ctx = context.WithValue(ctx, myKey("data"), value)
Types ¶
type Config ¶
type Config struct {
Workflow WorkflowConfig `yaml:"workflow"`
Storage *StorageConfig `yaml:"storage,omitempty"`
}
Config represents the complete YAML workflow configuration.
func LoadConfig ¶
LoadConfig loads a workflow configuration from a YAML file.
func LoadConfigFromBytes ¶
LoadConfigFromBytes loads a workflow configuration from YAML bytes.
Decoding is strict: any key that is not part of the schema causes an error (reported with its line number) rather than being silently ignored. This prevents typos and not-yet-implemented features from being accepted and then quietly dropped. Colored Petri Net tokens are declared with the polymorphic initial_marking key (see WorkflowConfig.InitialMarking).
type Connection ¶
type Connection interface {
// Underlying returns the underlying database connection object.
// For SQL databases, this returns *sql.DB.
// For NoSQL databases, this returns the appropriate client (e.g., *mongo.Client, *redis.Client).
// Returns nil if no connection is needed or available.
Underlying() any
}
Connection represents a database connection that can be any type (SQL, NoSQL, etc.). The Underlying() method returns the concrete connection object. This allows the system to work with any database type without being tied to SQL.
Example implementations:
- SQLConnection: wraps *sql.DB for SQL databases (sqlite, postgres, cockroachdb, etc.)
- MongoDBConnection: wraps *mongo.Client for MongoDB
- RedisConnection: wraps *redis.Client for Redis
- DynamoDBConnection: wraps *dynamodb.Client for AWS DynamoDB
type ConnectionProvider ¶
type ConnectionProvider func() (Connection, error)
ConnectionProvider is a function that provides a Connection interface. This allows different storage types to return their appropriate connection type.
func SetupStorageBuildersFromConfig ¶
func SetupStorageBuildersFromConfig(storageConfig *StorageConfig) (ConnectionProvider, error)
SetupStorageBuildersFromConfig automatically sets up storage builders based on the YAML config. It registers the appropriate builders for the storage type.
For SQL databases (sqlite, postgres, cockroachdb, etc.):
- Returns a ConnectionProvider that returns a SQLConnection wrapping *sql.DB
- Registers the SQL-specific builder
For NoSQL databases (mongodb, redis, dynamodb, etc.):
- Returns nil (custom builders should be registered manually)
- Custom builders can implement their own Connection types
Returns:
- connectionProvider: For supported databases, a function returning Connection. For others, nil.
- error: If storage type setup fails
type InitialMarkingConfig ¶
type InitialMarkingConfig struct {
// Places maps each initial place to its colored tokens (nil = presence token).
Places map[string][]map[string]any
}
InitialMarkingConfig declares a workflow's starting marking. It accepts three YAML forms, so the simple case stays a one-liner and the Colored Petri Net case is a single coherent construct:
initial_marking: draft # one place, an uncolored presence token
initial_marking: [draft, needs_legal] # several presence places
initial_marking: # data-carrying (colored) tokens per place
pending:
- {order_id: "001", amount: 100}
- {order_id: "002", amount: 250}
A place with a nil/empty token list gets a single uncolored presence token (the boolean case); a place with tokens is seeded with exactly those tokens.
func (*InitialMarkingConfig) UnmarshalYAML ¶
func (im *InitialMarkingConfig) UnmarshalYAML(value *yaml.Node) error
UnmarshalYAML accepts a scalar place name, a sequence of place names, or a mapping of place name to token list.
type Loader ¶
type Loader struct {
// EnvBuilder allows customizing the expression evaluation environment
EnvBuilder func(workflow.Event) map[string]any
}
Loader provides functionality to load workflow definitions from YAML configuration.
func NewLoaderWithEnv ¶
NewLoaderWithEnv creates a new YAML loader with a custom environment builder for expressions.
func (*Loader) LoadDefinition ¶
func (l *Loader) LoadDefinition(config *Config) (*workflow.Definition, error)
LoadDefinition loads a workflow definition from a YAML configuration.
type PlaceConfig ¶
type PlaceConfig struct {
Name string `yaml:"name"`
Metadata map[string]any `yaml:"metadata,omitempty"`
}
PlaceConfig defines a place with optional metadata.
type SQLConnection ¶
type SQLConnection struct {
// contains filtered or unexported fields
}
SQLConnection wraps a SQL database connection (*sql.DB). Use this for SQL databases like SQLite, PostgreSQL, MySQL, CockroachDB, etc.
func (*SQLConnection) DB ¶
func (c *SQLConnection) DB() *sql.DB
DB returns the underlying *sql.DB connection. This provides type-safe access to the database connection.
func (*SQLConnection) Underlying ¶
func (c *SQLConnection) Underlying() any
Underlying returns the underlying *sql.DB connection. This implements the Connection interface for polymorphic use.
type SQLiteStorageBuilder ¶
type SQLiteStorageBuilder struct {
// DBProvider is a function that provides a *sql.DB instance.
// This allows the builder to work with existing database connections.
DBProvider func() (*sql.DB, error)
}
SQLiteStorageBuilder implements StorageConfigBuilder for SQLite storage.
func NewSQLiteStorageBuilder ¶
func NewSQLiteStorageBuilder(dbProvider func() (*sql.DB, error)) *SQLiteStorageBuilder
NewSQLiteStorageBuilder creates a new SQLite storage builder. dbProvider is a function that returns a *sql.DB instance. If nil, the builder will try to create a connection from config.
func (*SQLiteStorageBuilder) Build ¶
func (b *SQLiteStorageBuilder) Build(config map[string]any) (workflow.Storage, *StorageInit, error)
Build creates a SQLite storage instance from YAML configuration.
func (*SQLiteStorageBuilder) Type ¶
func (b *SQLiteStorageBuilder) Type() string
Type returns "sqlite".
type StorageConfig ¶
type StorageConfig struct {
Type string `yaml:"type"` // "sqlite", "postgres", "mongodb", "redis", etc.
// Raw configuration data - structure depends on storage type
// This allows each storage implementation to define its own config structure
Config map[string]any `yaml:",inline"`
}
StorageConfig defines generic storage configuration. The actual structure is storage-type specific and handled by StorageConfigBuilder implementations. This uses a custom unmarshaler to capture all fields except "type" into the Config map.
func (*StorageConfig) ToMap ¶
func (sc *StorageConfig) ToMap() map[string]any
ToMap converts StorageConfig to a raw map for builder consumption.
func (*StorageConfig) UnmarshalYAML ¶
func (sc *StorageConfig) UnmarshalYAML(unmarshal func(any) error) error
UnmarshalYAML implements custom YAML unmarshaling to capture all fields.
type StorageConfigBuilder ¶
type StorageConfigBuilder interface {
// Type returns the storage type identifier (e.g., "sqlite", "postgres", "mongodb", "redis")
Type() string
// Build creates a workflow.Storage instance from the raw YAML configuration data.
// The config parameter contains the raw YAML data for the storage section.
// Returns the Storage instance and any initialization schema/commands needed.
Build(config map[string]any) (workflow.Storage, *StorageInit, error)
}
StorageConfigBuilder is an interface for building storage instances from YAML configuration. Each storage implementation (SQLite, PostgreSQL, MongoDB, Redis, etc.) should implement this interface.
type StorageFactory ¶
type StorageFactory struct {
// contains filtered or unexported fields
}
StorageFactory manages storage config builders and creates storage instances from YAML config.
func NewStorageFactory ¶
func NewStorageFactory() *StorageFactory
NewStorageFactory creates a new storage factory.
func (*StorageFactory) Build ¶
func (f *StorageFactory) Build(config *StorageConfig) (workflow.Storage, *StorageInit, error)
Build creates a storage instance from YAML configuration.
func (*StorageFactory) Register ¶
func (f *StorageFactory) Register(builder StorageConfigBuilder)
Register registers a storage config builder.
func (*StorageFactory) RegisteredTypes ¶
func (f *StorageFactory) RegisteredTypes() []string
RegisteredTypes returns a list of registered storage types.
type StorageInit ¶
type StorageInit struct {
// Schema contains initialization SQL or commands (e.g., CREATE TABLE statements)
Schema string
// InitFunc is an optional function to run after schema initialization
InitFunc func() error
}
StorageInit contains information needed to initialize the storage (e.g., SQL schema).
func BuildStorage ¶
func BuildStorage(config *StorageConfig) (workflow.Storage, *StorageInit, error)
BuildStorage creates a storage instance using the default factory.
type StorageSetupResult ¶
type StorageSetupResult struct {
Storage workflow.Storage
HistoryStore history.HistoryStore
Connection Connection // Generic connection interface - can be SQL, MongoDB, Redis, etc.
}
StorageSetupResult contains all the initialized components from storage configuration.
func SetupStorageFromConfig ¶
func SetupStorageFromConfig(storageConfig *StorageConfig) (*StorageSetupResult, error)
SetupStorageFromConfig is a comprehensive helper that: 1. Sets up storage builders based on config 2. Builds and initializes storage 3. Sets up history store (if configured) 4. Returns everything ready to use
Supports both SQL and NoSQL storage types:
- SQL (sqlite, postgres): Initializes schema, sets up DB connection, supports history store
- NoSQL (mongodb, redis): Uses custom builders, no SQL schema initialization
type TransitionConfig ¶
type TransitionConfig struct {
Name string `yaml:"name"`
From []string `yaml:"from"`
To []string `yaml:"to"`
Guard string `yaml:"guard,omitempty"` // Expression string
After string `yaml:"after,omitempty"` // Timeout duration, e.g. "72h" (Go time.ParseDuration)
Metadata map[string]any `yaml:"metadata,omitempty"` // Transition metadata
Notes string `yaml:"notes,omitempty"` // Default notes for history
Actor string `yaml:"actor,omitempty"` // Default actor for history
CustomFields map[string]any `yaml:"custom_fields,omitempty"` // Default custom fields for history
}
TransitionConfig defines a transition with guards, metadata, and history fields.
type WorkflowConfig ¶
type WorkflowConfig struct {
Name string `yaml:"name"`
InitialMarking InitialMarkingConfig `yaml:"initial_marking"`
Metadata map[string]any `yaml:"metadata,omitempty"`
Places []PlaceConfig `yaml:"places,omitempty"`
Transitions []TransitionConfig `yaml:"transitions"`
}
WorkflowConfig defines the workflow structure.