12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- package impl
15
+ package ctx
16
16
17
17
import (
18
18
"context"
19
19
"errors"
20
+ "github.com/serverlessworkflow/sdk-go/v3/model"
20
21
"sync"
21
22
)
22
23
24
+ var ErrWorkflowContextNotFound = errors .New ("workflow context not found" )
25
+
26
+ var _ WorkflowContext = & workflowContext {}
27
+
23
28
type ctxKey string
24
29
25
- const runnerCtxKey ctxKey = "wfRunnerContext"
30
+ const (
31
+ runnerCtxKey ctxKey = "wfRunnerContext"
32
+ varsContext = "$context"
33
+ varsInput = "$input"
34
+ varsOutput = "$output"
35
+ varsWorkflow = "$workflow"
36
+ )
37
+
38
+ type WorkflowContext interface {
39
+ SetStatus (status StatusPhase )
40
+ SetTaskStatus (task string , status StatusPhase )
41
+ SetInstanceCtx (value interface {})
42
+ GetInstanceCtx () interface {}
43
+ SetInput (input interface {})
44
+ GetInput () interface {}
45
+ SetOutput (output interface {})
46
+ GetOutput () interface {}
47
+ GetOutputAsMap () map [string ]interface {}
48
+ AsJQVars () map [string ]interface {}
49
+ }
26
50
27
- // WorkflowContext holds the necessary data for the workflow execution within the instance.
28
- type WorkflowContext struct {
51
+ // workflowContext holds the necessary data for the workflow execution within the instance.
52
+ type workflowContext struct {
29
53
mu sync.Mutex
30
- input interface {} // input can hold any type
31
- output interface {} // output can hold any type
32
- context map [string ]interface {}
54
+ input interface {} // $input can hold any type
55
+ output interface {} // $output can hold any type
56
+ context map [string ]interface {} // Holds `$context` as the key
57
+ definition map [string ]interface {} // $workflow representation in the context
33
58
StatusPhase []StatusPhaseLog
34
- TasksStatusPhase map [string ][]StatusPhaseLog // Holds `$context` as the key
59
+ TasksStatusPhase map [string ][]StatusPhaseLog
35
60
}
36
61
37
- type TaskContext interface {
38
- SetTaskStatus (task string , status StatusPhase )
62
+ func NewWorkflowContext (workflow * model.Workflow ) (WorkflowContext , error ) {
63
+ workflowCtx := & workflowContext {}
64
+ workflowDef , err := workflow .AsMap ()
65
+ if err != nil {
66
+ return nil , err
67
+ }
68
+
69
+ workflowCtx .definition = workflowDef
70
+ workflowCtx .SetStatus (PendingStatus )
71
+
72
+ return workflowCtx , nil
73
+ }
74
+
75
+ func (ctx * workflowContext ) AsJQVars () map [string ]interface {} {
76
+ vars := make (map [string ]interface {})
77
+ vars [varsInput ] = ctx .GetInput ()
78
+ vars [varsOutput ] = ctx .GetOutput ()
79
+ vars [varsContext ] = ctx .GetInstanceCtx ()
80
+ vars [varsOutput ] = ctx .definition
81
+ return vars
39
82
}
40
83
41
- func (ctx * WorkflowContext ) SetStatus (status StatusPhase ) {
84
+ func (ctx * workflowContext ) SetStatus (status StatusPhase ) {
42
85
ctx .mu .Lock ()
43
86
defer ctx .mu .Unlock ()
44
87
if ctx .StatusPhase == nil {
@@ -47,7 +90,7 @@ func (ctx *WorkflowContext) SetStatus(status StatusPhase) {
47
90
ctx .StatusPhase = append (ctx .StatusPhase , NewStatusPhaseLog (status ))
48
91
}
49
92
50
- func (ctx * WorkflowContext ) SetTaskStatus (task string , status StatusPhase ) {
93
+ func (ctx * workflowContext ) SetTaskStatus (task string , status StatusPhase ) {
51
94
ctx .mu .Lock ()
52
95
defer ctx .mu .Unlock ()
53
96
if ctx .TasksStatusPhase == nil {
@@ -57,56 +100,56 @@ func (ctx *WorkflowContext) SetTaskStatus(task string, status StatusPhase) {
57
100
}
58
101
59
102
// SetInstanceCtx safely sets the `$context` value
60
- func (ctx * WorkflowContext ) SetInstanceCtx (value interface {}) {
103
+ func (ctx * workflowContext ) SetInstanceCtx (value interface {}) {
61
104
ctx .mu .Lock ()
62
105
defer ctx .mu .Unlock ()
63
106
if ctx .context == nil {
64
107
ctx .context = make (map [string ]interface {})
65
108
}
66
- ctx .context ["$context" ] = value
109
+ ctx .context [varsContext ] = value
67
110
}
68
111
69
112
// GetInstanceCtx safely retrieves the `$context` value
70
- func (ctx * WorkflowContext ) GetInstanceCtx () interface {} {
113
+ func (ctx * workflowContext ) GetInstanceCtx () interface {} {
71
114
ctx .mu .Lock ()
72
115
defer ctx .mu .Unlock ()
73
116
if ctx .context == nil {
74
117
return nil
75
118
}
76
- return ctx .context ["$context" ]
119
+ return ctx .context [varsContext ]
77
120
}
78
121
79
122
// SetInput safely sets the input
80
- func (ctx * WorkflowContext ) SetInput (input interface {}) {
123
+ func (ctx * workflowContext ) SetInput (input interface {}) {
81
124
ctx .mu .Lock ()
82
125
defer ctx .mu .Unlock ()
83
126
ctx .input = input
84
127
}
85
128
86
129
// GetInput safely retrieves the input
87
- func (ctx * WorkflowContext ) GetInput () interface {} {
130
+ func (ctx * workflowContext ) GetInput () interface {} {
88
131
ctx .mu .Lock ()
89
132
defer ctx .mu .Unlock ()
90
133
return ctx .input
91
134
}
92
135
93
136
// SetOutput safely sets the output
94
- func (ctx * WorkflowContext ) SetOutput (output interface {}) {
137
+ func (ctx * workflowContext ) SetOutput (output interface {}) {
95
138
ctx .mu .Lock ()
96
139
defer ctx .mu .Unlock ()
97
140
ctx .output = output
98
141
}
99
142
100
143
// GetOutput safely retrieves the output
101
- func (ctx * WorkflowContext ) GetOutput () interface {} {
144
+ func (ctx * workflowContext ) GetOutput () interface {} {
102
145
ctx .mu .Lock ()
103
146
defer ctx .mu .Unlock ()
104
147
return ctx .output
105
148
}
106
149
107
150
// GetInputAsMap safely retrieves the input as a map[string]interface{}.
108
151
// If input is not a map, it creates a map with an empty string key and the input as the value.
109
- func (ctx * WorkflowContext ) GetInputAsMap () map [string ]interface {} {
152
+ func (ctx * workflowContext ) GetInputAsMap () map [string ]interface {} {
110
153
ctx .mu .Lock ()
111
154
defer ctx .mu .Unlock ()
112
155
@@ -122,7 +165,7 @@ func (ctx *WorkflowContext) GetInputAsMap() map[string]interface{} {
122
165
123
166
// GetOutputAsMap safely retrieves the output as a map[string]interface{}.
124
167
// If output is not a map, it creates a map with an empty string key and the output as the value.
125
- func (ctx * WorkflowContext ) GetOutputAsMap () map [string ]interface {} {
168
+ func (ctx * workflowContext ) GetOutputAsMap () map [string ]interface {} {
126
169
ctx .mu .Lock ()
127
170
defer ctx .mu .Unlock ()
128
171
@@ -136,16 +179,16 @@ func (ctx *WorkflowContext) GetOutputAsMap() map[string]interface{} {
136
179
}
137
180
}
138
181
139
- // WithWorkflowContext adds the WorkflowContext to a parent context
140
- func WithWorkflowContext (parent context.Context , wfCtx * WorkflowContext ) context.Context {
182
+ // WithWorkflowContext adds the workflowContext to a parent context
183
+ func WithWorkflowContext (parent context.Context , wfCtx WorkflowContext ) context.Context {
141
184
return context .WithValue (parent , runnerCtxKey , wfCtx )
142
185
}
143
186
144
- // GetWorkflowContext retrieves the WorkflowContext from a context
145
- func GetWorkflowContext (ctx context.Context ) (* WorkflowContext , error ) {
146
- wfCtx , ok := ctx .Value (runnerCtxKey ).(* WorkflowContext )
187
+ // GetWorkflowContext retrieves the workflowContext from a context
188
+ func GetWorkflowContext (ctx context.Context ) (WorkflowContext , error ) {
189
+ wfCtx , ok := ctx .Value (runnerCtxKey ).(* workflowContext )
147
190
if ! ok {
148
- return nil , errors . New ( "workflow context not found" )
191
+ return nil , ErrWorkflowContextNotFound
149
192
}
150
193
return wfCtx , nil
151
194
}
0 commit comments