Skip to content

Commit beb77e8

Browse files
authored
feat(*): add v0.8 action data filter's useResults field (#82)
Signed-off-by: lsytj0413 <511121939@qq.com> Signed-off-by: lsytj0413 <511121939@qq.com>
1 parent eb32b3b commit beb77e8

12 files changed

+267
-105
lines changed

model/action.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
// Action ...
22+
type Action struct {
23+
// Unique action definition name
24+
Name string `json:"name,omitempty"`
25+
FunctionRef *FunctionRef `json:"functionRef,omitempty"`
26+
// References a 'trigger' and 'result' reusable event definitions
27+
EventRef *EventRef `json:"eventRef,omitempty"`
28+
// References a sub-workflow to be executed
29+
SubFlowRef *WorkflowRef `json:"subFlowRef,omitempty"`
30+
// Sleep Defines time period workflow execution should sleep before / after function execution
31+
Sleep Sleep `json:"sleep,omitempty"`
32+
// RetryRef References a defined workflow retry definition. If not defined the default retry policy is assumed
33+
RetryRef string `json:"retryRef,omitempty"`
34+
// List of unique references to defined workflow errors for which the action should not be retried. Used only when `autoRetries` is set to `true`
35+
NonRetryableErrors []string `json:"nonRetryableErrors,omitempty" validate:"omitempty,min=1"`
36+
// List of unique references to defined workflow errors for which the action should be retried. Used only when `autoRetries` is set to `false`
37+
RetryableErrors []string `json:"retryableErrors,omitempty" validate:"omitempty,min=1"`
38+
// Action data filter
39+
ActionDataFilter ActionDataFilter `json:"actionDataFilter,omitempty"`
40+
}
41+
42+
// FunctionRef ...
43+
type FunctionRef struct {
44+
// Name of the referenced function
45+
RefName string `json:"refName" validate:"required"`
46+
// Function arguments
47+
Arguments map[string]interface{} `json:"arguments,omitempty"`
48+
// String containing a valid GraphQL selection set
49+
SelectionSet string `json:"selectionSet,omitempty"`
50+
}
51+
52+
// UnmarshalJSON ...
53+
func (f *FunctionRef) UnmarshalJSON(data []byte) error {
54+
funcRef := make(map[string]interface{})
55+
if err := json.Unmarshal(data, &funcRef); err != nil {
56+
f.RefName, err = unmarshalString(data)
57+
if err != nil {
58+
return err
59+
}
60+
return nil
61+
}
62+
63+
f.RefName = requiresNotNilOrEmpty(funcRef["refName"])
64+
if _, found := funcRef["arguments"]; found {
65+
f.Arguments = funcRef["arguments"].(map[string]interface{})
66+
}
67+
f.SelectionSet = requiresNotNilOrEmpty(funcRef["selectionSet"])
68+
69+
return nil
70+
}
71+
72+
// WorkflowRef holds a reference for a workflow definition
73+
type WorkflowRef struct {
74+
// Sub-workflow unique id
75+
WorkflowID string `json:"workflowId" validate:"required"`
76+
// Sub-workflow version
77+
Version string `json:"version,omitempty"`
78+
}
79+
80+
// UnmarshalJSON ...
81+
func (s *WorkflowRef) UnmarshalJSON(data []byte) error {
82+
subflowRef := make(map[string]json.RawMessage)
83+
if err := json.Unmarshal(data, &subflowRef); err != nil {
84+
s.WorkflowID, err = unmarshalString(data)
85+
if err != nil {
86+
return err
87+
}
88+
return nil
89+
}
90+
if err := unmarshalKey("version", subflowRef, &s.Version); err != nil {
91+
return err
92+
}
93+
if err := unmarshalKey("workflowId", subflowRef, &s.WorkflowID); err != nil {
94+
return err
95+
}
96+
97+
return nil
98+
}
99+
100+
// Sleep ...
101+
type Sleep struct {
102+
// Before Amount of time (ISO 8601 duration format) to sleep before function/subflow invocation. Does not apply if 'eventRef' is defined.
103+
Before string `json:"before,omitempty"`
104+
// After Amount of time (ISO 8601 duration format) to sleep after function/subflow invocation. Does not apply if 'eventRef' is defined.
105+
After string `json:"after,omitempty"`
106+
}

model/action_data_filter.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
"bytes"
19+
"encoding/json"
20+
"fmt"
21+
)
22+
23+
// ActionDataFilter used to filter action data results.
24+
type ActionDataFilter struct {
25+
// Workflow expression that selects state data that the state action can use
26+
FromStateData string `json:"fromStateData,omitempty"`
27+
28+
// UseResults represent where action data results is added/merged to state data. If it's false, results & toStateData should be ignored.
29+
// Defaults to true.
30+
UseResults bool `json:"useResults,omitempty"`
31+
32+
// Workflow expression that filters the actions' data results
33+
Results string `json:"results,omitempty"`
34+
// Workflow expression that selects a state data element to which the action results should be added/merged into. If not specified, denote, the top-level state data element
35+
ToStateData string `json:"toStateData,omitempty"`
36+
}
37+
38+
type actionDataFilterForUnmarshal ActionDataFilter
39+
40+
func (f *ActionDataFilter) UnmarshalJSON(data []byte) error {
41+
data = bytes.TrimSpace(data)
42+
if len(data) == 0 {
43+
return fmt.Errorf("no bytes to unmarshal")
44+
}
45+
46+
v := actionDataFilterForUnmarshal{
47+
UseResults: true,
48+
}
49+
err := json.Unmarshal(data, &v)
50+
if err != nil {
51+
// TODO: replace the error message with correct type's name
52+
return err
53+
}
54+
55+
*f = ActionDataFilter(v)
56+
return nil
57+
}

model/action_data_filter_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 TestActionDataFilterUnmarshalJSON(t *testing.T) {
25+
type testCase struct {
26+
desp string
27+
data string
28+
expect ActionDataFilter
29+
err string
30+
}
31+
testCases := []testCase{
32+
{
33+
desp: "normal test",
34+
data: `{"fromStateData": "1", "results": "2", "toStateData": "3"}`,
35+
expect: ActionDataFilter{
36+
FromStateData: "1",
37+
Results: "2",
38+
ToStateData: "3",
39+
UseResults: true,
40+
},
41+
err: ``,
42+
},
43+
{
44+
desp: "add UseData to false",
45+
data: `{"fromStateData": "1", "results": "2", "toStateData": "3", "useResults": false}`,
46+
expect: ActionDataFilter{
47+
FromStateData: "1",
48+
Results: "2",
49+
ToStateData: "3",
50+
UseResults: false,
51+
},
52+
err: ``,
53+
},
54+
{
55+
desp: "empty data",
56+
data: ` `,
57+
expect: ActionDataFilter{},
58+
err: `unexpected end of JSON input`,
59+
},
60+
{
61+
desp: "invalid json format",
62+
data: `{"fromStateData": 1, "results": "2", "toStateData": "3"}`,
63+
expect: ActionDataFilter{},
64+
err: `json: cannot unmarshal number into Go struct field actionDataFilterForUnmarshal.fromStateData of type string`,
65+
},
66+
}
67+
68+
for _, tc := range testCases {
69+
t.Run(tc.desp, func(t *testing.T) {
70+
var v ActionDataFilter
71+
err := json.Unmarshal([]byte(tc.data), &v)
72+
73+
if tc.err != "" {
74+
assert.Error(t, err)
75+
assert.Regexp(t, tc.err, err)
76+
return
77+
}
78+
79+
assert.NoError(t, err)
80+
assert.Equal(t, tc.expect, v)
81+
})
82+
}
83+
}

model/action_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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

model/function.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package model
1616

17-
import "encoding/json"
18-
1917
const (
2018
// FunctionTypeREST a combination of the function/service OpenAPI definition document URI and the particular service
2119
// operation that needs to be invoked, separated by a '#'.
@@ -55,33 +53,3 @@ type Function struct {
5553
// References an auth definition name to be used to access to resource defined in the operation parameter
5654
AuthRef string `json:"authRef,omitempty" validate:"omitempty,min=1"`
5755
}
58-
59-
// FunctionRef ...
60-
type FunctionRef struct {
61-
// Name of the referenced function
62-
RefName string `json:"refName" validate:"required"`
63-
// Function arguments
64-
Arguments map[string]interface{} `json:"arguments,omitempty"`
65-
// String containing a valid GraphQL selection set
66-
SelectionSet string `json:"selectionSet,omitempty"`
67-
}
68-
69-
// UnmarshalJSON ...
70-
func (f *FunctionRef) UnmarshalJSON(data []byte) error {
71-
funcRef := make(map[string]interface{})
72-
if err := json.Unmarshal(data, &funcRef); err != nil {
73-
f.RefName, err = unmarshalString(data)
74-
if err != nil {
75-
return err
76-
}
77-
return nil
78-
}
79-
80-
f.RefName = requiresNotNilOrEmpty(funcRef["refName"])
81-
if _, found := funcRef["arguments"]; found {
82-
f.Arguments = funcRef["arguments"].(map[string]interface{})
83-
}
84-
f.SelectionSet = requiresNotNilOrEmpty(funcRef["selectionSet"])
85-
86-
return nil
87-
}

model/workflow.go

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -206,34 +206,6 @@ func (w *Workflow) setDefaults() {
206206
}
207207
}
208208

209-
// WorkflowRef holds a reference for a workflow definition
210-
type WorkflowRef struct {
211-
// Sub-workflow unique id
212-
WorkflowID string `json:"workflowId" validate:"required"`
213-
// Sub-workflow version
214-
Version string `json:"version,omitempty"`
215-
}
216-
217-
// UnmarshalJSON ...
218-
func (s *WorkflowRef) UnmarshalJSON(data []byte) error {
219-
subflowRef := make(map[string]json.RawMessage)
220-
if err := json.Unmarshal(data, &subflowRef); err != nil {
221-
s.WorkflowID, err = unmarshalString(data)
222-
if err != nil {
223-
return err
224-
}
225-
return nil
226-
}
227-
if err := unmarshalKey("version", subflowRef, &s.Version); err != nil {
228-
return err
229-
}
230-
if err := unmarshalKey("workflowId", subflowRef, &s.WorkflowID); err != nil {
231-
return err
232-
}
233-
234-
return nil
235-
}
236-
237209
// Timeouts ...
238210
type Timeouts struct {
239211
// WorkflowExecTimeout Workflow execution timeout duration (ISO 8601 duration format). If not specified should be 'unlimited'
@@ -474,27 +446,6 @@ type OnEvents struct {
474446
EventDataFilter EventDataFilter `json:"eventDataFilter,omitempty"`
475447
}
476448

477-
// Action ...
478-
type Action struct {
479-
// Unique action definition name
480-
Name string `json:"name,omitempty"`
481-
FunctionRef *FunctionRef `json:"functionRef,omitempty"`
482-
// References a 'trigger' and 'result' reusable event definitions
483-
EventRef *EventRef `json:"eventRef,omitempty"`
484-
// References a sub-workflow to be executed
485-
SubFlowRef *WorkflowRef `json:"subFlowRef,omitempty"`
486-
// Sleep Defines time period workflow execution should sleep before / after function execution
487-
Sleep Sleep `json:"sleep,omitempty"`
488-
// RetryRef References a defined workflow retry definition. If not defined the default retry policy is assumed
489-
RetryRef string `json:"retryRef,omitempty"`
490-
// List of unique references to defined workflow errors for which the action should not be retried. Used only when `autoRetries` is set to `true`
491-
NonRetryableErrors []string `json:"nonRetryableErrors,omitempty" validate:"omitempty,min=1"`
492-
// List of unique references to defined workflow errors for which the action should be retried. Used only when `autoRetries` is set to `false`
493-
RetryableErrors []string `json:"retryableErrors,omitempty" validate:"omitempty,min=1"`
494-
// Action data filter
495-
ActionDataFilter ActionDataFilter `json:"actionDataFilter,omitempty"`
496-
}
497-
498449
// End definition
499450
type End struct {
500451
// If true, completes all execution flows in the given workflow instance
@@ -578,16 +529,6 @@ type BranchTimeouts struct {
578529
BranchExecTimeout string `json:"branchExecTimeout,omitempty" validate:"omitempty,min=1"`
579530
}
580531

581-
// ActionDataFilter ...
582-
type ActionDataFilter struct {
583-
// Workflow expression that selects state data that the state action can use
584-
FromStateData string `json:"fromStateData,omitempty"`
585-
// Workflow expression that filters the actions' data results
586-
Results string `json:"results,omitempty"`
587-
// Workflow expression that selects a state data element to which the action results should be added/merged into. If not specified, denote, the top-level state data element
588-
ToStateData string `json:"toStateData,omitempty"`
589-
}
590-
591532
// DataInputSchema ...
592533
type DataInputSchema struct {
593534
Schema string `json:"schema" validate:"required"`
@@ -656,11 +597,3 @@ func (c *Constants) UnmarshalJSON(data []byte) error {
656597
c.Data = constantData
657598
return nil
658599
}
659-
660-
// Sleep ...
661-
type Sleep struct {
662-
// Before Amount of time (ISO 8601 duration format) to sleep before function/subflow invocation. Does not apply if 'eventRef' is defined.
663-
Before string `json:"before,omitempty"`
664-
// After Amount of time (ISO 8601 duration format) to sleep after function/subflow invocation. Does not apply if 'eventRef' is defined.
665-
After string `json:"after,omitempty"`
666-
}

0 commit comments

Comments
 (0)