https://github.com/hishamk/statetrooper Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Case Studies + Customer Stories + Resources * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. {{ message }} hishamk / statetrooper Public * Notifications * Fork 0 * Star 85 StateTrooper is a Go package that provides a finite state machine (FSM) for managing states. It allows you to define and enforce state transitions based on predefined rules. License MIT license 85 stars 0 forks Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Wiki * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Wiki * Security * Insights hishamk/statetrooper This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 1 branch 0 tags Code * Local * Codespaces * Clone HTTPS GitHub CLI [https://github.com/h] Use Git or checkout with SVN using the web URL. [gh repo clone hisham] Work fast with our official CLI. Learn more about the CLI. * Open with GitHub Desktop * Download ZIP Sign In Required Please sign in to use Codespaces. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @hishamk hishamk Race condition fix for transitions. Added race condition test as well. ... 9afc711 Jun 17, 2023 Race condition fix for transitions. Added race condition test as well. 9afc711 Git stats * 16 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github/workflows Race condition fix for transitions. Added race condition test as well. June 17, 2023 22:28 LICENSE.md first commit June 16, 2023 16:30 README.md Race condition fix for transitions. Added race condition test as well. June 17, 2023 22:28 errors.go first commit June 16, 2023 16:30 go.mod first commit June 16, 2023 16:30 go.sum go.sum, empty, to appease coverage step June 16, 2023 19:04 statetrooper.go Race condition fix for transitions. Added race condition test as well. June 17, 2023 22:28 statetrooper_test.go Race condition fix for transitions. Added race condition test as well. June 17, 2023 22:28 View code Features Installation Usage Benchmark Example License Contributing README.md ______ __ ______ / __/ /____ _/ /____ /_ __/______ ___ ___ ___ ____ _\ \/ __/ _ `/ __/ -_) / / / __/ _ \/ _ \/ _ \/ -_) __/ /___/\__/\_,_/\__/\__/ /_/ /_/ \___/\___/ .__/\__/_/ /_/ Tiny, no frills finite state machine for Go GoDoc Go Coverage Go Report Card MIT Code size StateTrooper is a Go package that provides a finite state machine (FSM) for managing states. It allows you to define and enforce state transitions based on predefined rules. Features * Generic support for different comparable types * Transition history with metadata * Thread safe * Super minimal - no triggers/events or actions/callbacks. For my use case I just needed a structured, serializable way to constrain and track state transitions. Installation To install StateTrooper, use the following command: go get github.com/hishamk/statetrooper Usage Import the statetrooper package into your Go code: import "github.com/hishamk/statetrooper" Create an instance of the FSM with the desired state enum type and initial state: fsm := statetrooper.NewFSM[CustomStateEnum](CustomStateEnumA) Add valid transitions between states: fsm.AddRule(CustomStateEnumA, CustomStateEnumB) fsm.AddRule(CustomStateEnumB, CustomStateEnumC) Check if a transition from the current state to the target state is valid: canTransition := fsm.CanTransition(targetState) Transition the entity from the current state to the target state with no metadata: newState, err := fsm.Transition(targetState, nil) if err != nil { // Handle the error } Transition the entity from the current state to the target state with metadata: newState, err := fsm.Transition( CustomStateEnumB, map[string]string{ "requested_by": "Mahmoud", "logic_version": "1.0", }) Benchmark Time per Memory Allocations Benchmark Iterations Iteration Allocation per per Iteration Iteration Benchmark_transition-8 363,970 2,975 ns/ 2,187 B/op 12 allocs/ op op Example Here's an example usage with a custom entity struct and state enum: type OrderStatusEnum string // Enum values for the custom entity const ( StatusPacked OrderStatusEnum = "packed" StatusShipped OrderStatusEnum = "shipped" StatusDelivered OrderStatusEnum = "delivered" ) // Order represents a custom entity with its current state type Order struct { State *statetrooper.FSM[OrderStatusEnum] } func main() { entity := &Order{State: statetrooper.NewFSM[OrderStatusEnum](StatusPacked)} entity.State.AddRule(StatusPacked, StatusShipped) entity.State.AddRule(StatusShipped, StatusDelivered) // Check if a transition is valid canTransition := entity.State.CanTransition(StatusShipped) fmt.Printf("Can transition to %s: %t\n", StatusShipped, canTransition) // Transition to a new state _, err := entity.State.Transition(StatusShipped, nil) if err != nil { fmt.Println("Transition error:", err) } else { fmt.Println("Transition successful. Current state:", *entity.State.CurrentState) } } Note that states can be defined using any comparable type, such as strings, int, etc e.g.: // CustomStateEnum represents the state enum for the custom entity type CustomStateEnum int // Enum values for the custom entity const ( CustomStateEnumA CustomStateEnum = iota CustomStateEnumB CustomStateEnumC ) License This package is licensed under the MIT License. See the LICENSE file for details. Contributing Thank you for your interest in contributing! Feel free to PR bug fixes and documentation improvements. For new features or functional alterations, please open an issue for discussion prior to submitting a PR. About StateTrooper is a Go package that provides a finite state machine (FSM) for managing states. It allows you to define and enforce state transitions based on predefined rules. Topics go state-management fsm state-machine Resources Readme License MIT license Stars 85 stars Watchers 3 watching Forks 0 forks Report repository Releases No releases published Packages 0 No packages published Languages * Go 100.0% Footer (c) 2023 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time.