Skip to content

Commit 397ae25

Browse files
VicNguVictor Nguyen
andauthored
Fix EventState Exclusive parsing interface{} to *bool error (#52)
* Fix EventState Exclusive parsing interface{} to *bool error Signed-off-by: Victor Nguyen <victor.nguyen@acronis.com> * Added test cases for exclusive and nonexclusive event workflows Signed-off-by: Victor Nguyen <victor.nguyen@acronis.com> Co-authored-by: Victor Nguyen <victor.nguyen@acronis.com>
1 parent a1c8b1d commit 397ae25

File tree

4 files changed

+172
-5
lines changed

4 files changed

+172
-5
lines changed

model/states.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ type DelayState struct {
138138
type EventState struct {
139139
BaseState
140140
// If true consuming one of the defined events causes its associated actions to be performed. If false all of the defined events must be consumed in order for actions to be performed
141-
Exclusive *bool `json:"exclusive,omitempty"`
141+
Exclusive bool `json:"exclusive,omitempty"`
142142
// Define the events to be consumed and optional actions to be performed
143143
OnEvents []OnEvents `json:"onEvents" validate:"required,min=1,dive"`
144144
// State specific timeouts
@@ -156,10 +156,13 @@ func (e *EventState) UnmarshalJSON(data []byte) error {
156156
return err
157157
}
158158

159-
if eventStateMap["exclusive"] == nil {
160-
e.Exclusive = &TRUE
161-
} else {
162-
e.Exclusive = eventStateMap["exclusive"].(*bool)
159+
e.Exclusive = true
160+
161+
if eventStateMap["exclusive"] != nil {
162+
exclusiveVal, ok := eventStateMap["exclusive"].(bool)
163+
if ok {
164+
e.Exclusive = exclusiveVal
165+
}
163166
}
164167

165168
eventStateRaw := make(map[string]json.RawMessage)

parser/parser_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ func TestFromFile(t *testing.T) {
5959
assert.NotNil(t, eventState)
6060
assert.NotEmpty(t, eventState.OnEvents)
6161
assert.Equal(t, "GreetingEvent", eventState.OnEvents[0].EventRefs[0])
62+
assert.Equal(t, true, eventState.Exclusive)
63+
},
64+
"./testdata/workflows/eventbasedgreetingexclusive.sw.json": func(t *testing.T, w *model.Workflow) {
65+
assert.Equal(t, "GreetingEvent", w.Events[0].Name)
66+
assert.Equal(t, "GreetingEvent2", w.Events[1].Name)
67+
assert.IsType(t, &model.EventState{}, w.States[0])
68+
eventState := w.States[0].(*model.EventState)
69+
assert.NotNil(t, eventState)
70+
assert.NotEmpty(t, eventState.OnEvents)
71+
assert.Equal(t, "GreetingEvent", eventState.OnEvents[0].EventRefs[0])
72+
assert.Equal(t, "GreetingEvent2", eventState.OnEvents[1].EventRefs[0])
73+
assert.Equal(t, true, eventState.Exclusive)
74+
},
75+
"./testdata/workflows/eventbasedgreetingnonexclusive.sw.json": func(t *testing.T, w *model.Workflow) {
76+
assert.Equal(t, "GreetingEvent", w.Events[0].Name)
77+
assert.Equal(t, "GreetingEvent2", w.Events[1].Name)
78+
assert.IsType(t, &model.EventState{}, w.States[0])
79+
eventState := w.States[0].(*model.EventState)
80+
assert.NotNil(t, eventState)
81+
assert.NotEmpty(t, eventState.OnEvents)
82+
assert.Equal(t, "GreetingEvent", eventState.OnEvents[0].EventRefs[0])
83+
assert.Equal(t, "GreetingEvent2", eventState.OnEvents[0].EventRefs[1])
84+
assert.Equal(t, false, eventState.Exclusive)
6285
},
6386
"./testdata/workflows/eventbasedgreeting.sw.p.json": func(t *testing.T, w *model.Workflow) {
6487
assert.Equal(t, "GreetingEvent", w.Events[0].Name)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"id": "eventbasedgreetingexclusive",
3+
"version": "1.0",
4+
"name": "Event Based Greeting Workflow",
5+
"description": "Event Based Greeting",
6+
"specVersion": "0.7",
7+
"start": {
8+
"stateName": "Greet"
9+
},
10+
"events": [
11+
{
12+
"name": "GreetingEvent",
13+
"type": "greetingEventType",
14+
"source": "greetingEventSource"
15+
},
16+
{
17+
"name": "GreetingEvent2",
18+
"type": "greetingEventType2",
19+
"source": "greetingEventSource2"
20+
}
21+
],
22+
"functions": [
23+
{
24+
"name": "greetingFunction",
25+
"operation": "file://myapis/greetingapis.json#greeting"
26+
}
27+
],
28+
"states": [
29+
{
30+
"name": "Greet",
31+
"type": "event",
32+
"exclusive": true,
33+
"onEvents": [
34+
{
35+
"eventRefs": [
36+
"GreetingEvent"
37+
],
38+
"eventDataFilter": {
39+
"data": "{{ $.data.greet }}"
40+
},
41+
"actions": [
42+
{
43+
"functionRef": {
44+
"refName": "greetingFunction",
45+
"arguments": {
46+
"name": "{{ $.greet.name }}"
47+
}
48+
}
49+
}
50+
]
51+
},
52+
{
53+
"eventRefs": [
54+
"GreetingEvent2"
55+
],
56+
"eventDataFilter": {
57+
"data": "{{ $.data.greet2 }}"
58+
},
59+
"actions": [
60+
{
61+
"functionRef": {
62+
"refName": "greetingFunction2",
63+
"arguments": {
64+
"name": "{{ $.greet.name }}"
65+
}
66+
}
67+
}
68+
]
69+
}
70+
],
71+
"stateDataFilter": {
72+
"output": "{{ $.payload.greeting }}"
73+
},
74+
"end": {
75+
"terminate": true
76+
}
77+
}
78+
]
79+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"id": "eventbasedgreetingnonexclusive",
3+
"version": "1.0",
4+
"name": "Event Based Greeting Workflow",
5+
"description": "Event Based Greeting",
6+
"specVersion": "0.7",
7+
"start": {
8+
"stateName": "Greet"
9+
},
10+
"events": [
11+
{
12+
"name": "GreetingEvent",
13+
"type": "greetingEventType",
14+
"source": "greetingEventSource"
15+
},
16+
{
17+
"name": "GreetingEvent2",
18+
"type": "greetingEventType2",
19+
"source": "greetingEventSource2"
20+
}
21+
],
22+
"functions": [
23+
{
24+
"name": "greetingFunction",
25+
"operation": "file://myapis/greetingapis.json#greeting"
26+
}
27+
],
28+
"states": [
29+
{
30+
"name": "Greet",
31+
"type": "event",
32+
"exclusive": false,
33+
"onEvents": [
34+
{
35+
"eventRefs": [
36+
"GreetingEvent",
37+
"GreetingEvent2"
38+
],
39+
"eventDataFilter": {
40+
"data": "{{ $.data.greet }}"
41+
},
42+
"actions": [
43+
{
44+
"functionRef": {
45+
"refName": "greetingFunction",
46+
"arguments": {
47+
"name": "{{ $.greet.name }}"
48+
}
49+
}
50+
}
51+
]
52+
}
53+
],
54+
"stateDataFilter": {
55+
"output": "{{ $.payload.greeting }}"
56+
},
57+
"end": {
58+
"terminate": true
59+
}
60+
}
61+
]
62+
}

0 commit comments

Comments
 (0)