Skip to content

Commit

Permalink
feat: implement function "split_with_delimiter" (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhanusaputra authored Feb 25, 2023
1 parent eee9250 commit 01abb91
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
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
}
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"})
})
}
1 change: 1 addition & 0 deletions internal/primitive/transform/runtime/action_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}))
}
1 change: 1 addition & 0 deletions internal/primitive/transform/runtime/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func init() {
strings.NewCapitalizeSentenceAction,
strings.NewCheckCustomValuesAction,
strings.NewCapitalizeWordAction,
strings.NewSplitWithDelimiterAction,
// condition
condition.NewConditionIfAction,
// array
Expand Down

0 comments on commit 01abb91

Please sign in to comment.