-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add transformer function duplicate (#392)
* 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
1 parent
1395ee8
commit 060b7af
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
internal/primitive/transform/action/structs/duplicate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters