From 060b7af8e87e0e29a9e99d6f8d4e99400229ffeb Mon Sep 17 00:00:00 2001 From: n-kurasawa Date: Fri, 6 Jan 2023 12:38:01 +0900 Subject: [PATCH] feat: add transformer function duplicate (#392) * feat: add transformer function duplicate Signed-off-by: n-kurasawa * Update internal/primitive/transform/action/structs/duplicate.go fix the second arg type Co-authored-by: delu Signed-off-by: n-kurasawa Co-authored-by: delu --- .../transform/action/structs/duplicate.go | 57 ++++++++++++++++++ .../action/structs/duplicate_test.go | 60 +++++++++++++++++++ internal/primitive/transform/runtime/init.go | 1 + 3 files changed, 118 insertions(+) create mode 100644 internal/primitive/transform/action/structs/duplicate.go create mode 100644 internal/primitive/transform/action/structs/duplicate_test.go diff --git a/internal/primitive/transform/action/structs/duplicate.go b/internal/primitive/transform/action/structs/duplicate.go new file mode 100644 index 000000000..123d1d53b --- /dev/null +++ b/internal/primitive/transform/action/structs/duplicate.go @@ -0,0 +1,57 @@ +// 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 structs + +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 duplicateAction struct { + action.CommonAction +} + +// NewDuplicateAction ["duplicate", "sourcePath", "targetPath"]. +func NewDuplicateAction() action.Action { + return &duplicateAction{ + action.CommonAction{ + ActionName: "DUPLICATE", + FixedArgs: []arg.TypeList{arg.EventList, arg.EventList}, + }, + } +} + +func (a *duplicateAction) Init(args []arg.Arg) error { + a.TargetArg = args[1] + a.Args = args[:1] + a.ArgTypes = []common.Type{common.Any} + return nil +} + +func (a *duplicateAction) Execute(ceCtx *context.EventContext) error { + v, _ := a.TargetArg.Evaluate(ceCtx) + if v != nil { + return fmt.Errorf("key %s exist", a.TargetArg.Original()) + } + args, err := a.RunArgs(ceCtx) + if err != nil { + return err + } + return a.TargetArg.SetValue(ceCtx, args[0]) +} diff --git a/internal/primitive/transform/action/structs/duplicate_test.go b/internal/primitive/transform/action/structs/duplicate_test.go new file mode 100644 index 000000000..1034757f8 --- /dev/null +++ b/internal/primitive/transform/action/structs/duplicate_test.go @@ -0,0 +1,60 @@ +// 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 structs_test + +import ( + "testing" + + cetest "github.com/cloudevents/sdk-go/v2/test" + "github.com/linkall-labs/vanus/internal/primitive/transform/action/structs" + "github.com/linkall-labs/vanus/internal/primitive/transform/context" + "github.com/linkall-labs/vanus/internal/primitive/transform/runtime" + . "github.com/smartystreets/goconvey/convey" +) + +func TestDuplicateAction(t *testing.T) { + funcName := structs.NewDuplicateAction().Name() + Convey("test duplicate", t, func() { + Convey("duplicate target key exist", func() { + a, err := runtime.NewAction([]interface{}{funcName, "$.test", "$.data.abc.test"}) + So(err, ShouldBeNil) + e := cetest.MinEvent() + e.SetExtension("test", "abc") + err = a.Execute(&context.EventContext{ + Event: &e, + Data: map[string]interface{}{ + "abc": map[string]interface{}{ + "test": "value", + }, + }, + }) + So(err, ShouldNotBeNil) + }) + Convey("duplicate", func() { + a, err := runtime.NewAction([]interface{}{funcName, "$.test", "$.data.abc.test"}) + So(err, ShouldBeNil) + e := cetest.MinEvent() + e.SetExtension("test", "abc") + ceCtx := &context.EventContext{ + Event: &e, + Data: map[string]interface{}{}, + } + err = a.Execute(ceCtx) + So(err, ShouldBeNil) + So(e.Extensions()["test"], ShouldEqual, "abc") + So(ceCtx.Data.(map[string]interface{})["abc"].(map[string]interface{})["test"], ShouldEqual, "abc") + }) + }) +} diff --git a/internal/primitive/transform/runtime/init.go b/internal/primitive/transform/runtime/init.go index 70c7d62fe..ce4ceba29 100644 --- a/internal/primitive/transform/runtime/init.go +++ b/internal/primitive/transform/runtime/init.go @@ -33,6 +33,7 @@ func init() { structs.NewReplaceAction, structs.NewMoveAction, structs.NewRenameAction, + structs.NewDuplicateAction, // math math.NewMathAddAction, math.NewMathSubAction,