Skip to content

Commit

Permalink
Fixes workflow event states unmarshalling methods (#65)
Browse files Browse the repository at this point in the history
* clear mapState after next loop iteraction

* signed commit

* signed commit

* added unit test for both events at once
  • Loading branch information
andresmijares authored Aug 29, 2022
1 parent 98db3f0 commit c6596fd
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin
.idea
*.out
*.out
.vscode
3 changes: 2 additions & 1 deletion model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (w *Workflow) UnmarshalJSON(data []byte) error {
}

w.States = make([]State, len(rawStates))
var mapState map[string]interface{}
mapState := map[string]interface{}{}
for i, rawState := range rawStates {
if err := json.Unmarshal(rawState, &mapState); err != nil {
return err
Expand All @@ -130,6 +130,7 @@ func (w *Workflow) UnmarshalJSON(data []byte) error {
return err
}
w.States[i] = state
mapState = map[string]interface{}{}
}
if _, ok := workflowMap["events"]; ok {
if err := json.Unmarshal(workflowMap["events"], &w.Events); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func TestFromFile(t *testing.T) {
assert.NotNil(t, w.States[0].(*model.OperationState).Actions[0].FunctionRef)
assert.Equal(t, "greetingFunction", w.States[0].(*model.OperationState).Actions[0].FunctionRef.RefName)
},
"./testdata/workflows/eventbaseddataandswitch.sw.json": func(t *testing.T, w *model.Workflow) {
assert.Equal(t, "Start", w.States[0].GetName())
assert.Equal(t, "CheckVisaStatus", w.States[1].GetName())
assert.IsType(t, &model.DataBasedSwitchState{}, w.States[0])
assert.IsType(t, &model.EventBasedSwitchState{}, w.States[1])
},
"./testdata/workflows/eventbasedgreeting.sw.json": func(t *testing.T, w *model.Workflow) {
assert.Equal(t, "GreetingEvent", w.Events[0].Name)
assert.IsType(t, &model.EventState{}, w.States[0])
Expand Down
100 changes: 100 additions & 0 deletions parser/testdata/workflows/eventbaseddataandswitch.sw.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"id": "eventbaseddataandswitch",
"version": "1.0",
"name": "Event Based Switch Transitions",
"description": "Event Based Switch Transitions with Event Database Condition",
"specVersion": "0.7",
"start": {
"stateName": "Start"
},
"events": [
{
"name": "visaApprovedEvent",
"type": "VisaApproved",
"source": "visaCheckSource"
},
{
"name": "visaRejectedEvent",
"type": "VisaRejected",
"source": "visaCheckSource"
}
],
"states": [
{
"name": "Start",
"type": "switch",
"dataConditions": [
{
"condition": "${ true }",
"transition": "CheckVisaStatus"
}
]
},
{
"name": "CheckVisaStatus",
"type": "switch",
"eventConditions": [
{
"eventRef": "visaApprovedEvent",
"transition": {
"nextState": "HandleApprovedVisa"
}
},
{
"eventRef": "visaRejectedEvent",
"transition": {
"nextState": "HandleRejectedVisa"
}
}
],
"eventTimeout": "PT1H",
"defaultCondition": {
"transition": {
"nextState": "HandleNoVisaDecision"
}
}
},
{
"name": "HandleApprovedVisa",
"type": "operation",
"actions": [
{
"subFlowRef": {
"workflowId": "handleApprovedVisaWorkflowID"
}
}
],
"end": {
"terminate": true
}
},
{
"name": "HandleRejectedVisa",
"type": "operation",
"actions": [
{
"subFlowRef": {
"workflowId": "handleRejectedVisaWorkflowID"
}
}
],
"end": {
"terminate": true
}
},
{
"name": "HandleNoVisaDecision",
"type": "operation",
"actions": [
{
"subFlowRef": {
"workflowId": "handleNoVisaDecisionWorkfowId"
}
}
],
"end": {
"terminate": true
}
}
]
}

0 comments on commit c6596fd

Please sign in to comment.