Skip to content

Commit bafff78

Browse files
authored
fix(121): split operation state to separate file (#124)
Signed-off-by: lsytj0413 <511121939@qq.com> Signed-off-by: lsytj0413 <511121939@qq.com>
1 parent 7723e03 commit bafff78

File tree

3 files changed

+125
-17
lines changed

3 files changed

+125
-17
lines changed

model/operation_state.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
package model
16+
17+
import (
18+
"encoding/json"
19+
)
20+
21+
// OperationState defines a set of actions to be performed in sequence or in parallel.
22+
type OperationState struct {
23+
BaseState
24+
// Specifies whether actions are performed in sequence or in parallel
25+
// Defaults to sequential
26+
ActionMode ActionMode `json:"actionMode,omitempty" validate:"required,oneof=sequential parallel"`
27+
// Actions to be performed
28+
Actions []Action `json:"actions" validate:"required,min=1,dive"`
29+
// State specific timeouts
30+
Timeouts *OperationStateTimeout `json:"timeouts,omitempty"`
31+
}
32+
33+
type operationStateForUnmarshal OperationState
34+
35+
// UnmarshalJSON unmarshal OperationState object from json bytes
36+
func (o *OperationState) UnmarshalJSON(data []byte) error {
37+
v := operationStateForUnmarshal{
38+
ActionMode: ActionModeSequential,
39+
}
40+
err := json.Unmarshal(data, &v)
41+
if err != nil {
42+
return err
43+
}
44+
45+
*o = OperationState(v)
46+
return nil
47+
}
48+
49+
// OperationStateTimeout defines the specific timeout settings for operation state
50+
type OperationStateTimeout struct {
51+
StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"`
52+
ActionExecTimeout string `json:"actionExecTimeout,omitempty" validate:"omitempty,iso8601duration"`
53+
}

model/operation_state_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
package model
16+
17+
import (
18+
"encoding/json"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestOperationStateUnmarshalJSON(t *testing.T) {
25+
type testCase struct {
26+
desp string
27+
data string
28+
expect OperationState
29+
err string
30+
}
31+
testCases := []testCase{
32+
{
33+
desp: "all fields set",
34+
data: `{"actionMode": "parallel"}`,
35+
expect: OperationState{
36+
ActionMode: ActionModeParallel,
37+
},
38+
err: ``,
39+
},
40+
{
41+
desp: "actionMode unset",
42+
data: `{}`,
43+
expect: OperationState{
44+
ActionMode: ActionModeSequential,
45+
},
46+
err: ``,
47+
},
48+
{
49+
desp: "invalid object format",
50+
data: `{"actionMode": parallel}`,
51+
expect: OperationState{
52+
ActionMode: ActionModeParallel,
53+
},
54+
err: `invalid character 'p' looking for beginning of value`,
55+
},
56+
}
57+
for _, tc := range testCases {
58+
t.Run(tc.desp, func(t *testing.T) {
59+
v := OperationState{}
60+
err := json.Unmarshal([]byte(tc.data), &v)
61+
62+
if tc.err != "" {
63+
assert.Error(t, err)
64+
assert.Regexp(t, tc.err, err)
65+
return
66+
}
67+
68+
assert.NoError(t, err)
69+
assert.Equal(t, tc.expect, v)
70+
})
71+
}
72+
}

model/states.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,6 @@ func (s *BaseState) GetStateDataFilter() *StateDataFilter { return s.StateDataFi
136136
// GetMetadata ...
137137
func (s *BaseState) GetMetadata() *Metadata { return s.Metadata }
138138

139-
// OperationState Defines actions be performed. Does not wait for incoming events
140-
type OperationState struct {
141-
BaseState
142-
// Specifies whether actions are performed in sequence or in parallel
143-
ActionMode ActionMode `json:"actionMode,omitempty"`
144-
// Actions to be performed
145-
Actions []Action `json:"actions" validate:"required,min=1,dive"`
146-
// State specific timeouts
147-
Timeouts *OperationStateTimeout `json:"timeouts,omitempty"`
148-
}
149-
150-
// OperationStateTimeout ...
151-
type OperationStateTimeout struct {
152-
StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"`
153-
ActionExecTimeout string `json:"actionExecTimeout,omitempty" validate:"omitempty,min=1"`
154-
}
155-
156139
// InjectState ...
157140
type InjectState struct {
158141
BaseState

0 commit comments

Comments
 (0)