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 "check_custom_values" #440

Merged
merged 5 commits into from
Feb 21, 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,68 @@
// 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 (
"strings"

"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 checkCustomValueAction struct {
action.CommonAction
}

// NewCheckCustomValuesAction ["check_custom_values","sourceJsonPath", "customValue",
// "targetJsonPath", "trueFlagReplacement", "falseFlagReplacement"].
func NewCheckCustomValuesAction() action.Action {
return &checkCustomValueAction{
CommonAction: action.CommonAction{
ActionName: "CHECK_CUSTOM_VALUES",
FixedArgs: []arg.TypeList{
arg.EventList,
[]arg.Type{arg.Constant},
arg.EventList,
[]arg.Type{arg.Constant},
[]arg.Type{arg.Constant},
},
},
}
}

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

func (a *checkCustomValueAction) Execute(ceCtx *context.EventContext) error {
args, err := a.RunArgs(ceCtx)
if err != nil {
return err
}
str, _ := args[0].(string)
customValue, _ := args[1].(string)
trueFlagReplacement := args[2]
falseFlagReplacement := args[3]
if strings.Contains(str, customValue) {
return a.TargetArg.SetValue(ceCtx, trueFlagReplacement)
}
return a.TargetArg.SetValue(ceCtx, falseFlagReplacement)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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 (
"encoding/json"
"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 TestCheckCustomValuesAction(t *testing.T) {
funcName := strings.NewCheckCustomValuesAction().Name()
jsonStr := `{
"source": "value 2"
}`
Convey("contains", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.data.source", "value", "$.data.target", "true", "false"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
var data map[string]interface{}
err = json.Unmarshal([]byte(jsonStr), &data)
So(err, ShouldBeNil)
ceCtx := &context.EventContext{
Event: &e,
Data: data,
}
err = a.Execute(ceCtx)
So(err, ShouldBeNil)
res, ok := data["target"]
So(ok, ShouldBeTrue)
So(res, ShouldEqual, "true")
})
Convey("not contains", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.data.source", "Value", "$.data.target", "true", "false"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
var data map[string]interface{}
err = json.Unmarshal([]byte(jsonStr), &data)
So(err, ShouldBeNil)
ceCtx := &context.EventContext{
Event: &e,
Data: data,
}
err = a.Execute(ceCtx)
So(err, ShouldBeNil)
res, ok := data["target"]
So(ok, ShouldBeTrue)
So(res, ShouldEqual, "false")
})
Convey("contains, replacement int", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.data.source", "value", "$.data.target", 1, 0})
So(err, ShouldBeNil)
e := cetest.MinEvent()
var data map[string]interface{}
err = json.Unmarshal([]byte(jsonStr), &data)
So(err, ShouldBeNil)
ceCtx := &context.EventContext{
Event: &e,
Data: data,
}
err = a.Execute(ceCtx)
So(err, ShouldBeNil)
res, ok := data["target"]
So(ok, ShouldBeTrue)
So(res, ShouldEqual, 1)
})
Convey("source don't exist, runArgs error", t, func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.data.source2", "value", "$.data.target", "true", "false"})
So(err, ShouldBeNil)
e := cetest.MinEvent()
var data map[string]interface{}
err = json.Unmarshal([]byte(jsonStr), &data)
So(err, ShouldBeNil)
ceCtx := &context.EventContext{
Event: &e,
Data: data,
}
err = a.Execute(ceCtx)
So(err, ShouldNotBeNil)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ func BenchmarkAction(b *testing.B) {
b.Run("add_suffix", actionBenchmark([]interface{}{"add_suffix", "$.data.str", "suffix"}))
b.Run("replace_with_regex", actionBenchmark([]interface{}{"replace_with_regex", "$.data.str", "a", "Aa"}))
b.Run("capitalize_sentence", actionBenchmark([]interface{}{"capitalize_sentence", "$.data.str"}))
b.Run("check_custom_values", actionBenchmark([]interface{}{"check_custom_values", "$.data.str", "value", "$.data.target", "true", "false"}))
}
1 change: 1 addition & 0 deletions internal/primitive/transform/runtime/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func init() {
strings.NewReplaceStringAction,
strings.NewReplaceBetweenPositionsAction,
strings.NewCapitalizeSentenceAction,
strings.NewCheckCustomValuesAction,
// condition
condition.NewConditionIfAction,
// array
Expand Down