-
Notifications
You must be signed in to change notification settings - Fork 49
feat(eventState): add default value sequential
for OnEvents.ActionMode
#96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ricardozanini
merged 1 commit into
serverlessworkflow:main
from
lsytj0413:split-event-state
Oct 5, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2022 The Serverless Workflow Specification Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package model | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// EventState used to wait for events from event sources, then consumes them and invoke one or more actions to run in sequence or parallel | ||
type EventState struct { | ||
// TODO: EventState doesn't have usedForCompensation field. | ||
BaseState | ||
|
||
// 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 | ||
// Defaults to true | ||
Exclusive bool `json:"exclusive,omitempty"` | ||
// Define the events to be consumed and optional actions to be performed | ||
OnEvents []OnEvents `json:"onEvents" validate:"required,min=1,dive"` | ||
// State specific timeouts | ||
Timeout *EventStateTimeout `json:"timeouts,omitempty"` | ||
} | ||
|
||
type eventStateForUnmarshal EventState | ||
|
||
// UnmarshalJSON unmarshal EventState object from json bytes | ||
func (e *EventState) UnmarshalJSON(data []byte) error { | ||
v := eventStateForUnmarshal{ | ||
Exclusive: true, | ||
} | ||
err := json.Unmarshal(data, &v) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*e = EventState(v) | ||
return nil | ||
} | ||
|
||
// OnEvents define which actions are be be performed for the one or more events. | ||
type OnEvents struct { | ||
// References one or more unique event names in the defined workflow events | ||
EventRefs []string `json:"eventRefs" validate:"required,min=1"` | ||
// Specifies how actions are to be performed (in sequence or parallel) | ||
// Defaults to sequential | ||
ActionMode ActionMode `json:"actionMode,omitempty" validate:"required,oneof=sequential parallel"` | ||
// Actions to be performed if expression matches | ||
Actions []Action `json:"actions,omitempty" validate:"omitempty,dive"` | ||
// Event data filter | ||
EventDataFilter EventDataFilter `json:"eventDataFilter,omitempty"` | ||
} | ||
|
||
type onEventsForUnmarshal OnEvents | ||
|
||
// UnmarshalJSON unmarshal OnEvents object from json bytes | ||
func (o *OnEvents) UnmarshalJSON(data []byte) error { | ||
v := onEventsForUnmarshal{ | ||
ActionMode: ActionModeSequential, | ||
} | ||
|
||
err := json.Unmarshal(data, &v) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*o = OnEvents(v) | ||
return nil | ||
} | ||
|
||
// EventStateTimeout defines timeout settings for event state | ||
type EventStateTimeout struct { | ||
StateExecTimeout StateExecTimeout `json:"stateExecTimeout,omitempty"` | ||
ActionExecTimeout string `json:"actionExecTimeout,omitempty" validate:"omitempty,iso8601duration"` | ||
EventTimeout string `json:"eventTimeout,omitempty" validate:"omitempty,iso8601duration"` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright 2022 The Serverless Workflow Specification Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package model | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEventStateUnmarshalJSON(t *testing.T) { | ||
type testCase struct { | ||
desp string | ||
data string | ||
expect EventState | ||
err string | ||
} | ||
testCases := []testCase{ | ||
{ | ||
desp: "all fields set", | ||
data: `{"name": "1", "Type": "event", "exclusive": false, "onEvents": [{"eventRefs": ["E1", "E2"], "actionMode": "parallel"}], "timeouts": {"actionExecTimeout": "PT5M", "eventTimeout": "PT5M", "stateExecTimeout": "PT5M"}}`, | ||
expect: EventState{ | ||
BaseState: BaseState{ | ||
Name: "1", | ||
Type: StateTypeEvent, | ||
}, | ||
Exclusive: false, | ||
OnEvents: []OnEvents{ | ||
{ | ||
EventRefs: []string{"E1", "E2"}, | ||
ActionMode: "parallel", | ||
}, | ||
}, | ||
Timeout: &EventStateTimeout{ | ||
EventTimeout: "PT5M", | ||
ActionExecTimeout: "PT5M", | ||
StateExecTimeout: StateExecTimeout{ | ||
Total: "PT5M", | ||
}, | ||
}, | ||
}, | ||
err: ``, | ||
}, | ||
{ | ||
desp: "default exclusive", | ||
data: `{"name": "1", "Type": "event", "onEvents": [{"eventRefs": ["E1", "E2"], "actionMode": "parallel"}], "timeouts": {"actionExecTimeout": "PT5M", "eventTimeout": "PT5M", "stateExecTimeout": "PT5M"}}`, | ||
expect: EventState{ | ||
BaseState: BaseState{ | ||
Name: "1", | ||
Type: StateTypeEvent, | ||
}, | ||
Exclusive: true, | ||
OnEvents: []OnEvents{ | ||
{ | ||
EventRefs: []string{"E1", "E2"}, | ||
ActionMode: "parallel", | ||
}, | ||
}, | ||
Timeout: &EventStateTimeout{ | ||
EventTimeout: "PT5M", | ||
ActionExecTimeout: "PT5M", | ||
StateExecTimeout: StateExecTimeout{ | ||
Total: "PT5M", | ||
}, | ||
}, | ||
}, | ||
err: ``, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.desp, func(t *testing.T) { | ||
v := EventState{} | ||
err := json.Unmarshal([]byte(tc.data), &v) | ||
|
||
if tc.err != "" { | ||
assert.Error(t, err) | ||
assert.Regexp(t, tc.err, err) | ||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expect, v) | ||
}) | ||
} | ||
} | ||
|
||
func TestOnEventsUnmarshalJSON(t *testing.T) { | ||
type testCase struct { | ||
desp string | ||
data string | ||
expect OnEvents | ||
err string | ||
} | ||
testCases := []testCase{ | ||
{ | ||
desp: "all fields set", | ||
data: `{"eventRefs": ["E1", "E2"], "actionMode": "parallel"}`, | ||
expect: OnEvents{ | ||
EventRefs: []string{"E1", "E2"}, | ||
ActionMode: ActionModeParallel, | ||
}, | ||
err: ``, | ||
}, | ||
{ | ||
desp: "default action mode", | ||
data: `{"eventRefs": ["E1", "E2"]}`, | ||
expect: OnEvents{ | ||
EventRefs: []string{"E1", "E2"}, | ||
ActionMode: ActionModeSequential, | ||
}, | ||
err: ``, | ||
}, | ||
{ | ||
desp: "invalid object format", | ||
data: `"eventRefs": ["E1", "E2"], "actionMode": "parallel"}`, | ||
expect: OnEvents{}, | ||
err: `invalid character ':' after top-level value`, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.desp, func(t *testing.T) { | ||
v := OnEvents{} | ||
err := json.Unmarshal([]byte(tc.data), &v) | ||
|
||
if tc.err != "" { | ||
assert.Error(t, err) | ||
assert.Regexp(t, tc.err, err) | ||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expect, v) | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason to break it into two
const
blocks?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as #97 (comment)