diff --git a/model/event_data_filter.go b/model/event_data_filter.go new file mode 100644 index 0000000..5193a4d --- /dev/null +++ b/model/event_data_filter.go @@ -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 +} diff --git a/model/event_data_filter_test.go b/model/event_data_filter_test.go new file mode 100644 index 0000000..1d267ec --- /dev/null +++ b/model/event_data_filter_test.go @@ -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) + }) + } +} diff --git a/model/workflow.go b/model/workflow.go index 0a97f86..f6cf70f 100644 --- a/model/workflow.go +++ b/model/workflow.go @@ -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 diff --git a/parser/testdata/workflows/eventbasedgreeting.sw.json b/parser/testdata/workflows/eventbasedgreeting.sw.json index e0d66a6..a5cff3e 100644 --- a/parser/testdata/workflows/eventbasedgreeting.sw.json +++ b/parser/testdata/workflows/eventbasedgreeting.sw.json @@ -30,7 +30,8 @@ "GreetingEvent" ], "eventDataFilter": { - "data": "${ .data | .greet }" + "data": "${ .data | .greet }", + "useData": false }, "actions": [ {