Skip to content

Commit

Permalink
feat: add transformer function duplicate (#392)
Browse files Browse the repository at this point in the history
* feat: add transformer function duplicate

Signed-off-by: n-kurasawa <[email protected]>

* Update internal/primitive/transform/action/structs/duplicate.go

fix the second arg type

Co-authored-by: delu <[email protected]>

Signed-off-by: n-kurasawa <[email protected]>
Co-authored-by: delu <[email protected]>
  • Loading branch information
n-kurasawa and xdlbdy authored Jan 6, 2023
1 parent 1395ee8 commit 060b7af
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
57 changes: 57 additions & 0 deletions internal/primitive/transform/action/structs/duplicate.go
Original file line number Diff line number Diff line change
@@ -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])
}
60 changes: 60 additions & 0 deletions internal/primitive/transform/action/structs/duplicate_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
})
}
1 change: 1 addition & 0 deletions internal/primitive/transform/runtime/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func init() {
structs.NewReplaceAction,
structs.NewMoveAction,
structs.NewRenameAction,
structs.NewDuplicateAction,
// math
math.NewMathAddAction,
math.NewMathSubAction,
Expand Down

0 comments on commit 060b7af

Please sign in to comment.