-
Notifications
You must be signed in to change notification settings - Fork 312
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
Generics #86
Draft
majst01
wants to merge
33
commits into
looplab:main
Choose a base branch
from
majst01:generics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Generics #86
Changes from 20 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
b7a4085
Part 1, generic Events
majst01 752ee38
Refactor a bit
majst01 21ed7a8
Generic state and Event
majst01 d00aa3c
Adopt examples
majst01 114e252
any instead of interface{}
majst01 b3de70b
gitignore
majst01 698f85d
Flows
majst01 4be1559
Naming
majst01 4e23d6f
One more
majst01 459416d
One more
majst01 80bc866
renaming again
majst01 978d788
Add benchmarks
majst01 49ce321
fix example
majst01 c62ba1b
Even more renamings
majst01 aa1a0dc
Generic Callbacks
majst01 eec2205
more generic event and state constraints, godoc
majst01 67d99fb
Fix examples
majst01 2cedcae
Fix
majst01 c8078b8
Fix readme
majst01 1e23f92
Linter
majst01 31b8183
nameing
majst01 3cc3d2c
nameing
majst01 cd3c5e7
Back to one alloc
majst01 cf20b0a
test metadata
majst01 b213cec
godoc
majst01 040175d
no implicit structs
majst01 28f6666
Add one more benchmark
majst01 bb1ee6b
Enable benchmarks in test
majst01 0b31398
Run tests with make target
majst01 1633e53
Named callbacktypes with validation
majst01 54758d4
Even more unsupported callbacks
majst01 fe6eccb
Add missing file
majst01 2925185
V2
majst01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,5 +29,6 @@ _testmain.go | |
|
||
# Testing | ||
.coverprofile | ||
coverage.out | ||
|
||
.vscode | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,76 @@ | ||
//go:build ignore | ||
// +build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/looplab/fsm" | ||
) | ||
|
||
func main() { | ||
fsm := fsm.NewFSM( | ||
f := fsm.New( | ||
"idle", | ||
fsm.Events{ | ||
{Name: "scan", Src: []string{"idle"}, Dst: "scanning"}, | ||
{Name: "working", Src: []string{"scanning"}, Dst: "scanning"}, | ||
{Name: "situation", Src: []string{"scanning"}, Dst: "scanning"}, | ||
{Name: "situation", Src: []string{"idle"}, Dst: "idle"}, | ||
{Name: "finish", Src: []string{"scanning"}, Dst: "idle"}, | ||
fsm.Transistions[string, string]{ | ||
{Event: "scan", Src: []string{"idle"}, Dst: "scanning"}, | ||
{Event: "working", Src: []string{"scanning"}, Dst: "scanning"}, | ||
{Event: "situation", Src: []string{"scanning"}, Dst: "scanning"}, | ||
{Event: "situation", Src: []string{"idle"}, Dst: "idle"}, | ||
{Event: "finish", Src: []string{"scanning"}, Dst: "idle"}, | ||
}, | ||
fsm.Callbacks{ | ||
"scan": func(e *fsm.Event) { | ||
fmt.Println("after_scan: " + e.FSM.Current()) | ||
fsm.Callbacks[string, string]{ | ||
fsm.Callback[string, string]{When: fsm.BeforeEvent, Event: "scan", | ||
F: func(e *fsm.CallbackContext[string, string]) { | ||
fmt.Println("after_scan: " + e.FSM.Current()) | ||
}, | ||
}, | ||
"working": func(e *fsm.Event) { | ||
fmt.Println("working: " + e.FSM.Current()) | ||
fsm.Callback[string, string]{When: fsm.BeforeEvent, Event: "working", | ||
F: func(e *fsm.CallbackContext[string, string]) { | ||
fmt.Println("working: " + e.FSM.Current()) | ||
}, | ||
}, | ||
"situation": func(e *fsm.Event) { | ||
fmt.Println("situation: " + e.FSM.Current()) | ||
fsm.Callback[string, string]{When: fsm.BeforeEvent, Event: "situation", | ||
F: func(e *fsm.CallbackContext[string, string]) { | ||
fmt.Println("situation: " + e.FSM.Current()) | ||
}, | ||
}, | ||
"finish": func(e *fsm.Event) { | ||
fmt.Println("finish: " + e.FSM.Current()) | ||
fsm.Callback[string, string]{When: fsm.BeforeEvent, Event: "finish", | ||
F: func(e *fsm.CallbackContext[string, string]) { | ||
fmt.Println("finish: " + e.FSM.Current()) | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
fmt.Println(fsm.Current()) | ||
fmt.Println(f.Current()) | ||
|
||
err := fsm.Event("scan") | ||
err := f.Event("scan") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
fmt.Println("1:" + fsm.Current()) | ||
fmt.Println("1:" + f.Current()) | ||
|
||
err = fsm.Event("working") | ||
err = f.Event("working") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
fmt.Println("2:" + fsm.Current()) | ||
fmt.Println("2:" + f.Current()) | ||
|
||
err = fsm.Event("situation") | ||
err = f.Event("situation") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
fmt.Println("3:" + fsm.Current()) | ||
fmt.Println("3:" + f.Current()) | ||
|
||
err = fsm.Event("finish") | ||
err = f.Event("finish") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
fmt.Println("4:" + fsm.Current()) | ||
fmt.Println("4:" + f.Current()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//go:build ignore | ||
// +build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/looplab/fsm" | ||
) | ||
|
||
type MyEvent string | ||
type MyState string | ||
|
||
const ( | ||
Close MyEvent = "close" | ||
Open MyEvent = "open" | ||
Any MyEvent = "" | ||
|
||
IsClosed MyState = "closed" | ||
IsOpen MyState = "open" | ||
) | ||
|
||
func main() { | ||
fsm := fsm.New( | ||
IsClosed, | ||
fsm.Transitions[MyEvent, MyState]{ | ||
{Event: Open, Src: []MyState{IsClosed}, Dst: IsOpen}, | ||
{Event: Close, Src: []MyState{IsOpen}, Dst: IsClosed}, | ||
}, | ||
fsm.Callbacks[MyEvent, MyState]{ | ||
fsm.Callback[MyEvent, MyState]{ | ||
When: fsm.AfterEvent, Event: Open, | ||
F: func(cr *fsm.CallbackContext[MyEvent, MyState]) { | ||
fmt.Printf("callback: event:%s src:%s dst:%s\n", cr.Event, cr.Src, cr.Dst) | ||
}, | ||
}, | ||
fsm.Callback[MyEvent, MyState]{ | ||
When: fsm.AfterAllEvents, | ||
F: func(cr *fsm.CallbackContext[MyEvent, MyState]) { | ||
fmt.Printf("callback after all: event:%s src:%s dst:%s\n", cr.Event, cr.Src, cr.Dst) | ||
}, | ||
}, | ||
}, | ||
) | ||
fmt.Println(fsm.Current()) | ||
err := fsm.Event(Open) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println(fsm.Current()) | ||
err = fsm.Event(Close) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println(fsm.Current()) | ||
// Output: | ||
// closed | ||
// open | ||
// closed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can more types be inferred here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean with this ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven’t used generics in Go so much, it just felt there was some repetition in specifying the types. Thought maybe more types could be inferred, but that was more of an open question.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, type inference is the most complicated part in the generics of the go compiler, i tried making this a bit lighter by have a fluent interface for the FSM creation, something like:
Methods usually make inference easier for the compiler, but it didnt work out.