Skip to content

Commit e83573b

Browse files
authored
fix nill state timeouts and enhance v08 test (#104)
* fix nill state timeouts and enhance v08 test Signed-off-by: spolti <filippespolti@gmail.com> * review update Signed-off-by: spolti <filippespolti@gmail.com> * review changes Signed-off-by: spolti <filippespolti@gmail.com> * correct timeout declaration Signed-off-by: spolti <filippespolti@gmail.com> * review updates Signed-off-by: spolti <filippespolti@gmail.com> * review additions Signed-off-by: spolti <filippespolti@gmail.com> Signed-off-by: spolti <filippespolti@gmail.com>
1 parent b9db1dd commit e83573b

16 files changed

+390
-101
lines changed

model/action.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ type actionForUnmarshal Action
5050

5151
// UnmarshalJSON implements json.Unmarshaler
5252
func (a *Action) UnmarshalJSON(data []byte) error {
53+
5354
v := actionForUnmarshal{
5455
ActionDataFilter: ActionDataFilter{UseResults: true},
5556
}
5657
err := json.Unmarshal(data, &v)
5758
if err != nil {
58-
return err
59+
return fmt.Errorf("action value '%s' is not supported, it must be an object or string", string(data))
5960
}
6061
*a = Action(v)
62+
6163
return nil
6264
}
6365

@@ -101,8 +103,7 @@ func (f *FunctionRef) UnmarshalJSON(data []byte) error {
101103
}
102104
err = json.Unmarshal(data, &v)
103105
if err != nil {
104-
// TODO: replace the error message with correct type's name
105-
return err
106+
return fmt.Errorf("functionRef value '%s' is not supported, it must be an object or string", string(data))
106107
}
107108
*f = FunctionRef(v)
108109
return nil

model/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (a *AuthDefinitions) UnmarshalJSON(b []byte) error {
113113
func (a *AuthDefinitions) unmarshalFile(data []byte) error {
114114
b, err := unmarshalFile(data)
115115
if err != nil {
116-
return err
116+
return fmt.Errorf("authDefinitions value '%s' is not supported, it must be an object or string", string(data))
117117
}
118118

119119
return a.unmarshalMany(b)
@@ -123,7 +123,7 @@ func (a *AuthDefinitions) unmarshalMany(data []byte) error {
123123
var auths []Auth
124124
err := json.Unmarshal(data, &auths)
125125
if err != nil {
126-
return err
126+
return fmt.Errorf("authDefinitions value '%s' is not supported, it must be an object or string", string(data))
127127
}
128128

129129
a.Defs = auths

model/delay_state.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,9 @@
1414

1515
package model
1616

17-
import (
18-
"github.com/go-playground/validator/v10"
19-
20-
val "github.com/serverlessworkflow/sdk-go/v2/validator"
21-
)
22-
23-
func init() {
24-
val.GetValidator().RegisterStructValidation(
25-
DelayStateStructLevelValidation,
26-
DelayState{},
27-
)
28-
}
29-
3017
// DelayState Causes the workflow execution to delay for a specified duration
3118
type DelayState struct {
3219
BaseState
3320
// Amount of time (ISO 8601 format) to delay
3421
TimeDelay string `json:"timeDelay" validate:"required,iso8601duration"`
3522
}
36-
37-
// DelayStateStructLevelValidation custom validator for DelayState Struct
38-
func DelayStateStructLevelValidation(structLevel validator.StructLevel) {
39-
// TODO
40-
}

model/event.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ type EventRef struct {
9696
TriggerEventRef string `json:"triggerEventRef" validate:"required"`
9797
// Reference to the unique name of a 'consumed' event definition
9898
ResultEventRef string `json:"resultEventRef" validate:"required"`
99-
10099
// ResultEventTimeout defines maximum amount of time (ISO 8601 format) to wait for the result event. If not defined it be set to the actionExecutionTimeout
101100
ResultEventTimeout string `json:"resultEventTimeout,omitempty" validate:"omitempty,iso8601duration"`
102-
103101
// TODO: create StringOrMap structure
104102
// If string type, an expression which selects parts of the states data output to become the data (payload) of the event referenced by 'triggerEventRef'.
105103
// If object type, a custom object to become the data (payload) of the event referenced by 'triggerEventRef'.

model/event_state.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,40 @@ package model
1616

1717
import (
1818
"encoding/json"
19+
"fmt"
1920
)
2021

2122
// EventState used to wait for events from event sources, then consumes them and invoke one or more actions to run in sequence or parallel
2223
type EventState struct {
2324
// TODO: EventState doesn't have usedForCompensation field.
2425
BaseState
2526

26-
// 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
27+
// If true consuming one of the defined events causes its associated actions to be performed.
28+
// If false all the defined events must be consumed in order for actions to be performed
2729
// Defaults to true
2830
Exclusive bool `json:"exclusive,omitempty"`
2931
// Define the events to be consumed and optional actions to be performed
3032
OnEvents []OnEvents `json:"onEvents" validate:"required,min=1,dive"`
3133
// State specific timeouts
32-
Timeout *EventStateTimeout `json:"timeouts,omitempty"`
34+
Timeouts *EventStateTimeout `json:"timeouts,omitempty"`
3335
}
3436

3537
type eventStateForUnmarshal EventState
3638

3739
// UnmarshalJSON unmarshal EventState object from json bytes
3840
func (e *EventState) UnmarshalJSON(data []byte) error {
41+
// var timeout EventStateTimeout
42+
// if err := json.Unmarshal(data, &timeout); err != nil {
43+
// return err
44+
// }
45+
3946
v := eventStateForUnmarshal{
4047
Exclusive: true,
48+
// Timeouts: &timeout,
4149
}
4250
err := json.Unmarshal(data, &v)
4351
if err != nil {
44-
return err
52+
return fmt.Errorf("eventState value '%s' is not supported, it must be an object or string", string(data))
4553
}
4654

4755
*e = EventState(v)
@@ -71,7 +79,7 @@ func (o *OnEvents) UnmarshalJSON(data []byte) error {
7179

7280
err := json.Unmarshal(data, &v)
7381
if err != nil {
74-
return err
82+
return fmt.Errorf("onEvents value '%s' is not supported, it must be an object or string", string(data))
7583
}
7684

7785
*o = OnEvents(v)
@@ -80,7 +88,7 @@ func (o *OnEvents) UnmarshalJSON(data []byte) error {
8088

8189
// EventStateTimeout defines timeout settings for event state
8290
type EventStateTimeout struct {
83-
StateExecTimeout StateExecTimeout `json:"stateExecTimeout,omitempty"`
84-
ActionExecTimeout string `json:"actionExecTimeout,omitempty" validate:"omitempty,iso8601duration"`
85-
EventTimeout string `json:"eventTimeout,omitempty" validate:"omitempty,iso8601duration"`
91+
StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"`
92+
ActionExecTimeout string `json:"actionExecTimeout,omitempty" validate:"omitempty,iso8601duration"`
93+
EventTimeout string `json:"eventTimeout,omitempty" validate:"omitempty,iso8601duration"`
8694
}

model/event_state_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ func TestEventStateUnmarshalJSON(t *testing.T) {
4444
ActionMode: "parallel",
4545
},
4646
},
47-
Timeout: &EventStateTimeout{
47+
Timeouts: &EventStateTimeout{
4848
EventTimeout: "PT5M",
4949
ActionExecTimeout: "PT5M",
50-
StateExecTimeout: StateExecTimeout{
50+
StateExecTimeout: &StateExecTimeout{
5151
Total: "PT5M",
5252
},
5353
},
@@ -69,10 +69,10 @@ func TestEventStateUnmarshalJSON(t *testing.T) {
6969
ActionMode: "parallel",
7070
},
7171
},
72-
Timeout: &EventStateTimeout{
72+
Timeouts: &EventStateTimeout{
7373
EventTimeout: "PT5M",
7474
ActionExecTimeout: "PT5M",
75-
StateExecTimeout: StateExecTimeout{
75+
StateExecTimeout: &StateExecTimeout{
7676
Total: "PT5M",
7777
},
7878
},

model/retry.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ type Retry struct {
4545
Multiplier *floatstr.Float32OrString `json:"multiplier,omitempty" validate:"omitempty,min=1"`
4646
// Maximum number of retry attempts.
4747
MaxAttempts intstr.IntOrString `json:"maxAttempts" validate:"required"`
48-
4948
// If float type, maximum amount of random time added or subtracted from the delay between each retry relative to total delay (between 0 and 1). If string type, absolute maximum amount of random time added or subtracted from the delay between each retry (ISO 8601 duration format)
5049
// TODO: make iso8601duration compatible this type
5150
Jitter floatstr.Float32OrString `json:"jitter,omitempty" validate:"omitempty,min=0,max=1"`

model/sleep_state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ type SleepState struct {
2626

2727
// SleepStateTimeout defines timeout settings for sleep state
2828
type SleepStateTimeout struct {
29-
StateExecTimeout StateExecTimeout `json:"stateExecTimeout,omitempty"`
29+
StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"`
3030
}

model/state_exec_timeout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ func (s *StateExecTimeout) UnmarshalJSON(data []byte) error {
4949
var v stateExecTimeoutForUnmarshal
5050
err = json.Unmarshal(data, &v)
5151
if err != nil {
52-
// TODO: replace the error message with correct type's name
5352
return err
5453
}
5554

5655
*s = StateExecTimeout(v)
56+
5757
return nil
5858
}
5959

model/states.go

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ package model
1616

1717
import (
1818
"encoding/json"
19+
"fmt"
20+
1921
"k8s.io/apimachinery/pkg/util/intstr"
2022
)
2123

2224
const (
23-
//StateTypeDelay ...
25+
// StateTypeDelay ...
2426
StateTypeDelay = "delay"
2527
// StateTypeEvent ...
2628
StateTypeEvent = "event"
@@ -50,6 +52,33 @@ const (
5052
ForEachModeTypeParallel ForEachModeType = "parallel"
5153
)
5254

55+
func getActionsModelMapping(stateType string, s map[string]interface{}) (State, bool) {
56+
switch stateType {
57+
case StateTypeDelay:
58+
return &DelayState{}, true
59+
case StateTypeEvent:
60+
return &EventState{}, true
61+
case StateTypeOperation:
62+
return &OperationState{}, true
63+
case StateTypeParallel:
64+
return &ParallelState{}, true
65+
case StateTypeSwitch:
66+
if _, ok := s["dataConditions"]; ok {
67+
return &DataBasedSwitchState{}, true
68+
}
69+
return &EventBasedSwitchState{}, true
70+
case StateTypeInject:
71+
return &InjectState{}, true
72+
case StateTypeForEach:
73+
return &ForEachState{}, true
74+
case StateTypeCallback:
75+
return &CallbackState{}, true
76+
case StateTypeSleep:
77+
return &SleepState{}, true
78+
}
79+
return nil, false
80+
}
81+
5382
// StateType ...
5483
type StateType string
5584

@@ -139,8 +168,8 @@ type OperationState struct {
139168

140169
// OperationStateTimeout ...
141170
type OperationStateTimeout struct {
142-
StateExecTimeout StateExecTimeout `json:"stateExecTimeout,omitempty"`
143-
ActionExecTimeout string `json:"actionExecTimeout,omitempty" validate:"omitempty,min=1"`
171+
StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"`
172+
ActionExecTimeout string `json:"actionExecTimeout,omitempty" validate:"omitempty,min=1"`
144173
}
145174

146175
// ParallelState Consists of a number of states that are executed in parallel
@@ -173,7 +202,7 @@ type InjectState struct {
173202

174203
// InjectStateTimeout ...
175204
type InjectStateTimeout struct {
176-
StateExecTimeout StateExecTimeout `json:"stateExecTimeout,omitempty"`
205+
StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"`
177206
}
178207

179208
// ForEachState ...
@@ -196,10 +225,25 @@ type ForEachState struct {
196225
Mode ForEachModeType `json:"mode,omitempty"`
197226
}
198227

228+
type forEachStateForUnmarshal ForEachState
229+
230+
func (f *ForEachState) UnmarshalJSON(data []byte) error {
231+
v := forEachStateForUnmarshal{
232+
Mode: StateTypeParallel,
233+
}
234+
err := json.Unmarshal(data, &v)
235+
if err != nil {
236+
return fmt.Errorf("forEachState value '%s' is not supported, it must be an object or string", string(data))
237+
}
238+
239+
*f = ForEachState(v)
240+
return nil
241+
}
242+
199243
// ForEachStateTimeout ...
200244
type ForEachStateTimeout struct {
201-
StateExecTimeout StateExecTimeout `json:"stateExecTimeout,omitempty"`
202-
ActionExecTimeout string `json:"actionExecTimeout,omitempty"`
245+
StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"`
246+
ActionExecTimeout string `json:"actionExecTimeout,omitempty"`
203247
}
204248

205249
// CallbackState ...
@@ -217,9 +261,9 @@ type CallbackState struct {
217261

218262
// CallbackStateTimeout ...
219263
type CallbackStateTimeout struct {
220-
StateExecTimeout StateExecTimeout `json:"stateExecTimeout,omitempty"`
221-
ActionExecTimeout string `json:"actionExecTimeout,omitempty"`
222-
EventTimeout string `json:"eventTimeout,omitempty"`
264+
StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"`
265+
ActionExecTimeout string `json:"actionExecTimeout,omitempty"`
266+
EventTimeout string `json:"eventTimeout,omitempty"`
223267
}
224268

225269
// BaseSwitchState ...
@@ -238,7 +282,7 @@ type EventBasedSwitchState struct {
238282
Timeouts *EventBasedSwitchStateTimeout `json:"timeouts,omitempty"`
239283
}
240284

241-
// UnmarshalJSON implementation for json Unmarshal function for the Eventbasedswitch type
285+
// UnmarshalJSON implementation for json Unmarshal function for the EventBasedSwitch type
242286
func (j *EventBasedSwitchState) UnmarshalJSON(data []byte) error {
243287
if err := json.Unmarshal(data, &j.BaseSwitchState); err != nil {
244288
return err
@@ -247,10 +291,12 @@ func (j *EventBasedSwitchState) UnmarshalJSON(data []byte) error {
247291
if err := json.Unmarshal(data, &eventBasedSwitch); err != nil {
248292
return err
249293
}
250-
var rawConditions []json.RawMessage
251-
if err := unmarshalKey("timeouts", eventBasedSwitch, &j.Timeouts); err != nil {
294+
295+
if err := json.Unmarshal(eventBasedSwitch["timeouts"], &j.Timeouts); err != nil {
252296
return err
253297
}
298+
299+
var rawConditions []json.RawMessage
254300
if err := json.Unmarshal(eventBasedSwitch["eventConditions"], &rawConditions); err != nil {
255301
return err
256302
}
@@ -272,13 +318,14 @@ func (j *EventBasedSwitchState) UnmarshalJSON(data []byte) error {
272318
}
273319
j.EventConditions[i] = condition
274320
}
321+
275322
return nil
276323
}
277324

278325
// EventBasedSwitchStateTimeout ...
279326
type EventBasedSwitchStateTimeout struct {
280-
StateExecTimeout StateExecTimeout `json:"stateExecTimeout,omitempty"`
281-
EventTimeout string `json:"eventTimeout,omitempty"`
327+
StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"`
328+
EventTimeout string `json:"eventTimeout,omitempty"`
282329
}
283330

284331
// EventCondition ...
@@ -333,7 +380,7 @@ type DataBasedSwitchState struct {
333380
Timeouts *DataBasedSwitchStateTimeout `json:"timeouts,omitempty"`
334381
}
335382

336-
// UnmarshalJSON implementation for json Unmarshal function for the Databasedswitch type
383+
// UnmarshalJSON implementation for json Unmarshal function for the DataBasedSwitch type
337384
func (j *DataBasedSwitchState) UnmarshalJSON(data []byte) error {
338385
if err := json.Unmarshal(data, &j.BaseSwitchState); err != nil {
339386
return err
@@ -342,10 +389,10 @@ func (j *DataBasedSwitchState) UnmarshalJSON(data []byte) error {
342389
if err := json.Unmarshal(data, &dataBasedSwitch); err != nil {
343390
return err
344391
}
345-
var rawConditions []json.RawMessage
346-
if err := unmarshalKey("timeouts", dataBasedSwitch, &j.Timeouts); err != nil {
392+
if err := json.Unmarshal(data, &j.Timeouts); err != nil {
347393
return err
348394
}
395+
var rawConditions []json.RawMessage
349396
if err := json.Unmarshal(dataBasedSwitch["dataConditions"], &rawConditions); err != nil {
350397
return err
351398
}
@@ -371,7 +418,7 @@ func (j *DataBasedSwitchState) UnmarshalJSON(data []byte) error {
371418

372419
// DataBasedSwitchStateTimeout ...
373420
type DataBasedSwitchStateTimeout struct {
374-
StateExecTimeout StateExecTimeout `json:"stateExecTimeout,omitempty"`
421+
StateExecTimeout *StateExecTimeout `json:"stateExecTimeout,omitempty"`
375422
}
376423

377424
// DataCondition ...

0 commit comments

Comments
 (0)