Skip to content

Commit 828391a

Browse files
authored
feat(*): add v0.8 event data filter's useData field (#79)
Signed-off-by: lsytj0413 <511121939@qq.com> Signed-off-by: lsytj0413 <511121939@qq.com>
1 parent 1293519 commit 828391a

File tree

4 files changed

+137
-9
lines changed

4 files changed

+137
-9
lines changed

model/event_data_filter.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
// EventDataFilter used to filter consumed event payloads.
24+
type EventDataFilter struct {
25+
// UseData represent where event payload is added/merged to state data. If it's false, data & toStateData should be ignored.
26+
// Defaults to true.
27+
UseData bool `json:"useData,omitempty"`
28+
29+
// Workflow expression that filters of the event data (payload)
30+
Data string `json:"data,omitempty"`
31+
// 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.
32+
ToStateData string `json:"toStateData,omitempty"`
33+
}
34+
35+
type eventDataFilterForUnmarshal EventDataFilter
36+
37+
func (f *EventDataFilter) UnmarshalJSON(data []byte) error {
38+
data = bytes.TrimSpace(data)
39+
if len(data) == 0 {
40+
return fmt.Errorf("no bytes to unmarshal")
41+
}
42+
43+
v := eventDataFilterForUnmarshal{
44+
UseData: true,
45+
}
46+
err := json.Unmarshal(data, &v)
47+
if err != nil {
48+
// TODO: replace the error message with correct type's name
49+
return err
50+
}
51+
52+
*f = EventDataFilter(v)
53+
return nil
54+
}

model/event_data_filter_test.go

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

model/workflow.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -560,14 +560,6 @@ type StateDataFilter struct {
560560
Output string `json:"output,omitempty"`
561561
}
562562

563-
// EventDataFilter ...
564-
type EventDataFilter struct {
565-
// Workflow expression that filters of the event data (payload)
566-
Data string `json:"data,omitempty"`
567-
// 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.
568-
ToStateData string `json:"toStateData,omitempty"`
569-
}
570-
571563
// Branch Definition
572564
type Branch struct {
573565
// Branch name

parser/testdata/workflows/eventbasedgreeting.sw.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"GreetingEvent"
3131
],
3232
"eventDataFilter": {
33-
"data": "${ .data | .greet }"
33+
"data": "${ .data | .greet }",
34+
"useData": false
3435
},
3536
"actions": [
3637
{

0 commit comments

Comments
 (0)