1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- package impl
15+ package ctx
1616
1717import (
1818 "context"
1919 "errors"
20+ "github.com/serverlessworkflow/sdk-go/v3/model"
2021 "sync"
2122)
2223
24+ var ErrWorkflowContextNotFound = errors .New ("workflow context not found" )
25+
26+ var _ WorkflowContext = & workflowContext {}
27+
2328type ctxKey string
2429
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+ }
2650
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 {
2953 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
3358 StatusPhase []StatusPhaseLog
34- TasksStatusPhase map [string ][]StatusPhaseLog // Holds `$context` as the key
59+ TasksStatusPhase map [string ][]StatusPhaseLog
3560}
3661
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
3982}
4083
41- func (ctx * WorkflowContext ) SetStatus (status StatusPhase ) {
84+ func (ctx * workflowContext ) SetStatus (status StatusPhase ) {
4285 ctx .mu .Lock ()
4386 defer ctx .mu .Unlock ()
4487 if ctx .StatusPhase == nil {
@@ -47,7 +90,7 @@ func (ctx *WorkflowContext) SetStatus(status StatusPhase) {
4790 ctx .StatusPhase = append (ctx .StatusPhase , NewStatusPhaseLog (status ))
4891}
4992
50- func (ctx * WorkflowContext ) SetTaskStatus (task string , status StatusPhase ) {
93+ func (ctx * workflowContext ) SetTaskStatus (task string , status StatusPhase ) {
5194 ctx .mu .Lock ()
5295 defer ctx .mu .Unlock ()
5396 if ctx .TasksStatusPhase == nil {
@@ -57,56 +100,56 @@ func (ctx *WorkflowContext) SetTaskStatus(task string, status StatusPhase) {
57100}
58101
59102// SetInstanceCtx safely sets the `$context` value
60- func (ctx * WorkflowContext ) SetInstanceCtx (value interface {}) {
103+ func (ctx * workflowContext ) SetInstanceCtx (value interface {}) {
61104 ctx .mu .Lock ()
62105 defer ctx .mu .Unlock ()
63106 if ctx .context == nil {
64107 ctx .context = make (map [string ]interface {})
65108 }
66- ctx .context ["$context" ] = value
109+ ctx .context [varsContext ] = value
67110}
68111
69112// GetInstanceCtx safely retrieves the `$context` value
70- func (ctx * WorkflowContext ) GetInstanceCtx () interface {} {
113+ func (ctx * workflowContext ) GetInstanceCtx () interface {} {
71114 ctx .mu .Lock ()
72115 defer ctx .mu .Unlock ()
73116 if ctx .context == nil {
74117 return nil
75118 }
76- return ctx .context ["$context" ]
119+ return ctx .context [varsContext ]
77120}
78121
79122// SetInput safely sets the input
80- func (ctx * WorkflowContext ) SetInput (input interface {}) {
123+ func (ctx * workflowContext ) SetInput (input interface {}) {
81124 ctx .mu .Lock ()
82125 defer ctx .mu .Unlock ()
83126 ctx .input = input
84127}
85128
86129// GetInput safely retrieves the input
87- func (ctx * WorkflowContext ) GetInput () interface {} {
130+ func (ctx * workflowContext ) GetInput () interface {} {
88131 ctx .mu .Lock ()
89132 defer ctx .mu .Unlock ()
90133 return ctx .input
91134}
92135
93136// SetOutput safely sets the output
94- func (ctx * WorkflowContext ) SetOutput (output interface {}) {
137+ func (ctx * workflowContext ) SetOutput (output interface {}) {
95138 ctx .mu .Lock ()
96139 defer ctx .mu .Unlock ()
97140 ctx .output = output
98141}
99142
100143// GetOutput safely retrieves the output
101- func (ctx * WorkflowContext ) GetOutput () interface {} {
144+ func (ctx * workflowContext ) GetOutput () interface {} {
102145 ctx .mu .Lock ()
103146 defer ctx .mu .Unlock ()
104147 return ctx .output
105148}
106149
107150// GetInputAsMap safely retrieves the input as a map[string]interface{}.
108151// 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 {} {
110153 ctx .mu .Lock ()
111154 defer ctx .mu .Unlock ()
112155
@@ -122,7 +165,7 @@ func (ctx *WorkflowContext) GetInputAsMap() map[string]interface{} {
122165
123166// GetOutputAsMap safely retrieves the output as a map[string]interface{}.
124167// 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 {} {
126169 ctx .mu .Lock ()
127170 defer ctx .mu .Unlock ()
128171
@@ -136,16 +179,16 @@ func (ctx *WorkflowContext) GetOutputAsMap() map[string]interface{} {
136179 }
137180}
138181
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 {
141184 return context .WithValue (parent , runnerCtxKey , wfCtx )
142185}
143186
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 )
147190 if ! ok {
148- return nil , errors . New ( "workflow context not found" )
191+ return nil , ErrWorkflowContextNotFound
149192 }
150193 return wfCtx , nil
151194}
0 commit comments