From b43d14edc8adc299a044fd69d28a52dff55855f1 Mon Sep 17 00:00:00 2001 From: dhanu Date: Tue, 21 Feb 2023 09:52:45 +0700 Subject: [PATCH 1/5] feat: implement function "check_custom_values" --- .../action/strings/check_custom_values.go | 60 ++++++++++++++ .../strings/check_custom_values_test.go | 81 +++++++++++++++++++ .../transform/runtime/action_bench_test.go | 1 + internal/primitive/transform/runtime/init.go | 1 + 4 files changed, 143 insertions(+) create mode 100644 internal/primitive/transform/action/strings/check_custom_values.go create mode 100644 internal/primitive/transform/action/strings/check_custom_values_test.go diff --git a/internal/primitive/transform/action/strings/check_custom_values.go b/internal/primitive/transform/action/strings/check_custom_values.go new file mode 100644 index 000000000..f521cbfaf --- /dev/null +++ b/internal/primitive/transform/action/strings/check_custom_values.go @@ -0,0 +1,60 @@ +// 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 = append(args[:2], args[3:]...) + a.ArgTypes = []common.Type{common.String, common.String, common.String, common.String} + return nil +} + +func (a *checkCustomValueAction) Execute(ceCtx *context.EventContext) error { + args, err := a.RunArgs(ceCtx) + if err != nil { + return err + } + str, _ := args[0].(string) + checkStr, _ := args[1].(string) + trueFlagReplacement, _ := args[2].(string) + falseFlagReplacement, _ := args[3].(string) + if strings.Contains(str, checkStr) { + return a.TargetArg.SetValue(ceCtx, trueFlagReplacement) + } + return a.TargetArg.SetValue(ceCtx, falseFlagReplacement) +} diff --git a/internal/primitive/transform/action/strings/check_custom_values_test.go b/internal/primitive/transform/action/strings/check_custom_values_test.go new file mode 100644 index 000000000..a9467dda4 --- /dev/null +++ b/internal/primitive/transform/action/strings/check_custom_values_test.go @@ -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 ( + "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("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) + }) +} diff --git a/internal/primitive/transform/runtime/action_bench_test.go b/internal/primitive/transform/runtime/action_bench_test.go index 5005a4e57..b54f5f4b0 100644 --- a/internal/primitive/transform/runtime/action_bench_test.go +++ b/internal/primitive/transform/runtime/action_bench_test.go @@ -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"})) } diff --git a/internal/primitive/transform/runtime/init.go b/internal/primitive/transform/runtime/init.go index 101adff3a..6b3747eda 100644 --- a/internal/primitive/transform/runtime/init.go +++ b/internal/primitive/transform/runtime/init.go @@ -52,6 +52,7 @@ func init() { strings.NewReplaceStringAction, strings.NewReplaceBetweenPositionsAction, strings.NewCapitalizeSentenceAction, + strings.NewCheckCustomValuesAction, // condition condition.NewConditionIfAction, // array From fc02a352caf3e8565baba5b9ddc922a6e0976704 Mon Sep 17 00:00:00 2001 From: dhanu Date: Tue, 21 Feb 2023 10:02:36 +0700 Subject: [PATCH 2/5] rename --- .../primitive/transform/action/strings/check_custom_values.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/primitive/transform/action/strings/check_custom_values.go b/internal/primitive/transform/action/strings/check_custom_values.go index f521cbfaf..e8edd36bf 100644 --- a/internal/primitive/transform/action/strings/check_custom_values.go +++ b/internal/primitive/transform/action/strings/check_custom_values.go @@ -50,10 +50,10 @@ func (a *checkCustomValueAction) Execute(ceCtx *context.EventContext) error { return err } str, _ := args[0].(string) - checkStr, _ := args[1].(string) + customValue, _ := args[1].(string) trueFlagReplacement, _ := args[2].(string) falseFlagReplacement, _ := args[3].(string) - if strings.Contains(str, checkStr) { + if strings.Contains(str, customValue) { return a.TargetArg.SetValue(ceCtx, trueFlagReplacement) } return a.TargetArg.SetValue(ceCtx, falseFlagReplacement) From 68c78ec62e890fa5db1f9347d2fb54be6cb75e86 Mon Sep 17 00:00:00 2001 From: dhanu Date: Tue, 21 Feb 2023 10:35:49 +0700 Subject: [PATCH 3/5] fix lint --- .../action/strings/check_custom_values.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/primitive/transform/action/strings/check_custom_values.go b/internal/primitive/transform/action/strings/check_custom_values.go index e8edd36bf..5822e1d68 100644 --- a/internal/primitive/transform/action/strings/check_custom_values.go +++ b/internal/primitive/transform/action/strings/check_custom_values.go @@ -27,19 +27,27 @@ type checkCustomValueAction struct { action.CommonAction } -// NewCheckCustomValuesAction ["check_custom_values","sourceJsonPath", "customValue", "targetJsonPath", "trueFlagReplacement", "falseFlagReplacement"]. +// 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}}, + 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 = append(args[:2], args[3:]...) + a.Args = args[:2] + a.Args = append(a.Args, args[3:]...) a.ArgTypes = []common.Type{common.String, common.String, common.String, common.String} return nil } From d67c7f65ba6fe3079861537174286d340d07d851 Mon Sep 17 00:00:00 2001 From: dhanu Date: Tue, 21 Feb 2023 10:58:48 +0700 Subject: [PATCH 4/5] fix comment --- .../primitive/transform/action/strings/check_custom_values.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/primitive/transform/action/strings/check_custom_values.go b/internal/primitive/transform/action/strings/check_custom_values.go index 5822e1d68..4741c1a16 100644 --- a/internal/primitive/transform/action/strings/check_custom_values.go +++ b/internal/primitive/transform/action/strings/check_custom_values.go @@ -48,7 +48,7 @@ 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.String, common.String} + a.ArgTypes = []common.Type{common.String, common.String, common.Any, common.Any} return nil } From 463fd6dad8cfdfcb5f4ee927925bd2ff653057e9 Mon Sep 17 00:00:00 2001 From: dhanu Date: Tue, 21 Feb 2023 15:19:09 +0700 Subject: [PATCH 5/5] fix review --- .../action/strings/check_custom_values.go | 4 ++-- .../action/strings/check_custom_values_test.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/primitive/transform/action/strings/check_custom_values.go b/internal/primitive/transform/action/strings/check_custom_values.go index 4741c1a16..e8c4a0db9 100644 --- a/internal/primitive/transform/action/strings/check_custom_values.go +++ b/internal/primitive/transform/action/strings/check_custom_values.go @@ -59,8 +59,8 @@ func (a *checkCustomValueAction) Execute(ceCtx *context.EventContext) error { } str, _ := args[0].(string) customValue, _ := args[1].(string) - trueFlagReplacement, _ := args[2].(string) - falseFlagReplacement, _ := args[3].(string) + trueFlagReplacement := args[2] + falseFlagReplacement := args[3] if strings.Contains(str, customValue) { return a.TargetArg.SetValue(ceCtx, trueFlagReplacement) } diff --git a/internal/primitive/transform/action/strings/check_custom_values_test.go b/internal/primitive/transform/action/strings/check_custom_values_test.go index a9467dda4..3bc7a49d5 100644 --- a/internal/primitive/transform/action/strings/check_custom_values_test.go +++ b/internal/primitive/transform/action/strings/check_custom_values_test.go @@ -64,6 +64,23 @@ func TestCheckCustomValuesAction(t *testing.T) { 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)