Repository: vanus-labs/vanus (go)

Issue:
[Good First Issues]: implement function "check_missing" - 7 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: **extract_missing**

Args

- sourceJsonPath -  The name of an existing column.
- targetJsonPath – The name of the new column to be created.
- trueFlagReplacement - String is null custom string
- falseFlagReplacement - String is not null custom string

## Description
The function is used to check if the source JSON path is null or not. If its null, the trueFlagReplacement will be assigned to the target JSON path. If it's not null, the falseFlagReplacement will be assigned to the 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" : "",
       

    }
}
```

After the function "extract_missing":

```
"pipeline":[
    {"command":["extract_missing","$.data.appinfoA", "$.data.appinfoC", "The element is not null.", "The element is null"]}
]
```

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" : "",
        "appinfoC" : "The element is null"

```

# How to implement

1. Fork the vanus repository.
2. Create a branch `function-extract_missing`
3. Create `extract_missing.go` under `internal/primitive/transform/action/strings`, and then implement the function.
4. Create `extract_missing.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 **7 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:
feat:Function extract missing
<!--

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 #427

### Problem Summary
The function is used to check if the source JSON path is null or not. If its null, the trueFlagReplacement will be assigned to the target JSON path. If it's not null, the falseFlagReplacement will be assigned to the target JSON path.

### 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/extract_missing.go b/internal/primitive/transform/action/strings/extract_missing.go
new file mode 100644
index 0000000..0e8e088
--- /dev/null
+++ b/internal/primitive/transform/action/strings/extract_missing.go
@@ -0,0 +1,65 @@
+// 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/context"
+)
+
+type extractMissingAction struct {
+	action.CommonAction
+}
+
+// NewExtractMissingAction ["extract_missing", "sourceJSONPath", "targetJSONPath",
+// "trueFlagReplacement", "falseFlagReplacement"].
+func NewExtractMissingAction() action.Action {
+	return &extractMissingAction{
+		CommonAction: action.CommonAction{
+			ActionName: "EXTRACT_MISSING_ACTION",
+			FixedArgs: []arg.TypeList{
+				arg.EventList,
+				arg.EventList,
+				[]arg.Type{arg.Constant},
+				[]arg.Type{arg.Constant},
+			},
+		},
+	}
+}
+
+func (a *extractMissingAction) Init(args []arg.Arg) error {
+	a.TargetArg = args[1]
+	a.Args = []arg.Arg{args[0]}
+	a.Args = append(a.Args, args[2:]...)
+	a.ArgTypes = []common.Type{common.String, common.Any, common.Any}
+	return nil
+}
+
+func (a *extractMissingAction) Execute(ceCtx *context.EventContext) error {
+	args, err := a.RunArgs(ceCtx)
+	if err != nil {
+		return err
+	}
+	sourceJSONPath, _ := args[0].(string)
+	trueFlagReplacement := args[1]
+	falseFlagReplacement := args[2]
+
+	if sourceJSONPath == "" {
+		return a.TargetArg.SetValue(ceCtx, trueFlagReplacement)
+	}
+	return a.TargetArg.SetValue(ceCtx, falseFlagReplacement)
+}
diff --git a/internal/primitive/transform/action/strings/split_from_start.go b/internal/primitive/transform/action/strings/split_from_start.go
new file mode 100644
index 0000000..99c3458
--- /dev/null
+++ b/internal/primitive/transform/action/strings/split_from_start.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 splitFromStartAction struct {
+	action.FunctionAction
+}
+
+// NewSplitFromStartAction ["split_from_start", "sourceJsonPath", "position", "targetJsonPath"].
+func NewSplitFromStartAction() action.Action {
+	a := &splitFromStartAction{}
+	a.CommonAction = action.CommonAction{
+		ActionName: "SPLIT_FROM_START",
+		FixedArgs:  []arg.TypeList{arg.EventList, []arg.Type{arg.Constant}, []arg.Type{arg.EventData}},
+		Fn:         function.SplitFromStart,
+	}
+	return a
+}
+
+func (a *splitFromStartAction) Init(args []arg.Arg) error {
+	a.TargetArg = args[2]
+	a.Args = args[:2]
+	a.ArgTypes = []common.Type{common.String, common.Int}
+	return nil
+}
diff --git a/internal/primitive/transform/function/strings_functions.go b/internal/primitive/transform/function/strings_functions.go
index 2397686..4d3037a 100644
--- a/internal/primitive/transform/function/strings_functions.go
+++ b/internal/primitive/transform/function/strings_functions.go
@@ -165,3 +165,23 @@ var CapitalizeWord = function{
 		return string(rs), nil
 	},
 }
+
+var SplitFromStart = function{
+	name:      "SPLIT_FROM_START",
+	fixedArgs: []common.Type{common.String, common.Int},
+	fn: func(args []interface{}) (interface{}, error) {
+		value, _ := args[0].(string)
+		splitPosition, _ := args[1].(int)
+
+		if splitPosition <= 0 {
+			return nil, fmt.Errorf("split position must be more than zero")
+		}
+
+		if splitPosition >= len(value) {
+			return []string{value, ""}, nil
+		}
+
+		result := []string{value[:splitPosition], value[splitPosition:]}
+		return result, nil
+	},
+}
diff --git a/internal/primitive/transform/runtime/init.go b/internal/primitive/transform/runtime/init.go
index 64bc0c7..8443cb7 100644
--- a/internal/primitive/transform/runtime/init.go
+++ b/internal/primitive/transform/runtime/init.go
@@ -53,10 +53,12 @@ func init() {
 		strings.NewReplaceBetweenPositionsAction,
 		strings.NewCapitalizeSentenceAction,
 		strings.NewCheckCustomValuesAction,
+		strings.NewExtractMissingAction,
 		strings.NewReplaceBetweenDelimitersAction,
 		strings.NewCapitalizeWordAction,
 		strings.NewSplitWithDelimiterAction,
 		strings.NewSplitBetweenPositionsAction,
+		strings.NewSplitFromStartAction,
 		// condition
 		condition.NewConditionIfAction,
 		// array


Test patch:
diff --git a/internal/primitive/transform/action/strings/extract_missing_test.go b/internal/primitive/transform/action/strings/extract_missing_test.go
new file mode 100644
index 0000000..b9f70ab
--- /dev/null
+++ b/internal/primitive/transform/action/strings/extract_missing_test.go
@@ -0,0 +1,87 @@
+// 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 (
+	"encoding/json"
+	"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 TestExtractMissingAction(t *testing.T) {
+	funcName := strings.NewExtractMissingAction().Name()
+	jsonStr := `{
+                "source": "Vanus"
+}`
+	jsonStrTwo := `{
+				"source": ""
+}`
+	Convey("test source field using boolean", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.data.source", "$.data.target", "true", "false"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		var data map[string]interface{}
+		err = json.Unmarshal([]byte(jsonStr), &data)
+		So(err, ShouldBeNil)
+		ceCtx := &context.EventContext{
+			Event: &e,
+			Data:  data,
+		}
+		err = a.Execute(ceCtx)
+		So(err, ShouldBeNil)
+		res, ok := data["target"]
+		So(ok, ShouldBeTrue)
+		So(res, ShouldEqual, "false")
+	})
+	Convey("test source field using int", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.data.source", "$.data.target", 1, 0})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		var data map[string]interface{}
+		err = json.Unmarshal([]byte(jsonStr), &data)
+		So(err, ShouldBeNil)
+		ceCtx := &context.EventContext{
+			Event: &e,
+			Data:  data,
+		}
+		err = a.Execute(ceCtx)
+		So(err, ShouldBeNil)
+		res, ok := data["target"]
+		So(ok, ShouldBeTrue)
+		So(res, ShouldEqual, 0)
+	})
+	Convey("test empty source field with boolean", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.data.source", "$.data.target", "true", "false"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		var data map[string]interface{}
+		err = json.Unmarshal([]byte(jsonStrTwo), &data)
+		So(err, ShouldBeNil)
+		ceCtx := &context.EventContext{
+			Event: &e,
+			Data:  data,
+		}
+		err = a.Execute(ceCtx)
+		So(err, ShouldBeNil)
+		res, ok := data["target"]
+		So(ok, ShouldBeTrue)
+		So(res, ShouldEqual, "true")
+	})
+}
diff --git a/internal/primitive/transform/action/strings/split_from_start_test.go b/internal/primitive/transform/action/strings/split_from_start_test.go
new file mode 100644
index 0000000..2047925
--- /dev/null
+++ b/internal/primitive/transform/action/strings/split_from_start_test.go
@@ -0,0 +1,112 @@
+// 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 TestSplitFromStartAction(t *testing.T) {
+	funcName := strings.NewSplitFromStartAction().Name()
+
+	Convey("test split from start: Positive testcase, common", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.test", 5, "$.data.target"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		data := map[string]interface{}{}
+		e.SetExtension("test", "Hello, World!")
+		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{"Hello", ", World!"})
+	})
+
+	Convey("test split from start: Positive testcase, length 1", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.test", 1, "$.data.target"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		data := map[string]interface{}{}
+		e.SetExtension("test", "H")
+		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{"H", ""})
+	})
+
+	Convey("test split from start: Positive testcase, length 0", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.test", 1, "$.data.target"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		data := map[string]interface{}{}
+		e.SetExtension("test", "")
+		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{"", ""})
+	})
+
+	Convey("test split from start: Positive testcase, split position above value length", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.test", 10, "$.data.target"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		data := map[string]interface{}{}
+		e.SetExtension("test", "hello")
+		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{"hello", ""})
+	})
+
+	Convey("test split from start: Negative testcase, split position less than one", t, func() {
+		a, err := runtime.NewAction([]interface{}{funcName, "$.test", 0, "$.data.target"})
+		So(err, ShouldBeNil)
+		e := cetest.MinEvent()
+		data := map[string]interface{}{}
+		e.SetExtension("test", "split position must be more than zero")
+		ceCtx := &context.EventContext{
+			Event: &e,
+			Data:  data,
+		}
+		err = a.Execute(ceCtx)
+		So(err, ShouldNotBeNil)
+		So(e.Extensions()["test"], ShouldEqual, "split position must be more than zero")
+	})
+}
diff --git a/internal/primitive/transform/runtime/action_bench_test.go b/internal/primitive/transform/runtime/action_bench_test.go
index 143f6c1..4665682 100644
--- a/internal/primitive/transform/runtime/action_bench_test.go
+++ b/internal/primitive/transform/runtime/action_bench_test.go
@@ -81,5 +81,6 @@ func BenchmarkAction(b *testing.B) {
 	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"}))
 	b.Run("unfold_array", actionBenchmark([]interface{}{"unfold_array", "$.data.str", "$.data.target"}))
+	b.Run("extract_missing", actionBenchmark([]interface{}{"extract_missing", "$.data.source", "$.data.target", "true", "false"}))
 	b.Run("replace_between_delimiter", actionBenchmark([]interface{}{"replace_between_delimiter", "$.test", "&&", "&&", "Vanus"}))
 }