Repository: vanus-labs/vanus (go)

Issue:
[Good First Issues]: implement function "split_with_delimiter" - 4 points
# Motivation
Vanus Functions are a series of pre-built functions worked within Vanus transformer. By choosing a function, users can specify the way that events will be changed.

It will be a good start if you decide to contribute to Vanus codes.

# Details

Function name: ** split_with_delimiter**

Args

- sourceJsonPath
- delimiter - string representing 
- targetJsonPath


## Description

The function is used to split the source JSON path to an array based on the delimiter, and assign the array to a target JSON path. 


## Example

```
{
    "specversion" : "1.0",
    "type" : "com.example.someevent",
    "source" : "/mycontext",
    "subject": null,
    "id" : "C234-1234-1234",
    "time" : "2018-04-05T17:31:00Z",
    "comexampleextension1" : "value",
    "comexampleothervalue" : 5,
    "datacontenttype" : "application/json",
    "data": {
        appinfoA : "this is a string deli This will be a other string"

    }
}
```

After the function.

```
"pipeline":[
    {"command":["split_with_delimiter", "$.data.appinfoA", " deli ", "$.data.stringArray"]}
]
```

The transformed event should look like this:

```
{
    "specversion" : "1.0",
    "type" : "com.example.someevent",
    "source" : "/mycontext",
    "subject": null,
    "id" : "C234-1234-1234",
    "time" : "2018-04-05T17:31:00Z",
    "comexampleextension1" : "value",
    "comexampleothervalue" : 5,
    "datacontenttype" : "application/json",
    "data" : {
           "appinfoA": "This is a string deli This will be a other string deli Here a third string",
           "stringArray": ["This is a string", "This will be a other string", "Here a third string"]

     }
} 

```
# How to implement

1. Fork the vanus repository.
2. Create a branch `function-split_with_delimiter`
3. Create `split_with_delimiter.go` under `internal/primitive/transform/action/strings`, and then implement the function.
4. Create `split_with_delimiter_test.go` under `internal/primitive/transform/action/strings` for unit test.
5. Register your function in `internal/primitive/transform/runtime/init.go`.
6. Submit your PR

# Contribution points

Each issue counts specific points based on its difficulty. Contributors earn the corresponding points if they solve that issue. Contribution points are used to describe the contributions contributors have made.  They can also be used to get rewards when there are community events.

This issue counts **4 points**.

# How to claim to solve the issue

If you want to implement this function, please leave a comment in this issue like:

> I'd like to implement this function, please assign this issue to me.

Vanus community will assign the issue to you on time.

Pull request description:
feat: implement function "split_with_delimiter"
<!--

Thank you for contributing to Vanus!

PR Title Format: 

feat/fix/refactor/docs/...: what's changed

Note: see https://github.com/linkall-labs/vanus/blob/main/CONTRIBUTING.md to know details about title format
-->

### What problem does this PR solve?
<!--

Please create an issue first to describe the problem

There MUST be one line starting with "Issue Number:  ".

-->

Issue Number: close #441 

### Problem Summary

### What is changed and how does it work?

### Check List

<!-- At least one of them must be included. -->

#### Tests

- [x] Unit test
- [ ] Integration test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No code


Patch:
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 0000000..ac05f4d
--- /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/function/strings_functions.go b/internal/primitive/transform/function/strings_functions.go
index e087bf9..9cf4851 100644
--- a/internal/primitive/transform/function/strings_functions.go
+++ b/internal/primitive/transform/function/strings_functions.go
@@ -125,21 +125,18 @@ var CapitalizeWord = function{
 	fixedArgs: []common.Type{common.String},
 	fn: func(args []interface{}) (interface{}, error) {
 		value, _ := args[0].(string)
-		if len(value) == 0 {
-			return value, nil
-		}
-		if len(value) == 1 {
-			return strings.ToUpper(string(value[0])), nil
-		}
-		capWords := make([]rune, 0, len([]rune(value)))
-		prev := ' '
-		for _, v := range value {
-			if v != ' ' && prev == ' ' {
-				v = unicode.ToUpper(v)
+		rs := []rune(value)
+		inWord := false
+		for i, r := range rs {
+			if !unicode.IsSpace(r) {
+				if !inWord {
+					rs[i] = unicode.ToTitle(r)
+				}
+				inWord = true
+			} else {
+				inWord = false
 			}
-			capWords = append(capWords, v)
-			prev = v
 		}
-		return string(capWords), nil
+		return string(rs), nil
 	},
 }
diff --git a/internal/primitive/transform/runtime/init.go b/internal/primitive/transform/runtime/init.go
index ec19d0b..390cd85 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


Test patch:
diff --git a/internal/primitive/transform/action/strings/capitalize_word_test.go b/internal/primitive/transform/action/strings/capitalize_word_test.go
index 6c4ae36..8108e4d 100644
--- a/internal/primitive/transform/action/strings/capitalize_word_test.go
+++ b/internal/primitive/transform/action/strings/capitalize_word_test.go
@@ -86,4 +86,16 @@ func TestCapitalizeWordAction(t *testing.T) {
 		So(err, ShouldBeNil)
 		So(e.Extensions()["test"], ShouldEqual, "A")
 	})
+	Convey("test capitalize word: leading symbols", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.test"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		e.SetExtension("test", "let 'em go")
+		ceCtx := &context.EventContext{
+			Event: &e,
+		}
+		err = a.Execute(ceCtx)
+		So(err, ShouldBeNil)
+		So(e.Extensions()["test"], ShouldEqual, "Let 'em Go")
+	})
 }
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 0000000..19af4f2
--- /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 b54f5f4..0ba903b 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"}))
 }