Skip to content

states - unsupported AST kind *ast.InterfaceType #148

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
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion model/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Action struct {
// References a sub-workflow to be executed
SubFlowRef *WorkflowRef `json:"subFlowRef,omitempty"`
// Sleep Defines time period workflow execution should sleep before / after function execution
Sleep Sleep `json:"sleep,omitempty"`
Sleep *Sleep `json:"sleep,omitempty"`
// RetryRef References a defined workflow retry definition. If not defined the default retry policy is assumed
RetryRef string `json:"retryRef,omitempty"`
// List of unique references to defined workflow errors for which the action should not be retried. Used only when `autoRetries` is set to `true`
Expand Down
21 changes: 16 additions & 5 deletions model/callback_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,33 @@

package model

import (
"encoding/json"
)

// CallbackState executes a function and waits for callback event that indicates
// completion of the task.
type CallbackState struct {
BaseState
// Defines the action to be executed
Action Action `json:"action" validate:"required"`
// References a unique callback event name in the defined workflow events
EventRef string `json:"eventRef" validate:"required"`
// Time period to wait for incoming events (ISO 8601 format)
Timeouts *CallbackStateTimeout `json:"timeouts" validate:"omitempty"`
Timeouts *CallbackStateTimeout `json:"timeouts,omitempty"`
// Event data filter
EventDataFilter EventDataFilter `json:"eventDataFilter,omitempty"`
EventDataFilter *EventDataFilter `json:"eventDataFilter,omitempty"`
}

func (in *CallbackState) DeepCopyState() State {
return in
func (c *CallbackState) MarshalJSON() ([]byte, error) {
type Alias CallbackState
custom, err := json.Marshal(&struct {
*Alias
Timeouts *CallbackStateTimeout `json:"timeouts,omitempty"`
}{
Alias: (*Alias)(c),
Timeouts: c.Timeouts,
})
return custom, err
}

// CallbackStateTimeout defines timeout settings for callback state
Expand Down
93 changes: 93 additions & 0 deletions model/callback_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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 (
"testing"

"github.com/stretchr/testify/assert"

val "github.com/serverlessworkflow/sdk-go/v2/validator"
)

func TestCallbackStateStructLevelValidation(t *testing.T) {
type testCase struct {
desp string
callbackStateObj State
err string
}
testCases := []testCase{
{
desp: "normal",
callbackStateObj: State{
BaseState: BaseState{
Name: "callbackTest",
Type: StateTypeCallback,
},
CallbackState: &CallbackState{
Action: Action{
ID: "1",
Name: "action1",
},
EventRef: "refExample",
},
},
err: ``,
},
{
desp: "missing required EventRef",
callbackStateObj: State{
BaseState: BaseState{
Name: "callbackTest",
Type: StateTypeCallback,
},
CallbackState: &CallbackState{
Action: Action{
ID: "1",
Name: "action1",
},
},
},
err: `Key: 'State.CallbackState.EventRef' Error:Field validation for 'EventRef' failed on the 'required' tag`,
},
// TODO need to register custom types - will be fixed by https://github.com/serverlessworkflow/sdk-go/issues/151
//{
// desp: "missing required Action",
// callbackStateObj: State{
// BaseState: BaseState{
// Name: "callbackTest",
// Type: StateTypeCallback,
// },
// CallbackState: &CallbackState{
// EventRef: "refExample",
// },
// },
// err: `Key: 'State.CallbackState.Action' Error:Field validation for 'Action' failed on the 'required' tag`,
//},
}
for _, tc := range testCases {
t.Run(tc.desp, func(t *testing.T) {
err := val.GetValidator().Struct(&tc.callbackStateObj)

if tc.err != "" {
assert.Error(t, err)
assert.Regexp(t, tc.err, err)
return
}

assert.NoError(t, err)
})
}
}
12 changes: 9 additions & 3 deletions model/delay_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@

package model

import "encoding/json"

// DelayState Causes the workflow execution to delay for a specified duration
type DelayState struct {
BaseState
// Amount of time (ISO 8601 format) to delay
TimeDelay string `json:"timeDelay" validate:"required,iso8601duration"`
}

func (in *DelayState) DeepCopyState() State {
return in
func (a *DelayState) MarshalJSON() ([]byte, error) {
custom, err := json.Marshal(&struct {
TimeDelay string `json:"timeDelay" validate:"required,iso8601duration"`
}{
TimeDelay: a.TimeDelay,
})
return custom, err
}
24 changes: 15 additions & 9 deletions model/delay_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,48 @@ import (
func TestDelayStateStructLevelValidation(t *testing.T) {
type testCase struct {
desp string
delayStateObj DelayState
delayStateObj State
err string
}
testCases := []testCase{
{
desp: "normal",
delayStateObj: DelayState{
delayStateObj: State{
BaseState: BaseState{
Name: "1",
Type: "delay",
},
TimeDelay: "PT5S",
DelayState: &DelayState{
TimeDelay: "PT5S",
},
},
err: ``,
},
{
desp: "missing required timeDelay",
delayStateObj: DelayState{
delayStateObj: State{
BaseState: BaseState{
Name: "1",
Type: "delay",
},
TimeDelay: "",
DelayState: &DelayState{
TimeDelay: "",
},
},
err: `Key: 'DelayState.TimeDelay' Error:Field validation for 'TimeDelay' failed on the 'required' tag`,
err: `Key: 'State.DelayState.TimeDelay' Error:Field validation for 'TimeDelay' failed on the 'required' tag`,
},
{
desp: "invalid timeDelay duration",
delayStateObj: DelayState{
delayStateObj: State{
BaseState: BaseState{
Name: "1",
Type: "delay",
},
TimeDelay: "P5S",
DelayState: &DelayState{
TimeDelay: "P5S",
},
},
err: `Key: 'DelayState.TimeDelay' Error:Field validation for 'TimeDelay' failed on the 'iso8601duration' tag`,
err: `Key: 'State.DelayState.TimeDelay' Error:Field validation for 'TimeDelay' failed on the 'iso8601duration' tag`,
},
}
for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type EventRef struct {
ResultEventTimeout string `json:"resultEventTimeout,omitempty" validate:"omitempty,iso8601duration"`
// If string type, an expression which selects parts of the states data output to become the data (payload) of the event referenced by 'triggerEventRef'.
// If object type, a custom object to become the data (payload) of the event referenced by 'triggerEventRef'.
Data Object `json:"data,omitempty"`
Data *Object `json:"data,omitempty"`
// Add additional extension context attributes to the produced event
ContextAttributes map[string]Object `json:"contextAttributes,omitempty"`
// Invoke specifies if the subflow should be invoked sync or async.
Expand Down
8 changes: 4 additions & 4 deletions model/event_data_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (

// EventDataFilter used to filter consumed event payloads.
type EventDataFilter struct {
// UseData represent where event payload is added/merged to state data. If it's false, data & toStateData should be ignored.
// Defaults to true.
// UseData represent where event payload is added/merged to state data. If it's false, data & toStateData
// should be ignored. Defaults to true.
UseData bool `json:"useData,omitempty"`

// Workflow expression that filters of the event data (payload)
Data string `json:"data,omitempty"`
// Workflow expression that selects a state data element to which the event payload should be added/merged into. If not specified, denotes, the top-level state data element.
// Workflow expression that selects a state data element to which the event payload should be added/merged into.
// If not specified, denotes, the top-level state data element.
ToStateData string `json:"toStateData,omitempty"`
}

Expand Down
14 changes: 11 additions & 3 deletions model/event_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
// 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 the defined events must be consumed in order for actions to be performed
Expand All @@ -34,8 +33,16 @@ type EventState struct {
Timeouts *EventStateTimeout `json:"timeouts,omitempty"`
}

func (e *EventState) DeepCopyState() State {
return e
func (e *EventState) MarshalJSON() ([]byte, error) {
type Alias EventState
custom, err := json.Marshal(&struct {
*Alias
Timeouts *EventStateTimeout `json:"timeouts,omitempty"`
}{
Alias: (*Alias)(e),
Timeouts: e.Timeouts,
})
return custom, err
}

type eventStateForUnmarshal EventState
Expand Down Expand Up @@ -81,6 +88,7 @@ func (o *OnEvents) UnmarshalJSON(data []byte) error {
}

*o = OnEvents(v)

return nil
}

Expand Down
60 changes: 32 additions & 28 deletions model/event_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,55 +25,59 @@ func TestEventStateUnmarshalJSON(t *testing.T) {
type testCase struct {
desp string
data string
expect EventState
expect State
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{
data: `{"name": "1", "type": "event", "exclusive": false, "onEvents": [{"eventRefs": ["E1", "E2"], "actionMode": "parallel"}], "timeouts": {"actionExecTimeout": "PT5M", "eventTimeout": "PT5M", "stateExecTimeout": "PT5M"}}`,
expect: State{
BaseState: BaseState{
Name: "1",
Type: StateTypeEvent,
},
Exclusive: false,
OnEvents: []OnEvents{
{
EventRefs: []string{"E1", "E2"},
ActionMode: "parallel",
EventState: &EventState{
Exclusive: false,
OnEvents: []OnEvents{
{
EventRefs: []string{"E1", "E2"},
ActionMode: "parallel",
},
},
},
Timeouts: &EventStateTimeout{
EventTimeout: "PT5M",
ActionExecTimeout: "PT5M",
StateExecTimeout: &StateExecTimeout{
Total: "PT5M",
Timeouts: &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{
data: `{"name": "1", "type": "event", "onEvents": [{"eventRefs": ["E1", "E2"], "actionMode": "parallel"}], "timeouts": {"actionExecTimeout": "PT5M", "eventTimeout": "PT5M", "stateExecTimeout": "PT5M"}}`,
expect: State{
BaseState: BaseState{
Name: "1",
Type: StateTypeEvent,
},
Exclusive: true,
OnEvents: []OnEvents{
{
EventRefs: []string{"E1", "E2"},
ActionMode: "parallel",
EventState: &EventState{
Exclusive: true,
OnEvents: []OnEvents{
{
EventRefs: []string{"E1", "E2"},
ActionMode: "parallel",
},
},
},
Timeouts: &EventStateTimeout{
EventTimeout: "PT5M",
ActionExecTimeout: "PT5M",
StateExecTimeout: &StateExecTimeout{
Total: "PT5M",
Timeouts: &EventStateTimeout{
EventTimeout: "PT5M",
ActionExecTimeout: "PT5M",
StateExecTimeout: &StateExecTimeout{
Total: "PT5M",
},
},
},
},
Expand All @@ -82,7 +86,7 @@ func TestEventStateUnmarshalJSON(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.desp, func(t *testing.T) {
v := EventState{}
v := State{}
err := json.Unmarshal([]byte(tc.data), &v)

if tc.err != "" {
Expand Down
Loading