Skip to content

Commit

Permalink
feat: add function replace_between_positions (#406)
Browse files Browse the repository at this point in the history
* 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
c0d33ngr authored Jan 11, 2023
1 parent 7a4fb73 commit 29f5430
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
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
}
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")
})
}
2 changes: 2 additions & 0 deletions internal/primitive/transform/common/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func Cast(val interface{}, target Type) (interface{}, error) {
return float64(value), nil
case int64:
return float64(value), nil
case int:
return float64(value), nil
}
return 0, fmt.Errorf("undefined cast from %v to %v", TypeFromVal(val), target)
case Bool:
Expand Down
22 changes: 22 additions & 0 deletions internal/primitive/transform/function/strings_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package function

import (
"fmt"
"strings"

"github.com/linkall-labs/vanus/internal/primitive/transform/common"
Expand Down Expand Up @@ -81,3 +82,24 @@ var SplitWithSepFunction = function{
return strings.SplitN(s, sep, int(args[2].(float64))), nil
},
}

var ReplaceBetweenPositionsFunction = function{
name: "REPLACE_BETWEEN_POSITIONS",
fixedArgs: []common.Type{common.String, common.Number, common.Number, common.String},
fn: func(args []interface{}) (interface{}, error) {
path, _ := args[0].(string)
startPosition := int(args[1].(float64))
endPosition := int(args[2].(float64))
targetValue, _ := args[3].(string)
if startPosition >= len(path) {
return nil, fmt.Errorf("start position must be less than the length of the string")
}
if endPosition >= len(path) {
return nil, fmt.Errorf("end position must be less than the length of the string")
}
if startPosition >= endPosition {
return nil, fmt.Errorf("start position must be less than end position")
}
return path[:startPosition] + targetValue + path[endPosition:], nil
},
}
1 change: 1 addition & 0 deletions internal/primitive/transform/runtime/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func init() {
strings.NewAddSuffixAction,
strings.NewReplaceWithRegexAction,
strings.NewReplaceStringAction,
strings.NewReplaceBetweenPositionsAction,
// condition
condition.NewConditionIfAction,
// render
Expand Down

0 comments on commit 29f5430

Please sign in to comment.