Skip to content

Commit fb52d75

Browse files
committed
v08 - fix ContinueAs data field assignment
Signed-off-by: spolti <[email protected]>
1 parent 7589021 commit fb52d75

File tree

5 files changed

+117
-10
lines changed

5 files changed

+117
-10
lines changed

model/action.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,6 @@ func (f *FunctionRef) UnmarshalJSON(data []byte) error {
8686
return nil
8787
}
8888

89-
// WorkflowRef holds a reference for a workflow definition
90-
type WorkflowRef struct {
91-
// Sub-workflow unique id
92-
WorkflowID string `json:"workflowId" validate:"required"`
93-
// Sub-workflow version
94-
Version string `json:"version,omitempty"`
95-
}
96-
9789
// UnmarshalJSON ...
9890
func (s *WorkflowRef) UnmarshalJSON(data []byte) error {
9991
subflowRef := make(map[string]json.RawMessage)

model/states.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package model
1616

1717
import (
1818
"encoding/json"
19-
2019
"k8s.io/apimachinery/pkg/util/intstr"
2120
)
2221

model/workflow.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ func (w *Workflow) setDefaults() {
206206
}
207207
}
208208

209+
// WorkflowRef holds a reference for a workflow definition
210+
type WorkflowRef struct {
211+
// Sub-workflow unique id
212+
WorkflowID string `json:"workflowId" validate:"required"`
213+
// Sub-workflow version
214+
Version string `json:"version,omitempty"`
215+
}
216+
209217
// Timeouts ...
210218
type Timeouts struct {
211219
// WorkflowExecTimeout Workflow execution timeout duration (ISO 8601 duration format). If not specified should be 'unlimited'
@@ -486,12 +494,45 @@ func (e *End) UnmarshalJSON(data []byte) error {
486494
type ContinueAs struct {
487495
WorkflowRef
488496
// TODO: add object or string data type
489-
// If string type, an expression which selects parts of the states data output to become the workflow data input of continued execution. If object type, a custom object to become the workflow data input of the continued execution
497+
// If string type, an expression which selects parts of the states data output to become the workflow data input of
498+
// continued execution. If object type, a custom object to become the workflow data input of the continued execution
490499
Data interface{} `json:"data,omitempty"`
491500
// WorkflowExecTimeout Workflow execution timeout to be used by the workflow continuing execution. Overwrites any specific settings set by that workflow
492501
WorkflowExecTimeout WorkflowExecTimeout `json:"workflowExecTimeout,omitempty"`
493502
}
494503

504+
func (c *ContinueAs) UnmarshalJSON(data []byte) error {
505+
rewf := make(map[string]interface{})
506+
if err := json.Unmarshal(data, &rewf); err != nil {
507+
c.WorkflowID, err = unmarshalString(data)
508+
if err != nil {
509+
return err
510+
}
511+
return nil
512+
}
513+
514+
c.WorkflowID = requiresNotNilOrEmpty(rewf["workflowId"])
515+
if rewf["version"] != "" {
516+
c.Version = requiresNotNilOrEmpty(rewf["version"])
517+
}
518+
if rewf["data"] != "" {
519+
c.Data = requiresNotNilOrEmpty(rewf["data"])
520+
}
521+
if rewf["workflowExecTimeout"] != "" {
522+
timeout := make(map[string]json.RawMessage)
523+
if err := json.Unmarshal(data, &timeout); err != nil {
524+
if err != nil {
525+
return err
526+
}
527+
}
528+
if err := unmarshalKey("workflowExecTimeout", timeout, &c.WorkflowExecTimeout); err != nil {
529+
return err
530+
}
531+
}
532+
533+
return nil
534+
}
535+
495536
// ProduceEvent ...
496537
type ProduceEvent struct {
497538
// References a name of a defined event

parser/parser_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,23 @@ func TestFromFile(t *testing.T) {
329329
assert.Equal(t, "PT30D", w.Timeouts.WorkflowExecTimeout.Duration)
330330
assert.Equal(t, "CancelOrder", w.Timeouts.WorkflowExecTimeout.RunBefore)
331331
},
332+
}, {
333+
"./testdata/workflows/continue-as-example.yaml", func(t *testing.T, w *model.Workflow) {
334+
assert.Equal(t, "Notify Customer", w.Name)
335+
eventState := w.States[1].(*model.DataBasedSwitchState)
336+
337+
assert.NotNil(t, eventState)
338+
assert.NotEmpty(t, eventState.DataConditions)
339+
assert.IsType(t, &model.EndDataCondition{}, eventState.DataConditions[0])
340+
341+
endDataCondition := eventState.DataConditions[0].(*model.EndDataCondition)
342+
assert.Equal(t, "notifycustomerworkflow", endDataCondition.End.ContinueAs.WorkflowRef.WorkflowID)
343+
assert.Equal(t, "1.0", endDataCondition.End.ContinueAs.WorkflowRef.Version)
344+
assert.Equal(t, "${ del(.customerCount) }", endDataCondition.End.ContinueAs.Data)
345+
assert.Equal(t, "GenerateReport", endDataCondition.End.ContinueAs.WorkflowExecTimeout.RunBefore)
346+
assert.Equal(t, true, endDataCondition.End.ContinueAs.WorkflowExecTimeout.Interrupt)
347+
assert.Equal(t, "PT1H", endDataCondition.End.ContinueAs.WorkflowExecTimeout.Duration)
348+
},
332349
},
333350
}
334351
for _, file := range files {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2022 The Serverless Workflow Specification Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
id: notifycustomerworkflow
16+
name: Notify Customer
17+
version: '1.0'
18+
specVersion: '0.8'
19+
start: WaitForCustomerEvent
20+
states:
21+
- name: WaitForCustomerEvent
22+
type: event
23+
onEvents:
24+
- eventRefs:
25+
- CustomerEvent
26+
eventDataFilter:
27+
data: "${ .customerId }"
28+
toStateData: "${ .eventCustomerId }"
29+
actions:
30+
- functionRef:
31+
refName: NotifyCustomerFunction
32+
arguments:
33+
customerId: "${ .eventCustomerId }"
34+
stateDataFilter:
35+
output: "${ .count = .count + 1 }"
36+
transition: CheckEventQuota
37+
- name: CheckEventQuota
38+
type: switch
39+
dataConditions:
40+
- condition: "${ try(.customerCount) != null and .customerCount > .quota.maxConsumedEvents}"
41+
end:
42+
continueAs:
43+
workflowId: notifycustomerworkflow
44+
version: '1.0'
45+
data: "${ del(.customerCount) }"
46+
workflowExecTimeout:
47+
duration: "PT1H"
48+
runBefore: "GenerateReport"
49+
interrupt: true
50+
defaultCondition:
51+
transition: WaitForCustomerEvent
52+
events:
53+
- name: CustomerEvent
54+
type: org.events.customerEvent
55+
source: customerSource
56+
functions:
57+
- name: NotifyCustomerFunction
58+
operation: http://myapis.org/customerapis.json#notifyCustomer

0 commit comments

Comments
 (0)