Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add function replace_between_positions #406

Merged
merged 6 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
xdlbdy marked this conversation as resolved.
Show resolved Hide resolved
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")
xdlbdy marked this conversation as resolved.
Show resolved Hide resolved
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) {
xdlbdy marked this conversation as resolved.
Show resolved Hide resolved
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