-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add function replace_between_positions (#406)
* implement the function replace_between_positions * Update code to be more robust with checks and test cases * fix errors in code * fix tests in replace-between-positions * Fix bug * fix: fix code error
- Loading branch information
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
internal/primitive/transform/action/strings/replace_between_positions.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,32 @@ | ||
// 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/function" | ||
) | ||
|
||
// NewReplaceBetweenPositionsAction ["path","startPosition","endPosition","targetValue"]. | ||
func NewReplaceBetweenPositionsAction() action.Action { | ||
a := &action.SourceTargetSameAction{} | ||
a.CommonAction = action.CommonAction{ | ||
ActionName: "REPLACE_BETWEEN_POSITIONS", | ||
FixedArgs: []arg.TypeList{arg.EventList, arg.All, arg.All, arg.All}, | ||
Fn: function.ReplaceBetweenPositionsFunction, | ||
} | ||
return a | ||
} |
81 changes: 81 additions & 0 deletions
81
internal/primitive/transform/action/strings/replace_between_positions_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,81 @@ | ||
// 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 TestReplaceBetweenPositionsAction(t *testing.T) { | ||
funcName := strings.NewReplaceBetweenPositionsAction().Name() | ||
|
||
Convey("test Positive testcase", t, func() { | ||
a, err := runtime.NewAction([]interface{}{funcName, "$.test", 7, 12, "Vanus"}) | ||
So(err, ShouldBeNil) | ||
e := cetest.MinEvent() | ||
e.SetExtension("test", "Hello, World!") | ||
ceCtx := &context.EventContext{ | ||
Event: &e, | ||
} | ||
err = a.Execute(ceCtx) | ||
So(err, ShouldBeNil) | ||
So(e.Extensions()["test"], ShouldEqual, "Hello, Vanus!") | ||
}) | ||
|
||
Convey("test Negative testcase: startPosition greater than string length", t, func() { | ||
a, err := runtime.NewAction([]interface{}{funcName, "$.test", 100, 8, "Dan"}) | ||
So(err, ShouldBeNil) | ||
e := cetest.MinEvent() | ||
e.SetExtension("test", "Start position must be less than the length of the string") | ||
ceCtx := &context.EventContext{ | ||
Event: &e, | ||
} | ||
err = a.Execute(ceCtx) | ||
So(err, ShouldNotBeNil) | ||
So(e.Extensions()["test"], ShouldEqual, "Start position must be less than the length of the string") | ||
}) | ||
|
||
Convey("test Negative testcase: endPosition greater than string length", t, func() { | ||
a, err := runtime.NewAction([]interface{}{funcName, "$.test", 8, 60, "free to use"}) | ||
So(err, ShouldBeNil) | ||
e := cetest.MinEvent() | ||
e.SetExtension("test", "End position must be less than the length of the string") | ||
ceCtx := &context.EventContext{ | ||
Event: &e, | ||
} | ||
err = a.Execute(ceCtx) | ||
So(err, ShouldNotBeNil) | ||
So(e.Extensions()["test"], ShouldEqual, "End position must be less than the length of the string") | ||
}) | ||
|
||
Convey("test Negative testcase: startPosition greater than endPosition", t, func() { | ||
a, err := runtime.NewAction([]interface{}{funcName, "$.test", 12, 5, "Python"}) | ||
So(err, ShouldBeNil) | ||
e := cetest.MinEvent() | ||
e.SetExtension("test", "Start position must be less than end position") | ||
ceCtx := &context.EventContext{ | ||
Event: &e, | ||
} | ||
err = a.Execute(ceCtx) | ||
So(err, ShouldNotBeNil) | ||
So(e.Extensions()["test"], ShouldEqual, "Start position must be less than end position") | ||
}) | ||
} |
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
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