Skip to content

Commit

Permalink
update: back merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
allensuvorov committed Mar 6, 2023
2 parents f622733 + 7d6f616 commit ee76217
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
44 changes: 44 additions & 0 deletions internal/primitive/transform/action/strings/split_from_start.go
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 internal/primitive/transform/action/strings/split_from_start_test.go
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")
})
}
20 changes: 20 additions & 0 deletions internal/primitive/transform/function/strings_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,23 @@ var CapitalizeWord = function{
return string(rs), nil
},
}

var SplitFromStart = function{
name: "SPLIT_FROM_START",
fixedArgs: []common.Type{common.String, common.Int},
fn: func(args []interface{}) (interface{}, error) {
value, _ := args[0].(string)
splitPosition, _ := args[1].(int)

if splitPosition <= 0 {
return nil, fmt.Errorf("split position must be more than zero")
}

if splitPosition >= len(value) {
return []string{value, ""}, nil
}

result := []string{value[:splitPosition], value[splitPosition:]}
return result, 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 @@ -57,6 +57,7 @@ func init() {
strings.NewCapitalizeWordAction,
strings.NewSplitWithDelimiterAction,
strings.NewSplitBetweenPositionsAction,
strings.NewSplitFromStartAction,
// condition
condition.NewConditionIfAction,
// array
Expand Down

0 comments on commit ee76217

Please sign in to comment.