sequbus

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