sequbus

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2025 License: MIT Imports: 3 Imported by: 0

README ΒΆ

sequbus

License Release GolangVersion Go Reference

sequbus is a generic and lightweight sequential command bus for Go. It allows you to register and execute a series of commands in a strict order. Inspired by middleware and pipeline patterns.

✨ Features

  • βœ… Generic: works with any data type
  • βœ… Sequential: commands run one after another
  • βœ… Chainable: register multiple commands easily
  • βœ… Early exit on error
  • βœ… Simple interface

πŸ“¦ Installation

go get github.com/go-nop/sequbus

πŸš€ Usage

1. Define Your Data and Command(s)
type User struct {
	ID   string
	Name string
}

type ValidateUser struct{}

func (v ValidateUser) Run(ctx context.Context, user *User) error {
	if user.ID == "" {
		return errors.New("missing ID")
	}
	return nil
}
2. Register Commands to the Bus
bus := sequbus.New[*User]()
bus.Register(ValidateUser{})
// bus.Register(SendWelcomeEmail{}) // More commands

user := &User{ID: "123", Name: "Alice"}
err := bus.Dispatch(context.Background(), user)
if err != nil {
	log.Fatal(err)
}

πŸ§ͺ Testing

Run all tests:

go test ./...

πŸ“‚ Project Structure

sequbus/
β”œβ”€β”€ bus.go                 // CommandBus implementation
β”œβ”€β”€ bus_test.go         // Unit tests
β”œβ”€β”€ node.go              // Command node implementation
β”œβ”€β”€ node_test.go      // Unit tests
β”œβ”€β”€ runner/
    └── interface.go   // runner.Interface definition
└── example/
	└── main.go     // Example usage

πŸ“˜ Interface

type Interface[T any] interface {
	Run(ctx context.Context, data T) error
}

Implement this interface to create custom commands.


πŸ›  Example Use Cases

  • User registration pipeline
  • Data processing workflows
  • Approval chains
  • Middleware-like systems in CLI or microservices

πŸ“„ License

MIT


Made with ❀️ by @go-nop

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type CommandBus ΒΆ

type CommandBus[T any] struct {
	// contains filtered or unexported fields
}

CommandBus is a sequential command bus that allows registering commands

func New ΒΆ

func New[T any]() *CommandBus[T]

New creates a new instance of CommandBus

func (*CommandBus[T]) Dispatch ΒΆ

func (cb *CommandBus[T]) Dispatch(ctx context.Context, data T) error

Dispatch executes the registered commands in sequence

func (*CommandBus[T]) Register ΒΆ

func (cb *CommandBus[T]) Register(r runner.Interface[T])

Register adds a new command to the bus

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL