Skip to content

Commit 4be1862

Browse files
authored
v08 spec aligment - Added resultEventTimeout for action eventref (#90)
* v08 spec aligment - Added resultEventTimeout for action eventref Signed-off-by: spolti <filippespolti@gmail.com> * review update Signed-off-by: spolti <filippespolti@gmail.com> Signed-off-by: spolti <filippespolti@gmail.com>
1 parent ba75ee1 commit 4be1862

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Current status of features implemented in the SDK is listed in the table below:
99
| Parse workflow JSON and YAML definitions | :heavy_check_mark: |
1010
| Programmatically build workflow definitions | :no_entry_sign: |
1111
| Validate workflow definitions (Schema) | :heavy_check_mark: |
12-
| Validate workflow definitions (Integrity) | :heavy_check_mark: |
12+
| Validate workflow definitions (Integrity) | :heavy_check_mark: |
1313
| Generate workflow diagram (SVG) | :no_entry_sign: |
1414

1515
## Status

model/event.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
package model
1616

1717
import (
18+
val "github.com/serverlessworkflow/sdk-go/v2/validator"
1819
"reflect"
1920

2021
validator "github.com/go-playground/validator/v10"
21-
val "github.com/serverlessworkflow/sdk-go/v2/validator"
2222
)
2323

2424
const (
@@ -30,17 +30,29 @@ const (
3030

3131
func init() {
3232
val.GetValidator().RegisterStructValidation(EventStructLevelValidation, Event{})
33+
val.GetValidator().RegisterStructValidation(EventRefStructLevelValidation, EventRef{})
3334
}
3435

3536
// EventStructLevelValidation custom validator for event kind consumed
3637
func EventStructLevelValidation(structLevel validator.StructLevel) {
3738
event := structLevel.Current().Interface().(Event)
38-
3939
if event.Kind == EventKindConsumed && len(event.Type) == 0 {
4040
structLevel.ReportError(reflect.ValueOf(event.Type), "Type", "type", "reqtypeconsumed", "")
4141
}
4242
}
4343

44+
// EventRefStructLevelValidation custom validator for event kind consumed
45+
func EventRefStructLevelValidation(structLevel validator.StructLevel) {
46+
eventRef := structLevel.Current().Interface().(EventRef)
47+
48+
if len(eventRef.ResultEventTimeout) > 0 {
49+
err := val.ValidateISO8601TimeDuration(eventRef.ResultEventTimeout)
50+
if err != nil {
51+
structLevel.ReportError(reflect.ValueOf(eventRef.ResultEventTimeout), "ResultEventTimeout", "resultEventTimeout", "reqiso8601duration", "")
52+
}
53+
}
54+
}
55+
4456
// EventKind ...
4557
type EventKind string
4658

@@ -75,9 +87,15 @@ type EventRef struct {
7587
TriggerEventRef string `json:"triggerEventRef" validate:"required"`
7688
// Reference to the unique name of a 'consumed' event definition
7789
ResultEventRef string `json:"resultEventRef" validate:"required"`
90+
// Maximum amount of time (ISO 8601 format) to wait for the result event. If not defined it will be set to the 'actionExecTimeout'
91+
ResultEventTimeout string `json:"resultEventTimeout,omitempty"`
7892
// TODO: create StringOrMap structure
79-
// 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'.
93+
// If string type, an expression which selects parts of the states data output to become the data (payload) of the event referenced by 'triggerEventRef'.
94+
// If object type, a custom object to become the data (payload) of the event referenced by 'triggerEventRef'.
8095
Data interface{} `json:"data,omitempty"`
8196
// Add additional extension context attributes to the produced event
8297
ContextAttributes map[string]interface{} `json:"contextAttributes,omitempty"`
8398
}
99+
100+
// InvokeKind ...
101+
type InvokeKind string

model/event_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
val "github.com/serverlessworkflow/sdk-go/v2/validator"
19+
"github.com/stretchr/testify/assert"
20+
"testing"
21+
)
22+
23+
func TestEventRefStructLevelValidation(t *testing.T) {
24+
type testCase struct {
25+
name string
26+
eventRef EventRef
27+
err string
28+
}
29+
30+
testCases := []testCase{
31+
{
32+
name: "valid resultEventTimeout",
33+
eventRef: EventRef{
34+
TriggerEventRef: "example valid",
35+
ResultEventRef: "example valid",
36+
ResultEventTimeout: "PT1H",
37+
},
38+
err: ``,
39+
},
40+
{
41+
name: "invalid resultEventTimeout",
42+
eventRef: EventRef{
43+
TriggerEventRef: "example invalid",
44+
ResultEventRef: "example invalid red",
45+
ResultEventTimeout: "10hs",
46+
},
47+
err: `Key: 'EventRef.ResultEventTimeout' Error:Field validation for 'ResultEventTimeout' failed on the 'reqiso8601duration' tag`,
48+
},
49+
}
50+
51+
for _, tc := range testCases {
52+
t.Run(tc.name, func(t *testing.T) {
53+
err := val.GetValidator().Struct(tc.eventRef)
54+
55+
if tc.err != "" {
56+
assert.Error(t, err)
57+
assert.Regexp(t, tc.err, err)
58+
return
59+
}
60+
assert.NoError(t, err)
61+
})
62+
}
63+
}

parser/testdata/workflows/greetings-v08-spec.sw.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ states:
3535
name: "${ .greet | .name }"
3636
actionDataFilter:
3737
dataResultsPath: "${ .payload | .greeting }"
38+
- eventRef:
39+
triggerEventRef: example
40+
resultEventRef: example
41+
resultEventTimeout: PT1H
3842
stateDataFilter:
3943
dataOutputPath: "${ .greeting }"
4044
transition: SendTextForHighPriority

0 commit comments

Comments
 (0)