From 4fe948ed6478fab3bf791a65c57f188b80ac351e Mon Sep 17 00:00:00 2001 From: Jeffrey Whewhetu Date: Tue, 10 Jan 2023 03:08:25 +0100 Subject: [PATCH 1/6] implement the function replace_between_positions --- .../strings/replace_between_positions.go | 32 ++++++++++++++ .../strings/replace_between_positions_test.go | 44 +++++++++++++++++++ .../transform/function/strings_functions.go | 12 +++++ internal/primitive/transform/runtime/init.go | 1 + 4 files changed, 89 insertions(+) create mode 100644 internal/primitive/transform/action/strings/replace_between_positions.go create mode 100644 internal/primitive/transform/action/strings/replace_between_positions_test.go diff --git a/internal/primitive/transform/action/strings/replace_between_positions.go b/internal/primitive/transform/action/strings/replace_between_positions.go new file mode 100644 index 000000000..3f589b37d --- /dev/null +++ b/internal/primitive/transform/action/strings/replace_between_positions.go @@ -0,0 +1,32 @@ +// Copyright 2022 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}, + Fn: function.ReplaceBetweenPositionsFunction, + } + return a +} diff --git a/internal/primitive/transform/action/strings/replace_between_positions_test.go b/internal/primitive/transform/action/strings/replace_between_positions_test.go new file mode 100644 index 000000000..1672057d3 --- /dev/null +++ b/internal/primitive/transform/action/strings/replace_between_positions_test.go @@ -0,0 +1,44 @@ +// Copyright 2022 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) { + Convey("test ReplaceBetweenPositionsAction", t, func() { + a := NewReplaceBetweenPositionsAction() + So(a, ShouldNotBeNil) + So(a.Name(), ShouldEqual, "REPLACE_BETWEEN_POSITIONS") + So(a.FixedArgs(), ShouldResemble, []arg.TypeList{arg.EventList}) + + e := cetest.MinEvent() + e.SetExtension("test", "Hello, World!") + ceCtx := &context.EventContext{ + Event: &e, + } + args := []interface{}{"$.test", 7, 11, "Vanus"} + err := a.Execute(ceCtx, args) + So(err, ShouldBeNil) + So(e.Extensions()["test"], ShouldEqual, "Hello, Vanus!") + }) +} diff --git a/internal/primitive/transform/function/strings_functions.go b/internal/primitive/transform/function/strings_functions.go index 590430980..6ddb17fde 100644 --- a/internal/primitive/transform/function/strings_functions.go +++ b/internal/primitive/transform/function/strings_functions.go @@ -81,3 +81,15 @@ 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.String}, + fn: func(args []interface{}) (interface{}, error) { + path, _ := args[0].(string) + startPosition := args[1].(int) + endPosition := args[2].(int) + targetValue := args[3].(string) + return path[:startPosition] + targetValue + path[endPosition:], nil + } +} diff --git a/internal/primitive/transform/runtime/init.go b/internal/primitive/transform/runtime/init.go index 48987e5b8..27bf52286 100644 --- a/internal/primitive/transform/runtime/init.go +++ b/internal/primitive/transform/runtime/init.go @@ -50,6 +50,7 @@ func init() { strings.NewAddSuffixAction, strings.NewReplaceWithRegexAction, strings.NewReplaceStringAction, + strings.NewReplaceBetweenPositionsAction, // condition condition.NewConditionIfAction, // render From 885c11a19b3f3f2a3f69de841599a3528a6ebda1 Mon Sep 17 00:00:00 2001 From: Jeffrey Whewhetu Date: Tue, 10 Jan 2023 12:48:31 +0100 Subject: [PATCH 2/6] Update code to be more robust with checks and test cases --- .../strings/replace_between_positions.go | 4 ++-- .../strings/replace_between_positions_test.go | 24 +++++++++++++++++-- .../transform/function/strings_functions.go | 17 +++++++++---- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/internal/primitive/transform/action/strings/replace_between_positions.go b/internal/primitive/transform/action/strings/replace_between_positions.go index 3f589b37d..892227c39 100644 --- a/internal/primitive/transform/action/strings/replace_between_positions.go +++ b/internal/primitive/transform/action/strings/replace_between_positions.go @@ -1,4 +1,4 @@ -// Copyright 2022 Linkall Inc. +// 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. @@ -25,7 +25,7 @@ func NewReplaceBetweenPositionsAction() action.Action { a := &action.SourceTargetSameAction{} a.CommonAction = action.CommonAction{ ActionName: "REPLACE_BETWEEN_POSITIONS", - FixedArgs: []arg.TypeList{arg.EventList}, + FixedArgs: []arg.TypeList{arg.EventList, arg.All, arg.All, arg.All}, Fn: function.ReplaceBetweenPositionsFunction, } return a diff --git a/internal/primitive/transform/action/strings/replace_between_positions_test.go b/internal/primitive/transform/action/strings/replace_between_positions_test.go index 1672057d3..680bd4ce6 100644 --- a/internal/primitive/transform/action/strings/replace_between_positions_test.go +++ b/internal/primitive/transform/action/strings/replace_between_positions_test.go @@ -1,4 +1,4 @@ -// Copyright 2022 Linkall Inc. +// 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. @@ -36,9 +36,29 @@ func TestReplaceBetweenPositionsAction(t *testing.T) { ceCtx := &context.EventContext{ Event: &e, } + + // Positive test case args := []interface{}{"$.test", 7, 11, "Vanus"} err := a.Execute(ceCtx, args) So(err, ShouldBeNil) So(e.Extensions()["test"], ShouldEqual, "Hello, Vanus!") - }) + + // Negative test case: startPosition greater than length of string + args = []interface{}{"$.test", 100, 110, "Vanus"} + err = a.Execute(ceCtx, args) + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, "Start position must be less than the length of the string") + + // Negative test case: endPosition greater than length of string + args = []interface{}{"$.test", 7, 110, "Vanus"} + err = a.Execute(ceCtx, args) + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, "End position must be less than the length of the string") + + // Negative test case: startPosition greater than endPosition + args = []interface{}{"$.test", 11, 7, "Vanus"} + err = a.Execute(ceCtx, args) + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, "Start position must be less than end position") + }) } diff --git a/internal/primitive/transform/function/strings_functions.go b/internal/primitive/transform/function/strings_functions.go index 6ddb17fde..f0b2524bf 100644 --- a/internal/primitive/transform/function/strings_functions.go +++ b/internal/primitive/transform/function/strings_functions.go @@ -15,6 +15,7 @@ package function import ( + "fmt" "strings" "github.com/linkall-labs/vanus/internal/primitive/transform/common" @@ -84,12 +85,20 @@ var SplitWithSepFunction = function{ var ReplaceBetweenPositionsFunction = function{ name: "REPLACE_BETWEEN_POSITIONS", - fixedArgs: []common.Type{common.String, common.String}, + fixedArgs: []common.Type{common.String, common.Number, common.Number, common.String}, fn: func(args []interface{}) (interface{}, error) { path, _ := args[0].(string) - startPosition := args[1].(int) - endPosition := args[2].(int) + startPosition := args[1].(float64) + endPosition := args[2].(float64) targetValue := args[3].(string) - return path[:startPosition] + targetValue + path[endPosition:], nil + if startPosition >= len(path) { + return nil, fmt.Errorf("Start 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[:int(startPosition)] + targetValue + path[int(endPosition):], nil } } From dc1ab5d42bc20c9dc684852ed6959d52c649ab92 Mon Sep 17 00:00:00 2001 From: Jeffrey Whewhetu Date: Wed, 11 Jan 2023 02:54:37 +0100 Subject: [PATCH 3/6] fix errors in code --- .../strings/replace_between_positions_test.go | 67 ++++++++++++------- .../transform/function/strings_functions.go | 6 +- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/internal/primitive/transform/action/strings/replace_between_positions_test.go b/internal/primitive/transform/action/strings/replace_between_positions_test.go index 680bd4ce6..5ea0ec03b 100644 --- a/internal/primitive/transform/action/strings/replace_between_positions_test.go +++ b/internal/primitive/transform/action/strings/replace_between_positions_test.go @@ -25,40 +25,57 @@ import ( ) func TestReplaceBetweenPositionsAction(t *testing.T) { - Convey("test ReplaceBetweenPositionsAction", t, func() { - a := NewReplaceBetweenPositionsAction() - So(a, ShouldNotBeNil) - So(a.Name(), ShouldEqual, "REPLACE_BETWEEN_POSITIONS") - So(a.FixedArgs(), ShouldResemble, []arg.TypeList{arg.EventList}) + funcName := strings.NewReplaceBetweenPositionsAction().Name() + Convey("test Positive testcase", t, func() { + a, err := runtime.NewAction([]interface{}{funcName, "$.test", 7, 11, "Vanus"}) + So(err, ShouldBeNil) e := cetest.MinEvent() e.SetExtension("test", "Hello, World!") ceCtx := &context.EventContext{ Event: &e, } - - // Positive test case - args := []interface{}{"$.test", 7, 11, "Vanus"} - err := a.Execute(ceCtx, args) + err = a.Execute(ceCtx) So(err, ShouldBeNil) So(e.Extensions()["test"], ShouldEqual, "Hello, Vanus!") + }) - // Negative test case: startPosition greater than length of string - args = []interface{}{"$.test", 100, 110, "Vanus"} - err = a.Execute(ceCtx, args) - So(err, ShouldNotBeNil) - So(err.Error(), ShouldEqual, "Start position must be less than the length of the string") + 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","Hello Joe, Vanus is an amazing technology") + ceCtx := &context.EventContext{ + Event: &e, + } + err = a.Execute(ceCtx) + So(err, ShouldBeNil) + So(e.Extensions()["test"], ShouldEqual, "Start position must be less than the length of the string") + }) - // Negative test case: endPosition greater than length of string - args = []interface{}{"$.test", 7, 110, "Vanus"} - err = a.Execute(ceCtx, args) - So(err, ShouldNotBeNil) - So(err.Error(), ShouldEqual, "End 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", "Vanus is an opensource technology") + ceCtx := &context.EventContext{ + Event: &e, + } + err = a.Execute(ceCtx) + So(err, ShouldBeNil) + So(e.Extensions()["test"], ShouldEqual, "End position must be less than the length of the string") + }) - // Negative test case: startPosition greater than endPosition - args = []interface{}{"$.test", 11, 7, "Vanus"} - err = a.Execute(ceCtx, args) - So(err, ShouldNotBeNil) - So(err.Error(), ShouldEqual, "Start position must be less than end position") - }) + 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", "Golang is an opensource language") + ceCtx := &context.EventContext{ + Event: &e, + } + err = a.Execute(ceCtx) + So(err, ShouldBeNil) + So(e.Extensions()["test"], ShouldEqual, "Start position must be less than end position") + }) } diff --git a/internal/primitive/transform/function/strings_functions.go b/internal/primitive/transform/function/strings_functions.go index f0b2524bf..9651f5f27 100644 --- a/internal/primitive/transform/function/strings_functions.go +++ b/internal/primitive/transform/function/strings_functions.go @@ -99,6 +99,10 @@ var ReplaceBetweenPositionsFunction = function{ return nil, fmt.Errorf("Start position must be less than end position") } + if endPosition >= len(path) { + return nil, fmt.Errorf("End position must be less than the length of the string") + } + return path[:int(startPosition)] + targetValue + path[int(endPosition):], nil - } + }, } From 1c960836d1331d14e142929eb7617d86ea28b6dd Mon Sep 17 00:00:00 2001 From: Jeffrey Whewhetu Date: Wed, 11 Jan 2023 08:57:56 +0100 Subject: [PATCH 4/6] fix tests in replace-between-positions --- .../strings/replace_between_positions_test.go | 18 +++++++++--------- .../transform/function/strings_functions.go | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/primitive/transform/action/strings/replace_between_positions_test.go b/internal/primitive/transform/action/strings/replace_between_positions_test.go index 5ea0ec03b..a5e7c5da1 100644 --- a/internal/primitive/transform/action/strings/replace_between_positions_test.go +++ b/internal/primitive/transform/action/strings/replace_between_positions_test.go @@ -44,38 +44,38 @@ func TestReplaceBetweenPositionsAction(t *testing.T) { a, err := runtime.NewAction([]interface{}{funcName, "$.test", 100, 8, "Dan"}) So(err, ShouldBeNil) e := cetest.MinEvent() - e.SetExtension("test","Hello Joe, Vanus is an amazing technology") + 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, ShouldBeNil) - So(e.Extensions()["test"], ShouldEqual, "Start position must be less than the length of the string") + So(err, ShouldNotBeNil) + So(e.Extensions()["test"], ShouldEqual, "Hello Joe, Vanus is an amazing technology") }) 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", "Vanus is an opensource technology") + 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, ShouldBeNil) - So(e.Extensions()["test"], ShouldEqual, "End position must be less than the length of the string") + So(err, ShouldNotBeNil) + So(e.Extensions()["test"], ShouldEqual, "Vanus is an opensource technology") }) 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", "Golang is an opensource language") + e.SetExtension("test", "Start position must be less than end position") ceCtx := &context.EventContext{ Event: &e, } err = a.Execute(ceCtx) - So(err, ShouldBeNil) - So(e.Extensions()["test"], ShouldEqual, "Start position must be less than end position") + So(err, ShouldNotBeNil) + So(e.Extensions()["test"], ShouldEqual, "Golang is an opensource language") }) } diff --git a/internal/primitive/transform/function/strings_functions.go b/internal/primitive/transform/function/strings_functions.go index 9651f5f27..d030da7df 100644 --- a/internal/primitive/transform/function/strings_functions.go +++ b/internal/primitive/transform/function/strings_functions.go @@ -92,15 +92,15 @@ var ReplaceBetweenPositionsFunction = function{ endPosition := 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") + return nil, fmt.Errorf("start 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 nil, fmt.Errorf("start position must be less than end position") } if endPosition >= len(path) { - return nil, fmt.Errorf("End position must be less than the length of the string") + return nil, fmt.Errorf("end position must be less than the length of the string") } return path[:int(startPosition)] + targetValue + path[int(endPosition):], nil From ab83026f7f28d2eb2fa010625d35553559e80a2c Mon Sep 17 00:00:00 2001 From: Jeffrey Whewhetu Date: Wed, 11 Jan 2023 10:04:41 +0100 Subject: [PATCH 5/6] Fix bug --- .../action/strings/replace_between_positions_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/primitive/transform/action/strings/replace_between_positions_test.go b/internal/primitive/transform/action/strings/replace_between_positions_test.go index a5e7c5da1..ece16cc6a 100644 --- a/internal/primitive/transform/action/strings/replace_between_positions_test.go +++ b/internal/primitive/transform/action/strings/replace_between_positions_test.go @@ -50,7 +50,7 @@ func TestReplaceBetweenPositionsAction(t *testing.T) { } err = a.Execute(ceCtx) So(err, ShouldNotBeNil) - So(e.Extensions()["test"], ShouldEqual, "Hello Joe, Vanus is an amazing technology") + 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() { @@ -63,7 +63,7 @@ func TestReplaceBetweenPositionsAction(t *testing.T) { } err = a.Execute(ceCtx) So(err, ShouldNotBeNil) - So(e.Extensions()["test"], ShouldEqual, "Vanus is an opensource technology") + 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() { @@ -76,6 +76,6 @@ func TestReplaceBetweenPositionsAction(t *testing.T) { } err = a.Execute(ceCtx) So(err, ShouldNotBeNil) - So(e.Extensions()["test"], ShouldEqual, "Golang is an opensource language") + So(e.Extensions()["test"], ShouldEqual, "Start position must be less than end position") }) } From bdcd51807c740bb3d3f60ffb4222acaa0389349d Mon Sep 17 00:00:00 2001 From: xdlbdy Date: Wed, 11 Jan 2023 21:05:33 +0800 Subject: [PATCH 6/6] fix: fix code error Signed-off-by: xdlbdy --- .../strings/replace_between_positions_test.go | 8 +++--- internal/primitive/transform/common/cast.go | 2 ++ .../transform/function/strings_functions.go | 25 ++++++++----------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/internal/primitive/transform/action/strings/replace_between_positions_test.go b/internal/primitive/transform/action/strings/replace_between_positions_test.go index ece16cc6a..b6faf0f64 100644 --- a/internal/primitive/transform/action/strings/replace_between_positions_test.go +++ b/internal/primitive/transform/action/strings/replace_between_positions_test.go @@ -28,7 +28,7 @@ func TestReplaceBetweenPositionsAction(t *testing.T) { funcName := strings.NewReplaceBetweenPositionsAction().Name() Convey("test Positive testcase", t, func() { - a, err := runtime.NewAction([]interface{}{funcName, "$.test", 7, 11, "Vanus"}) + a, err := runtime.NewAction([]interface{}{funcName, "$.test", 7, 12, "Vanus"}) So(err, ShouldBeNil) e := cetest.MinEvent() e.SetExtension("test", "Hello, World!") @@ -44,7 +44,7 @@ func TestReplaceBetweenPositionsAction(t *testing.T) { 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") + e.SetExtension("test", "Start position must be less than the length of the string") ceCtx := &context.EventContext{ Event: &e, } @@ -57,7 +57,7 @@ func TestReplaceBetweenPositionsAction(t *testing.T) { 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") + e.SetExtension("test", "End position must be less than the length of the string") ceCtx := &context.EventContext{ Event: &e, } @@ -67,7 +67,7 @@ func TestReplaceBetweenPositionsAction(t *testing.T) { }) Convey("test Negative testcase: startPosition greater than endPosition", t, func() { - a, err := runtime.NewAction([]interface{}{funcName, "$.test", 12, 5,"Python"}) + 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") diff --git a/internal/primitive/transform/common/cast.go b/internal/primitive/transform/common/cast.go index 89f1b0973..7218e8395 100644 --- a/internal/primitive/transform/common/cast.go +++ b/internal/primitive/transform/common/cast.go @@ -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: diff --git a/internal/primitive/transform/function/strings_functions.go b/internal/primitive/transform/function/strings_functions.go index d030da7df..386a42db6 100644 --- a/internal/primitive/transform/function/strings_functions.go +++ b/internal/primitive/transform/function/strings_functions.go @@ -84,25 +84,22 @@ var SplitWithSepFunction = function{ } var ReplaceBetweenPositionsFunction = function{ - name: "REPLACE_BETWEEN_POSITIONS", - fixedArgs: []common.Type{common.String, common.Number, common.Number, common.String}, + 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 := args[1].(float64) - endPosition := args[2].(float64) - targetValue := args[3].(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") + return nil, fmt.Errorf("start 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") - } - if endPosition >= len(path) { - return nil, fmt.Errorf("end position must be less than the length of the string") + return nil, fmt.Errorf("end position must be less than the length of the string") } - - return path[:int(startPosition)] + targetValue + path[int(endPosition):], nil + if startPosition >= endPosition { + return nil, fmt.Errorf("start position must be less than end position") + } + return path[:startPosition] + targetValue + path[endPosition:], nil }, }