-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow.go
More file actions
119 lines (105 loc) · 2.82 KB
/
Copy pathflow.go
File metadata and controls
119 lines (105 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package partial
import (
"net/http"
)
// FlowStep represents a single step in a pageflow.
type FlowStep struct {
Name string
Partial *Partial
Validate func(r *http.Request, data map[string]any) error // nil for info-only steps
}
// PageFlow manages a multi-step flow.
type PageFlow struct {
Steps []FlowStep
Current int
}
// FlowSessionData holds all data and validation info for a flow, to be stored in session.
type FlowSessionData struct {
StepData map[string]map[string]any // stepName -> data
Validated map[string]bool // stepName -> validated
Current string // current step name
}
// NewPageFlow creates a new PageFlow with the given steps.
func NewPageFlow(steps []FlowStep) *PageFlow {
return &PageFlow{
Steps: steps,
Current: 0,
}
}
// GetCurrentStep returns the current FlowStep.
func (f *PageFlow) GetCurrentStep() *FlowStep {
if f.Current < 0 || f.Current >= len(f.Steps) {
return nil
}
return &f.Steps[f.Current]
}
// Next advances to the next step if possible.
func (f *PageFlow) Next() bool {
if f.Current < len(f.Steps)-1 {
f.Current++
return true
}
return false
}
// Prev goes back to the previous step if possible.
func (f *PageFlow) Prev() bool {
if f.Current > 0 {
f.Current--
return true
}
return false
}
// FindStep returns the index of a step by name, or -1 if not found.
func (f *PageFlow) FindStep(name string) int {
for i, step := range f.Steps {
if step.Name == name {
return i
}
}
return -1
}
// AllPreviousValidated checks if all previous steps are validated.
func (f *PageFlow) AllPreviousValidated(session *FlowSessionData) bool {
curStep := f.GetCurrentStep()
if curStep == nil {
return false
}
curIdx := f.FindStep(curStep.Name)
for i := 0; i < curIdx; i++ {
if !session.Validated[f.Steps[i].Name] {
return false
}
}
return true
}
// SetStepValidated marks a step as validated in the session.
func (session *FlowSessionData) SetStepValidated(stepName string, valid bool) {
if session.Validated == nil {
session.Validated = make(map[string]bool)
}
session.Validated[stepName] = valid
}
// SetStepData sets the data for a step in the session.
func (session *FlowSessionData) SetStepData(stepName string, data map[string]any) {
if session.StepData == nil {
session.StepData = make(map[string]map[string]any)
}
session.StepData[stepName] = data
}
// GetStepData gets the data for a step from the session.
func (session *FlowSessionData) GetStepData(stepName string) map[string]any {
if session.StepData == nil {
return nil
}
return session.StepData[stepName]
}
// GetAllData returns all step data as a single merged map.
func (session *FlowSessionData) GetAllData() map[string]any {
merged := make(map[string]any)
for _, data := range session.StepData {
for k, v := range data {
merged[k] = v
}
}
return merged
}