Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to document FSM visually. #9

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ f.Transition(
)
```

_Visual simple event_:
```mermaid
flowchart LR
id0(StateBar)
id1(StateFoo)
id1--> |EventFoo| id0
```

Transitions can be triggered the second time an event occurs:

```go
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Times(2),
fsm.Dst(StateBar),
)
```

_Visual repeated event_:
```mermaid
flowchart LR
id0(StateBar)
id1(StateFoo)
id1--> |2 x EventFoo| id0
```

You can have custom checks or actions:

```go
Expand All @@ -35,15 +60,6 @@ f.Transition(
)
```

Transitions can be triggered the second time an event occurs:

```go
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Times(2),
fsm.Dst(StateBar),
)
```

Functions can be called when entering or leaving a state:

```go
Expand Down
68 changes: 68 additions & 0 deletions cmd/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//go:generate stringer -type=State,Event --output=doc_fsm_string.go
//go:generate go run -tags doc doc.go doc_fsm_string.go ../README.md

package main

import (
"fmt"
"os"

"github.com/cocoonspace/fsm"
)

type State fsm.State
type Event fsm.Event

func (s State) State() fsm.State {
return fsm.State(s)
}

func (e Event) Event() fsm.Event {
return fsm.Event(e)
}

var _ fsm.NamedState = (*State)(nil)
var _ fsm.NamedEvent = (*Event)(nil)

const (
StateFoo State = iota
StateBar
)

const (
EventFoo Event = iota
)

func example1() *fsm.FSM {
f := fsm.New(StateFoo.State())

f.Transition(
fsm.On(EventFoo),
fsm.Src(StateFoo),
fsm.Dst(StateBar),
)

return f
}

func example2() *fsm.FSM {
f := fsm.New(StateFoo.State())

f.Transition(
fsm.On(EventFoo),
fsm.Times(2),
fsm.Src(StateFoo),
fsm.Dst(StateBar),
)

return f
}

func main() {
if len(os.Args) != 2 {
fmt.Println("use go generate")
return
}
example1().GenerateDoc("Visual simple event", os.Args[1])
example2().GenerateDoc("Visual repeated event", os.Args[1])
}
41 changes: 41 additions & 0 deletions cmd/doc_fsm_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 0 additions & 54 deletions doc.go

This file was deleted.

Loading