-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_step_context.go
100 lines (83 loc) · 2.47 KB
/
go_step_context.go
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
package gosteps
// GoStepsCtxData type defines the data stored in the context
type GoStepsCtxData map[string]interface{}
// StepProgress type defines the progress of the step
type StepProgress struct {
StepName StepName `json:"stepName"`
StepResult StepResult `json:"stepResult"`
}
// GoStepsCtx type defines the context for the step-chain
type GoStepsCtx struct {
data GoStepsCtxData
currentStep StepName
stepsProgress map[StepName]StepProgress
logger *goStepsLogger
}
// GoStepsContext interface defines the methods for the context
type GoStepsContext interface {
getCtx() GoStepsCtx
log(step *Step)
Use(args ...interface{}) GoStepsContext
Log(message string, levels ...LogLevel)
SetData(key string, value interface{})
GetData(key string) interface{}
WithData(data map[string]interface{})
SetProgress(step StepName, stepResult StepResult) GoStepsCtx
SetCurrentStep(step StepName) GoStepsCtx
}
// GoStepsCtx type defines the context for the step-chain
func NewGoStepsContext() GoStepsContext {
logger := NewGoStepsLogger(nil, &LoggerOpts{
StepLoggingEnabled: false,
})
return &GoStepsCtx{
data: GoStepsCtxData{},
stepsProgress: map[StepName]StepProgress{},
logger: &logger,
}
}
// getCtx returns the context - not exported
func (ctx GoStepsCtx) getCtx() GoStepsCtx {
return ctx
}
// Handles adding handlers to the context
func (ctx *GoStepsCtx) Use(args ...interface{}) GoStepsContext {
for i := range args {
switch arg := args[i].(type) {
case goStepsLogger:
ctx.logger = &arg
}
}
return ctx
}
// SetData sets the data in the context
func (ctx GoStepsCtx) SetData(key string, value interface{}) {
ctx.data[key] = value
}
// GetData gets the data from the context
func (ctx GoStepsCtx) GetData(key string) interface{} {
return ctx.data[key]
}
// WithData sets the data in the context
func (ctx GoStepsCtx) WithData(data map[string]interface{}) {
for key, value := range data {
ctx.SetData(key, value)
}
}
// SetProgress sets the progress of the step
func (ctx *GoStepsCtx) SetProgress(step StepName, stepResult StepResult) GoStepsCtx {
ctx.stepsProgress[step] = StepProgress{
StepName: step,
StepResult: stepResult,
}
return *ctx
}
// GetProgress gets the progress of the step
func (ctx GoStepsCtx) GetProgress(step StepName) StepProgress {
return ctx.stepsProgress[step]
}
// SetCurrentStep sets the current step
func (ctx *GoStepsCtx) SetCurrentStep(step StepName) GoStepsCtx {
ctx.currentStep = step
return *ctx
}