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: implement function split from start #472

Merged
merged 9 commits into from
Mar 6, 2023
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"
)

// NewReplaceBetweenDelimitersAction ["path","startDelimiter","endDelimiter","newValue"].
func NewReplaceBetweenDelimitersAction() action.Action {
a := &action.SourceTargetSameAction{}
a.CommonAction = action.CommonAction{
ActionName: "REPLACE_BETWEEN_DELIMITERS",
FixedArgs: []arg.TypeList{arg.EventList, arg.All, arg.All, arg.All},
Fn: function.ReplaceBetweenDelimitersFunction,
}
return a
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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 TestReplaceBetweenDelimitersAction(t *testing.T) {
funcName := strings.NewReplaceBetweenDelimitersAction().Name()

Convey("test startDelimiter and endDelimiter present in string testcase", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.test", "&&", "&&", "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 startDelimiter and endDelimiter present in string testcase", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.test", "^^", "^^", "lots of"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
e.SetExtension("test", "Vanus has ^^many^^ beginner friendly open issues!")
ceCtx := &context.EventContext{
Event: &e,
}
err = a.Execute(ceCtx)
So(err, ShouldBeNil)
So(e.Extensions()["test"], ShouldEqual, "Vanus has lots of beginner friendly open issues!")
})

Convey("test startDelimiter and endDelimiter not present in string testcase", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.test", "**", "**", "fun"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
e.SetExtension("test", "Contributing to Vanus Opensource project is %%an eye opener%%!")
ceCtx := &context.EventContext{
Event: &e,
}
err = a.Execute(ceCtx)
So(err, ShouldNotBeNil)
So(e.Extensions()["test"], ShouldEqual, "Contributing to Vanus Opensource project is %%an eye opener%%!")
})

Convey("test endDelimiter before startDelimiter in string testcase", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.test", "&&", "!!", "love"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
e.SetExtension("test", "I !!like&& opensource contributions")
ceCtx := &context.EventContext{
Event: &e,
}
err = a.Execute(ceCtx)
So(err, ShouldNotBeNil)
So(e.Extensions()["test"], ShouldEqual, "I !!like&& opensource contributions")
})

Convey("test Only endDelimiter present in string testcase", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.test", "&&", "**", "supported"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
e.SetExtension("test", "FOSS is !!powered** by open communities")
ceCtx := &context.EventContext{
Event: &e,
}
err = a.Execute(ceCtx)
So(err, ShouldNotBeNil)
So(e.Extensions()["test"], ShouldEqual, "FOSS is !!powered** by open communities")
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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 (
"fmt"

"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/context"
)

type splitBetweenPositionsAction struct {
action.CommonAction
}

// NewSplitBetweenPositionsAction["sourceJSONPath", "startPosition", "endPosition", "targetJsonPath"].
func NewSplitBetweenPositionsAction() action.Action {
return &splitBetweenPositionsAction{
CommonAction: action.CommonAction{
ActionName: "SPLIT_BETWEEN_POSITIONS",
FixedArgs: []arg.TypeList{arg.EventList, arg.All, arg.All, []arg.Type{arg.EventData}},
},
}
}

func (a *splitBetweenPositionsAction) Init(args []arg.Arg) error {
a.TargetArg = args[3]
a.Args = args[:3]
a.ArgTypes = []common.Type{common.String, common.Int, common.Int}
return nil
}

func (a *splitBetweenPositionsAction) Execute(ceCtx *context.EventContext) error {
args, err := a.RunArgs(ceCtx)
if err != nil {
return err
}

v, _ := a.TargetArg.Evaluate(ceCtx)
if v != nil {
return fmt.Errorf("key %s exists", a.TargetArg.Original())
}

sourceJSONPath, _ := args[0].(string)
startPosition, _ := args[1].(int)
endPosition, _ := args[2].(int)

var substrings []string

switch {
case startPosition >= endPosition:
// if startPosition is gte endPosition, return an error
return fmt.Errorf("start position must be less than the endPosition")
case startPosition >= len(sourceJSONPath):
// if startPosition is beyond the end of the string
substrings = []string{
sourceJSONPath,
"",
"",
}
case endPosition > len(sourceJSONPath):
// if endPosition is beyond the end of the string
substrings = []string{
sourceJSONPath[:startPosition],
sourceJSONPath[startPosition:],
"",
}
default:
substrings = []string{
sourceJSONPath[:startPosition],
sourceJSONPath[startPosition:endPosition],
sourceJSONPath[endPosition:],
}
}
return a.TargetArg.SetValue(ceCtx, substrings)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// 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 TestSplitBetweenPositionsAction(t *testing.T) {
funcName := strings.NewSplitBetweenPositionsAction().Name()

Convey("test Positive testcase", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.data.appinfoA", 2, 4, "$.data.appinfoB"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
data := map[string]interface{}{
"appinfoA": "hello world!",
}
ceCtx := &context.EventContext{
Event: &e,
Data: data,
}
err = a.Execute(ceCtx)
So(err, ShouldBeNil)
res, ok := data["appinfoB"]
So(ok, ShouldBeTrue)
So(res, ShouldResemble, []string{"he", "ll", "o world!"})
})

Convey("test when start position is greater than the end position", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.data.appinfoA", 7, 6, "$.data.appinfoB"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
data := map[string]interface{}{
"appinfoA": "hello world!",
}
ceCtx := &context.EventContext{
Event: &e,
Data: data,
}
err = a.Execute(ceCtx)
So(err.Error(), ShouldEqual, "start position must be less than the endPosition")
})

Convey("test when start position is greater than the length of the string", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.data.appinfoA", 100, 200, "$.data.appinfoB"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
data := map[string]interface{}{
"appinfoA": "hello world!",
}
ceCtx := &context.EventContext{
Event: &e,
Data: data,
}
err = a.Execute(ceCtx)
So(err, ShouldBeNil)
res, ok := data["appinfoB"]
So(ok, ShouldBeTrue)
So(res, ShouldResemble, []string{"hello world!", "", ""})
})

Convey("test when end position is greater than the length of the string", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.data.appinfoA", 4, 200, "$.data.appinfoB"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
data := map[string]interface{}{
"appinfoA": "hello world!",
}
ceCtx := &context.EventContext{
Event: &e,
Data: data,
}
err = a.Execute(ceCtx)
So(err, ShouldBeNil)
res, ok := data["appinfoB"]
So(ok, ShouldBeTrue)
So(res, ShouldResemble, []string{"hell", "o world!", ""})
})

Convey("test when target key exists", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.data.appinfoA", 2, 2, "$.data.appinfoB"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
data := map[string]interface{}{
"appinfoA": "hello world!",
"appinfoB": "",
}
ceCtx := &context.EventContext{
Event: &e,
Data: data,
}
err = a.Execute(ceCtx)
So(err.Error(), ShouldEqual, "key $.data.appinfoB exists")
})
}
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
}
Loading