-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement function "split_with_delimiter" (#458)
- Loading branch information
1 parent
6ce2c8b
commit 4b0135b
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
internal/primitive/transform/action/strings/split_with_delimiter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2023 Linkall Inc. | ||
// | ||
// 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 strings | ||
|
||
import ( | ||
"github.com/linkall-labs/vanus/internal/primitive/transform/action" | ||
"github.com/linkall-labs/vanus/internal/primitive/transform/arg" | ||
"github.com/linkall-labs/vanus/internal/primitive/transform/common" | ||
"github.com/linkall-labs/vanus/internal/primitive/transform/function" | ||
) | ||
|
||
type splitWithDelimiterAction struct { | ||
action.FunctionAction | ||
} | ||
|
||
// NewSplitWithDelimiterAction ["split_with_delimiter","sourceJsonPath", "delimiter", "targetJsonPath"]. | ||
func NewSplitWithDelimiterAction() action.Action { | ||
a := &splitWithDelimiterAction{} | ||
a.CommonAction = action.CommonAction{ | ||
ActionName: "SPLIT_WITH_DELIMITER", | ||
FixedArgs: []arg.TypeList{arg.EventList, []arg.Type{arg.Constant}, []arg.Type{arg.EventData}}, | ||
Fn: function.SplitWithSepFunction, | ||
} | ||
return a | ||
} | ||
|
||
func (a *splitWithDelimiterAction) Init(args []arg.Arg) error { | ||
a.TargetArg = args[2] | ||
a.Args = args[:2] | ||
a.ArgTypes = []common.Type{common.String, common.String} | ||
return nil | ||
} |
45 changes: 45 additions & 0 deletions
45
internal/primitive/transform/action/strings/split_with_delimiter_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2023 Linkall Inc. | ||
// | ||
// 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 strings_test | ||
|
||
import ( | ||
"testing" | ||
|
||
cetest "github.com/cloudevents/sdk-go/v2/test" | ||
"github.com/linkall-labs/vanus/internal/primitive/transform/action/strings" | ||
"github.com/linkall-labs/vanus/internal/primitive/transform/context" | ||
"github.com/linkall-labs/vanus/internal/primitive/transform/runtime" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestSplitWithDelimiterAction(t *testing.T) { | ||
funcName := strings.NewSplitWithDelimiterAction().Name() | ||
Convey("test split with delimiter", t, func() { | ||
a, err := runtime.NewAction([]interface{}{funcName, "$.test", ",", "$.data.target"}) | ||
So(err, ShouldBeNil) | ||
e := cetest.MinEvent() | ||
data := map[string]interface{}{} | ||
e.SetExtension("test", "one,two,three") | ||
ceCtx := &context.EventContext{ | ||
Event: &e, | ||
Data: data, | ||
} | ||
err = a.Execute(ceCtx) | ||
So(err, ShouldBeNil) | ||
res, ok := data["target"] | ||
So(ok, ShouldBeTrue) | ||
So(res, ShouldResemble, []string{"one", "two", "three"}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters