-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
internal/primitive/transform/action/strings/split_from_start.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 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 | ||
} |
112 changes: 112 additions & 0 deletions
112
internal/primitive/transform/action/strings/split_from_start_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,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") | ||
}) | ||
} |
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