-
Notifications
You must be signed in to change notification settings - Fork 95
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 condition if (#353)
Signed-off-by: delu <[email protected]> Signed-off-by: delu <[email protected]>
- Loading branch information
Showing
11 changed files
with
243 additions
and
43 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
85 changes: 85 additions & 0 deletions
85
internal/primitive/transform/action/condition_if_action.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,85 @@ | ||
// 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 action | ||
|
||
import ( | ||
"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" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type conditionIfAction struct { | ||
commonAction | ||
} | ||
|
||
// ["condition_if","$.targetPath","$.path","op","compareValue","trueValue","falseValue"] | ||
// op must be string and only support ==,>=,>,<=,< . | ||
func newConditionIfAction() Action { | ||
return &conditionIfAction{ | ||
commonAction{ | ||
name: "CONDITION_IF", | ||
fixedArgs: []arg.TypeList{arg.EventList, arg.All, arg.All, arg.All, arg.All, arg.All}, | ||
}, | ||
} | ||
} | ||
|
||
func (a *conditionIfAction) Init(args []arg.Arg) error { | ||
a.targetArg = args[0] | ||
a.args = args[1:] | ||
return nil | ||
} | ||
|
||
func (a *conditionIfAction) Execute(ceCtx *context.EventContext) error { | ||
v, err := a.args[1].Evaluate(ceCtx) | ||
if err != nil { | ||
return errors.Wrapf(err, "arg %s evaluate error", a.args[1].Original()) | ||
} | ||
op, ok := v.(string) | ||
if !ok { | ||
return errors.New("op type must be string") | ||
} | ||
if op == "==" { | ||
a.argTypes = []common.Type{common.String, common.String, common.String, common.Any, common.Any} | ||
} else { | ||
switch op { | ||
case ">=", ">", "<=", "<": | ||
a.argTypes = []common.Type{common.Number, common.String, common.Number, common.Any, common.Any} | ||
default: | ||
return errors.Errorf("not support op [%s]", op) | ||
} | ||
} | ||
args, err := a.runArgs(ceCtx) | ||
if err != nil { | ||
return err | ||
} | ||
var result bool | ||
switch op { | ||
case "==": | ||
result = args[0] == args[2] | ||
case ">=": | ||
result = args[0].(float64) >= args[2].(float64) | ||
case ">": | ||
result = args[0].(float64) > args[2].(float64) | ||
case "<=": | ||
result = args[0].(float64) <= args[2].(float64) | ||
case "<": | ||
result = args[0].(float64) < args[2].(float64) | ||
} | ||
if result { | ||
return a.targetArg.SetValue(ceCtx, args[3]) | ||
} | ||
return a.targetArg.SetValue(ceCtx, args[4]) | ||
} |
103 changes: 103 additions & 0 deletions
103
internal/primitive/transform/action/condition_if_action_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,103 @@ | ||
// 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 action | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/linkall-labs/vanus/internal/primitive/transform/context" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestConditionIfAction(t *testing.T) { | ||
Convey("test condition if ==", t, func() { | ||
Convey("test string", func() { | ||
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", "==", "test", true, false}) | ||
So(err, ShouldBeNil) | ||
e := newEvent() | ||
e.SetExtension("test", "test") | ||
err = a.Execute(&context.EventContext{ | ||
Event: e, | ||
}) | ||
So(err, ShouldBeNil) | ||
So(e.Extensions()["test2"], ShouldEqual, true) | ||
}) | ||
Convey("test number", func() { | ||
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", "==", 123, true, false}) | ||
So(err, ShouldBeNil) | ||
e := newEvent() | ||
e.SetExtension("test", 123) | ||
err = a.Execute(&context.EventContext{ | ||
Event: e, | ||
}) | ||
So(err, ShouldBeNil) | ||
So(e.Extensions()["test2"], ShouldEqual, true) | ||
}) | ||
}) | ||
Convey("test condition >=", t, func() { | ||
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", ">=", int32(123), true, false}) | ||
So(err, ShouldBeNil) | ||
e := newEvent() | ||
e.SetExtension("test", "456") | ||
err = a.Execute(&context.EventContext{ | ||
Event: e, | ||
}) | ||
So(err, ShouldBeNil) | ||
So(e.Extensions()["test2"], ShouldEqual, true) | ||
}) | ||
Convey("test condition >", t, func() { | ||
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", ">", int32(123), true, false}) | ||
So(err, ShouldBeNil) | ||
e := newEvent() | ||
e.SetExtension("test", 456) | ||
err = a.Execute(&context.EventContext{ | ||
Event: e, | ||
}) | ||
So(err, ShouldBeNil) | ||
So(e.Extensions()["test2"], ShouldEqual, true) | ||
}) | ||
Convey("test condition <=", t, func() { | ||
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", "<=", "123", true, false}) | ||
So(err, ShouldBeNil) | ||
e := newEvent() | ||
e.SetExtension("test", 456) | ||
err = a.Execute(&context.EventContext{ | ||
Event: e, | ||
}) | ||
So(err, ShouldBeNil) | ||
So(e.Extensions()["test2"], ShouldEqual, false) | ||
}) | ||
Convey("test condition <", t, func() { | ||
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", "<", "123", true, false}) | ||
So(err, ShouldBeNil) | ||
e := newEvent() | ||
e.SetExtension("test", 456) | ||
err = a.Execute(&context.EventContext{ | ||
Event: e, | ||
}) | ||
So(err, ShouldBeNil) | ||
So(e.Extensions()["test2"], ShouldEqual, false) | ||
}) | ||
Convey("test condition unDefine op invalid", t, func() { | ||
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", "<==", "123", true, false}) | ||
So(err, ShouldBeNil) | ||
e := newEvent() | ||
e.SetExtension("test", 456) | ||
err = a.Execute(&context.EventContext{ | ||
Event: e, | ||
}) | ||
So(err, ShouldNotBeNil) | ||
}) | ||
} |
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
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
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
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
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
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
Oops, something went wrong.