From 01abb91f38e0e3d303bd1a19b533a17d000cd578 Mon Sep 17 00:00:00 2001 From: Dhanu Saputra <35093673+dhanusaputra@users.noreply.github.com> Date: Sat, 25 Feb 2023 17:26:53 +0700 Subject: [PATCH] feat: implement function "split_with_delimiter" (#458) --- .../action/strings/split_with_delimiter.go | 44 ++++++++++++++++++ .../strings/split_with_delimiter_test.go | 45 +++++++++++++++++++ .../transform/runtime/action_bench_test.go | 1 + internal/primitive/transform/runtime/init.go | 1 + 4 files changed, 91 insertions(+) create mode 100644 internal/primitive/transform/action/strings/split_with_delimiter.go create mode 100644 internal/primitive/transform/action/strings/split_with_delimiter_test.go diff --git a/internal/primitive/transform/action/strings/split_with_delimiter.go b/internal/primitive/transform/action/strings/split_with_delimiter.go new file mode 100644 index 000000000..ac05f4d45 --- /dev/null +++ b/internal/primitive/transform/action/strings/split_with_delimiter.go @@ -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 +} diff --git a/internal/primitive/transform/action/strings/split_with_delimiter_test.go b/internal/primitive/transform/action/strings/split_with_delimiter_test.go new file mode 100644 index 000000000..19af4f2ac --- /dev/null +++ b/internal/primitive/transform/action/strings/split_with_delimiter_test.go @@ -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"}) + }) +} diff --git a/internal/primitive/transform/runtime/action_bench_test.go b/internal/primitive/transform/runtime/action_bench_test.go index b54f5f4b0..0ba903b88 100644 --- a/internal/primitive/transform/runtime/action_bench_test.go +++ b/internal/primitive/transform/runtime/action_bench_test.go @@ -79,4 +79,5 @@ func BenchmarkAction(b *testing.B) { b.Run("replace_with_regex", actionBenchmark([]interface{}{"replace_with_regex", "$.data.str", "a", "Aa"})) b.Run("capitalize_sentence", actionBenchmark([]interface{}{"capitalize_sentence", "$.data.str"})) b.Run("check_custom_values", actionBenchmark([]interface{}{"check_custom_values", "$.data.str", "value", "$.data.target", "true", "false"})) + b.Run("split_with_delimiter", actionBenchmark([]interface{}{"split_with_delimiter", "$.data.str", "a", "$.data.target"})) } diff --git a/internal/primitive/transform/runtime/init.go b/internal/primitive/transform/runtime/init.go index ec19d0b05..390cd8511 100644 --- a/internal/primitive/transform/runtime/init.go +++ b/internal/primitive/transform/runtime/init.go @@ -54,6 +54,7 @@ func init() { strings.NewCapitalizeSentenceAction, strings.NewCheckCustomValuesAction, strings.NewCapitalizeWordAction, + strings.NewSplitWithDelimiterAction, // condition condition.NewConditionIfAction, // array