Package: swf

Simple linear, human-driven workflows: steps, progress, current responsible, metadata, serialization, and DOT visualization.

Package: swf

Simple linear, human-driven workflow engine for Go.

Repository: https://github.com/dracory/swf

Overview

The workflow package allows you to create and manage linear, sequential workflows driven by human interaction. It provides functionality to:

  • Create and manage steps in a sequential workflow
  • Track the current step and who is responsible for it
  • Determine if steps are complete
  • Calculate workflow progress
  • Store and retrieve metadata for steps
  • Serialize and deserialize workflow state
  • Visualize the workflow as a DOT graph

Note: This is a simple linear workflow system. It does not support branching/DAGs or parallel execution.

When to Use

Ideal for:

  • Document approval workflows
  • Employee onboarding
  • Manual reviews
  • Form completion wizards
  • Business process checklists
  • Project stage gates

Not suitable for:

  • Automated or DAG/branching workflows
  • Parallel processing
  • Conditional branching
  • Automated task execution

Components

Step

A Step represents a single step in a workflow. Each step has:

  • Name: unique identifier
  • Type: type (default "normal")
  • Title: display title
  • Description: what the step does
  • Responsible: person/role responsible

Workflow

A Workflow manages multiple steps and tracks state. It can:

  • Add steps in sequence
  • Get and set the current step
  • Check if a step is current or complete
  • Calculate progress
  • Store/retrieve per-step metadata
  • Serialize/deserialize state
  • Visualize as DOT graph

Usage

package main

import (
    "fmt"
    workflow "github.com/dracory/swf"
)

func main() {
    // Create a new linear workflow
    wf := workflow.NewWorkflow()

    // Create steps in sequence
    step1 := workflow.NewStep("step1")
    step1.Title = "Document Review"
    step1.Description = "Review the submitted document"
    step1.Responsible = "John Doe"

    step2 := workflow.NewStep("step2")
    step2.Title = "Manager Approval"
    step2.Description = "Manager approval of the document"
    step2.Responsible = "Jane Smith"

    // Add steps to the workflow in sequence
    wf.AddStep(step1)
    wf.AddStep(step2)

    // Get the current step
    current := wf.GetCurrentStep()
    fmt.Printf("Current step: %s\n", current.Name)

    // Move to the next step
    wf.SetCurrentStep(step2)

    // Check if a step is complete
    isComplete := wf.IsStepComplete(step1)
    fmt.Printf("Is step1 complete? %v\n", isComplete)

    // Progress
    p := wf.GetProgress()
    fmt.Printf("Progress: %d/%d steps completed (%.2f%%)\n", p.Completed, p.Total, p.Percents)

    // Visualize (DOT)
    dot := wf.Visualize()
    fmt.Println(dot)
}

Visualization

The DOT graph:

  • Boxes for steps with titles
  • Colors:
    • White: pending
    • Blue (#2196F3): current
    • Green (#4CAF50): completed
  • Edges:
    • Gray (#9E9E9E): default
    • Green (#4CAF50): completed path
  • Tooltips include step descriptions
  • Left-to-right layout

Render with Graphviz:

  • dot -Tpng workflow.dot -o workflow.png
  • Or use an online Graphviz tool.
Menu