Skip to content

Commit c6596fd

Browse files
Fixes workflow event states unmarshalling methods (#65)
* clear mapState after next loop iteraction * signed commit * signed commit * added unit test for both events at once
1 parent 98db3f0 commit c6596fd

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
bin
22
.idea
3-
*.out
3+
*.out
4+
.vscode

model/workflow.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (w *Workflow) UnmarshalJSON(data []byte) error {
117117
}
118118

119119
w.States = make([]State, len(rawStates))
120-
var mapState map[string]interface{}
120+
mapState := map[string]interface{}{}
121121
for i, rawState := range rawStates {
122122
if err := json.Unmarshal(rawState, &mapState); err != nil {
123123
return err
@@ -130,6 +130,7 @@ func (w *Workflow) UnmarshalJSON(data []byte) error {
130130
return err
131131
}
132132
w.States[i] = state
133+
mapState = map[string]interface{}{}
133134
}
134135
if _, ok := workflowMap["events"]; ok {
135136
if err := json.Unmarshal(workflowMap["events"], &w.Events); err != nil {

parser/parser_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ func TestFromFile(t *testing.T) {
6464
assert.NotNil(t, w.States[0].(*model.OperationState).Actions[0].FunctionRef)
6565
assert.Equal(t, "greetingFunction", w.States[0].(*model.OperationState).Actions[0].FunctionRef.RefName)
6666
},
67+
"./testdata/workflows/eventbaseddataandswitch.sw.json": func(t *testing.T, w *model.Workflow) {
68+
assert.Equal(t, "Start", w.States[0].GetName())
69+
assert.Equal(t, "CheckVisaStatus", w.States[1].GetName())
70+
assert.IsType(t, &model.DataBasedSwitchState{}, w.States[0])
71+
assert.IsType(t, &model.EventBasedSwitchState{}, w.States[1])
72+
},
6773
"./testdata/workflows/eventbasedgreeting.sw.json": func(t *testing.T, w *model.Workflow) {
6874
assert.Equal(t, "GreetingEvent", w.Events[0].Name)
6975
assert.IsType(t, &model.EventState{}, w.States[0])
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"id": "eventbaseddataandswitch",
3+
"version": "1.0",
4+
"name": "Event Based Switch Transitions",
5+
"description": "Event Based Switch Transitions with Event Database Condition",
6+
"specVersion": "0.7",
7+
"start": {
8+
"stateName": "Start"
9+
},
10+
"events": [
11+
{
12+
"name": "visaApprovedEvent",
13+
"type": "VisaApproved",
14+
"source": "visaCheckSource"
15+
},
16+
{
17+
"name": "visaRejectedEvent",
18+
"type": "VisaRejected",
19+
"source": "visaCheckSource"
20+
}
21+
],
22+
"states": [
23+
{
24+
"name": "Start",
25+
"type": "switch",
26+
"dataConditions": [
27+
{
28+
"condition": "${ true }",
29+
"transition": "CheckVisaStatus"
30+
}
31+
]
32+
},
33+
{
34+
"name": "CheckVisaStatus",
35+
"type": "switch",
36+
"eventConditions": [
37+
{
38+
"eventRef": "visaApprovedEvent",
39+
"transition": {
40+
"nextState": "HandleApprovedVisa"
41+
}
42+
},
43+
{
44+
"eventRef": "visaRejectedEvent",
45+
"transition": {
46+
"nextState": "HandleRejectedVisa"
47+
}
48+
}
49+
],
50+
"eventTimeout": "PT1H",
51+
"defaultCondition": {
52+
"transition": {
53+
"nextState": "HandleNoVisaDecision"
54+
}
55+
}
56+
},
57+
{
58+
"name": "HandleApprovedVisa",
59+
"type": "operation",
60+
"actions": [
61+
{
62+
"subFlowRef": {
63+
"workflowId": "handleApprovedVisaWorkflowID"
64+
}
65+
}
66+
],
67+
"end": {
68+
"terminate": true
69+
}
70+
},
71+
{
72+
"name": "HandleRejectedVisa",
73+
"type": "operation",
74+
"actions": [
75+
{
76+
"subFlowRef": {
77+
"workflowId": "handleRejectedVisaWorkflowID"
78+
}
79+
}
80+
],
81+
"end": {
82+
"terminate": true
83+
}
84+
},
85+
{
86+
"name": "HandleNoVisaDecision",
87+
"type": "operation",
88+
"actions": [
89+
{
90+
"subFlowRef": {
91+
"workflowId": "handleNoVisaDecisionWorkfowId"
92+
}
93+
}
94+
],
95+
"end": {
96+
"terminate": true
97+
}
98+
}
99+
]
100+
}

0 commit comments

Comments
 (0)