Skip to content

feat(*): add v0.8 event data filter's useData field #79

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 1 commit into from
Sep 27, 2022
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
54 changes: 54 additions & 0 deletions model/event_data_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 (
"bytes"
"encoding/json"
"fmt"
)

// 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 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.
ToStateData string `json:"toStateData,omitempty"`
}

type eventDataFilterForUnmarshal EventDataFilter

func (f *EventDataFilter) UnmarshalJSON(data []byte) error {
data = bytes.TrimSpace(data)
if len(data) == 0 {
return fmt.Errorf("no bytes to unmarshal")
}

v := eventDataFilterForUnmarshal{
UseData: true,
}
err := json.Unmarshal(data, &v)
if err != nil {
// TODO: replace the error message with correct type's name
return err
}

*f = EventDataFilter(v)
return nil
}
81 changes: 81 additions & 0 deletions model/event_data_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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 (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestEventDataFilterUnmarshalJSON(t *testing.T) {
type testCase struct {
desp string
data string
expect EventDataFilter
err string
}
testCases := []testCase{
{
desp: "normal test",
data: `{"data": "1", "toStateData": "2"}`,
expect: EventDataFilter{
UseData: true,
Data: "1",
ToStateData: "2",
},
err: ``,
},
{
desp: "add UseData to false",
data: `{"UseData": false, "data": "1", "toStateData": "2"}`,
expect: EventDataFilter{
UseData: false,
Data: "1",
ToStateData: "2",
},
err: ``,
},
{
desp: "empty data",
data: ` `,
expect: EventDataFilter{},
err: `unexpected end of JSON input`,
},
{
desp: "invalid json format",
data: `{"data": 1, "toStateData": "2"}`,
expect: EventDataFilter{},
err: `json: cannot unmarshal number into Go struct field eventDataFilterForUnmarshal.data of type string`,
},
}

for _, tc := range testCases {
t.Run(tc.desp, func(t *testing.T) {
var v EventDataFilter
err := json.Unmarshal([]byte(tc.data), &v)

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

assert.NoError(t, err)
assert.Equal(t, tc.expect, v)
})
}
}
8 changes: 0 additions & 8 deletions model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,14 +560,6 @@ type StateDataFilter struct {
Output string `json:"output,omitempty"`
}

// EventDataFilter ...
type EventDataFilter struct {
// 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.
ToStateData string `json:"toStateData,omitempty"`
}

// Branch Definition
type Branch struct {
// Branch name
Expand Down
3 changes: 2 additions & 1 deletion parser/testdata/workflows/eventbasedgreeting.sw.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"GreetingEvent"
],
"eventDataFilter": {
"data": "${ .data | .greet }"
"data": "${ .data | .greet }",
"useData": false
},
"actions": [
{
Expand Down