Big refactor, trying to implement an FSM

main
Nick Dumas 2 years ago
parent ab32890495
commit 110753ef52

@ -0,0 +1,11 @@
package obspipeline
func (p *Pipeline) FindAttachments() error {
return nil
}
func (p *Pipeline) MoveAttachments(post string) error {
return nil
}

@ -0,0 +1,59 @@
package obspipeline
import (
"context"
"fmt"
)
type State string
type Event string
type Action func(ctx context.Context) error
type Node struct {
State
Transitions map[Event]*Transition
}
type Transition struct {
*Node
Action
}
type StateMachine struct {
initialNode *Node
CurrentNode *Node
}
func (m *StateMachine) getCurrentNode() *Node {
return m.CurrentNode
}
func (m *StateMachine) getNextNode(event Event) (*Node, error) {
if m.CurrentNode == nil {
return nil, fmt.Errorf("nowhere to go anymore!\n")
}
transition, ok := m.CurrentNode.Transitions[event]
if !ok {
return nil, fmt.Errorf("invalid event: %v", event)
}
return transition.Node, nil
}
func (m *StateMachine) Transition(ctx context.Context, event Event) (*Node, error) {
node, err := m.getNextNode(event) // gets current node
if err != nil {
return nil, fmt.Errorf(err.Error())
}
err = m.CurrentNode.Transitions[event].Action(ctx)
if err != nil {
return nil, fmt.Errorf(err.Error())
}
m.CurrentNode = node
return m.CurrentNode, nil
}

@ -0,0 +1,91 @@
package obspipeline
import (
"context"
)
var CopyPost = Node{
State: "CopyPost",
Transitions: map[Event]*Transition{
"HasAttachments": &Transition{
Node: &HasAttachments,
Action: func(ctx context.Context) error {
return nil
},
},
"NoAttachments": &Transition{
Node: &NoAttachments,
Action: func(ctx context.Context) error {
return nil
},
},
},
}
var NoteFound = Node{
State: "NoteFound",
Transitions: map[Event]*Transition{
"CopyPost": &Transition{
Node: &CopyPost,
Action: func(ctx context.Context) error {
// scan for attachments here
// if len() attachments > 0
//
return nil
},
},
},
}
var NoAttachments = Node{
State: "NoAttachments",
Transitions: map[Event]*Transition{
"Terminate": &Transition{
Node: &Terminate,
Action: func(ctx context.Context) error {
return nil
},
},
},
}
var HasAttachments = Node{
State: "HasAttachments",
Transitions: map[Event]*Transition{
"CopyAttachments": &Transition{
Node: &CopyAttachments,
Action: func(ctx context.Context) error {
return nil
},
},
},
}
var CopyAttachments = Node{
State: "CopyAttachments",
Transitions: map[Event]*Transition{
"SanitizeLinks": &Transition{
Node: &SanitizeLinks,
Action: func(ctx context.Context) error {
return nil
},
},
},
}
var SanitizeLinks = Node{
State: "SanitizeLinks",
Transitions: map[Event]*Transition{
"Terminate": &Transition{
Node: &Terminate,
Action: func(ctx context.Context) error {
return nil
},
},
},
}
var Terminate = Node{
State: "SanitizeLinks",
Transitions: map[Event]*Transition{},
}

@ -0,0 +1,5 @@
package obspipeline
func (p *Pipeline) FindNotes() error {
return nil
}

@ -26,31 +26,3 @@ type Pipeline struct {
L *zap.Logger L *zap.Logger
BlogDir, AttachmentsDir string BlogDir, AttachmentsDir string
} }
func (p *Pipeline) FindAttachments() error {
return nil
}
func (p *Pipeline) FindNotes() error {
return nil
}
func (p *Pipeline) FindPosts() error {
return nil
}
func (p *Pipeline) CopyPost(post string) error {
return nil
}
func (p *Pipeline) MoveAttachments(post string) error {
return nil
}
func (p *Pipeline) SanitizePost(post string) error {
return nil
}

@ -0,0 +1,15 @@
package obspipeline
func (p *Pipeline) FindPosts() error {
return nil
}
func (p *Pipeline) SanitizePost(post string) error {
return nil
}
func (p *Pipeline) CopyPost(post string) error {
return nil
}
Loading…
Cancel
Save