Skip to content

Commit 4ed721d

Browse files
authored
feat: add transformer function condition if (#353)
Signed-off-by: delu <[email protected]> Signed-off-by: delu <[email protected]>
1 parent ef58b6b commit 4ed721d

File tree

11 files changed

+243
-43
lines changed

11 files changed

+243
-43
lines changed

internal/primitive/transform/action/action.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import (
1818
"fmt"
1919
"strings"
2020

21+
"github.com/linkall-labs/vanus/internal/primitive/transform/function"
22+
2123
"github.com/linkall-labs/vanus/internal/primitive/transform/arg"
24+
"github.com/linkall-labs/vanus/internal/primitive/transform/common"
2225
"github.com/linkall-labs/vanus/internal/primitive/transform/context"
23-
"github.com/linkall-labs/vanus/internal/primitive/transform/function"
2426
"github.com/pkg/errors"
2527
)
2628

@@ -46,7 +48,7 @@ type commonAction struct {
4648
fn function.Function
4749

4850
args []arg.Arg
49-
argTypes []function.Type
51+
argTypes []common.Type
5052
targetArg arg.Arg
5153
}
5254

@@ -91,7 +93,7 @@ func (a *commonAction) setArgTypes() error {
9193
if len(a.args) > a.fn.Arity() && !a.fn.IsVariadic() {
9294
return ErrArgNumber
9395
}
94-
argTypes := make([]function.Type, len(a.args))
96+
argTypes := make([]common.Type, len(a.args))
9597
for i := 0; i < len(a.args); i++ {
9698
argTypes[i] = *a.fn.ArgType(i)
9799
}
@@ -124,7 +126,7 @@ func (a *commonAction) runArgs(ceCtx *context.EventContext) ([]interface{}, erro
124126
if err != nil {
125127
return nil, errors.Wrapf(err, "arg %s evaluate error", _arg.Original())
126128
}
127-
v, err := function.Cast(value, a.argTypes[i])
129+
v, err := common.Cast(value, a.argTypes[i])
128130
if err != nil {
129131
return nil, err
130132
}
@@ -177,6 +179,8 @@ func init() {
177179
newAddPrefixAction,
178180
newAddSuffixAction,
179181
newReplaceWithRegexAction,
182+
// condition
183+
newConditionIfAction,
180184
} {
181185
if err := AddAction(fn); err != nil {
182186
panic(err)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2022 Linkall Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package action
16+
17+
import (
18+
"github.com/linkall-labs/vanus/internal/primitive/transform/arg"
19+
"github.com/linkall-labs/vanus/internal/primitive/transform/common"
20+
"github.com/linkall-labs/vanus/internal/primitive/transform/context"
21+
"github.com/pkg/errors"
22+
)
23+
24+
type conditionIfAction struct {
25+
commonAction
26+
}
27+
28+
// ["condition_if","$.targetPath","$.path","op","compareValue","trueValue","falseValue"]
29+
// op must be string and only support ==,>=,>,<=,< .
30+
func newConditionIfAction() Action {
31+
return &conditionIfAction{
32+
commonAction{
33+
name: "CONDITION_IF",
34+
fixedArgs: []arg.TypeList{arg.EventList, arg.All, arg.All, arg.All, arg.All, arg.All},
35+
},
36+
}
37+
}
38+
39+
func (a *conditionIfAction) Init(args []arg.Arg) error {
40+
a.targetArg = args[0]
41+
a.args = args[1:]
42+
return nil
43+
}
44+
45+
func (a *conditionIfAction) Execute(ceCtx *context.EventContext) error {
46+
v, err := a.args[1].Evaluate(ceCtx)
47+
if err != nil {
48+
return errors.Wrapf(err, "arg %s evaluate error", a.args[1].Original())
49+
}
50+
op, ok := v.(string)
51+
if !ok {
52+
return errors.New("op type must be string")
53+
}
54+
if op == "==" {
55+
a.argTypes = []common.Type{common.String, common.String, common.String, common.Any, common.Any}
56+
} else {
57+
switch op {
58+
case ">=", ">", "<=", "<":
59+
a.argTypes = []common.Type{common.Number, common.String, common.Number, common.Any, common.Any}
60+
default:
61+
return errors.Errorf("not support op [%s]", op)
62+
}
63+
}
64+
args, err := a.runArgs(ceCtx)
65+
if err != nil {
66+
return err
67+
}
68+
var result bool
69+
switch op {
70+
case "==":
71+
result = args[0] == args[2]
72+
case ">=":
73+
result = args[0].(float64) >= args[2].(float64)
74+
case ">":
75+
result = args[0].(float64) > args[2].(float64)
76+
case "<=":
77+
result = args[0].(float64) <= args[2].(float64)
78+
case "<":
79+
result = args[0].(float64) < args[2].(float64)
80+
}
81+
if result {
82+
return a.targetArg.SetValue(ceCtx, args[3])
83+
}
84+
return a.targetArg.SetValue(ceCtx, args[4])
85+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2022 Linkall Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package action
16+
17+
import (
18+
"testing"
19+
20+
"github.com/linkall-labs/vanus/internal/primitive/transform/context"
21+
. "github.com/smartystreets/goconvey/convey"
22+
)
23+
24+
func TestConditionIfAction(t *testing.T) {
25+
Convey("test condition if ==", t, func() {
26+
Convey("test string", func() {
27+
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", "==", "test", true, false})
28+
So(err, ShouldBeNil)
29+
e := newEvent()
30+
e.SetExtension("test", "test")
31+
err = a.Execute(&context.EventContext{
32+
Event: e,
33+
})
34+
So(err, ShouldBeNil)
35+
So(e.Extensions()["test2"], ShouldEqual, true)
36+
})
37+
Convey("test number", func() {
38+
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", "==", 123, true, false})
39+
So(err, ShouldBeNil)
40+
e := newEvent()
41+
e.SetExtension("test", 123)
42+
err = a.Execute(&context.EventContext{
43+
Event: e,
44+
})
45+
So(err, ShouldBeNil)
46+
So(e.Extensions()["test2"], ShouldEqual, true)
47+
})
48+
})
49+
Convey("test condition >=", t, func() {
50+
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", ">=", int32(123), true, false})
51+
So(err, ShouldBeNil)
52+
e := newEvent()
53+
e.SetExtension("test", "456")
54+
err = a.Execute(&context.EventContext{
55+
Event: e,
56+
})
57+
So(err, ShouldBeNil)
58+
So(e.Extensions()["test2"], ShouldEqual, true)
59+
})
60+
Convey("test condition >", t, func() {
61+
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", ">", int32(123), true, false})
62+
So(err, ShouldBeNil)
63+
e := newEvent()
64+
e.SetExtension("test", 456)
65+
err = a.Execute(&context.EventContext{
66+
Event: e,
67+
})
68+
So(err, ShouldBeNil)
69+
So(e.Extensions()["test2"], ShouldEqual, true)
70+
})
71+
Convey("test condition <=", t, func() {
72+
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", "<=", "123", true, false})
73+
So(err, ShouldBeNil)
74+
e := newEvent()
75+
e.SetExtension("test", 456)
76+
err = a.Execute(&context.EventContext{
77+
Event: e,
78+
})
79+
So(err, ShouldBeNil)
80+
So(e.Extensions()["test2"], ShouldEqual, false)
81+
})
82+
Convey("test condition <", t, func() {
83+
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", "<", "123", true, false})
84+
So(err, ShouldBeNil)
85+
e := newEvent()
86+
e.SetExtension("test", 456)
87+
err = a.Execute(&context.EventContext{
88+
Event: e,
89+
})
90+
So(err, ShouldBeNil)
91+
So(e.Extensions()["test2"], ShouldEqual, false)
92+
})
93+
Convey("test condition unDefine op invalid", t, func() {
94+
a, err := NewAction([]interface{}{newConditionIfAction().Name(), "$.test2", "$.test", "<==", "123", true, false})
95+
So(err, ShouldBeNil)
96+
e := newEvent()
97+
e.SetExtension("test", 456)
98+
err = a.Execute(&context.EventContext{
99+
Event: e,
100+
})
101+
So(err, ShouldNotBeNil)
102+
})
103+
}

internal/primitive/transform/action/regex_action.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ import (
1818
"regexp"
1919
"sync"
2020

21+
"github.com/linkall-labs/vanus/internal/primitive/transform/arg"
22+
"github.com/linkall-labs/vanus/internal/primitive/transform/common"
2123
"github.com/linkall-labs/vanus/internal/primitive/transform/context"
22-
23-
"github.com/linkall-labs/vanus/internal/primitive/transform/function"
24-
2524
"github.com/pkg/errors"
26-
27-
"github.com/linkall-labs/vanus/internal/primitive/transform/arg"
2825
)
2926

3027
type replaceWithRegexAction struct {
@@ -47,7 +44,7 @@ func newReplaceWithRegexAction() Action {
4744
func (a *replaceWithRegexAction) Init(args []arg.Arg) error {
4845
a.targetArg = args[0]
4946
a.args = args
50-
a.argTypes = []function.Type{function.String, function.String, function.String}
47+
a.argTypes = []common.Type{common.String, common.String, common.String}
5148
return nil
5249
}
5350

internal/primitive/transform/action/struct_action.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ package action
1717
import (
1818
"fmt"
1919

20-
"github.com/linkall-labs/vanus/internal/primitive/transform/context"
21-
2220
"github.com/linkall-labs/vanus/internal/primitive/transform/arg"
23-
"github.com/linkall-labs/vanus/internal/primitive/transform/function"
21+
"github.com/linkall-labs/vanus/internal/primitive/transform/common"
22+
"github.com/linkall-labs/vanus/internal/primitive/transform/context"
2423
)
2524

2625
// ["delete", "key"].
@@ -63,7 +62,7 @@ func newCreateActionAction() Action {
6362
func (a *createAction) Init(args []arg.Arg) error {
6463
a.targetArg = args[0]
6564
a.args = args[1:]
66-
a.argTypes = []function.Type{function.Any}
65+
a.argTypes = []common.Type{common.Any}
6766
return nil
6867
}
6968

@@ -96,7 +95,7 @@ func newReplaceAction() Action {
9695
func (a *replaceAction) Init(args []arg.Arg) error {
9796
a.targetArg = args[0]
9897
a.args = args[1:]
99-
a.argTypes = []function.Type{function.Any}
98+
a.argTypes = []common.Type{common.Any}
10099
return nil
101100
}
102101

@@ -129,7 +128,7 @@ func newMoveActionAction() Action {
129128
func (a *moveAction) Init(args []arg.Arg) error {
130129
a.targetArg = args[1]
131130
a.args = args[:1]
132-
a.argTypes = []function.Type{function.Any}
131+
a.argTypes = []common.Type{common.Any}
133132
return nil
134133
}
135134

@@ -166,7 +165,7 @@ func newRenameActionAction() Action {
166165
func (a *renameAction) Init(args []arg.Arg) error {
167166
a.targetArg = args[1]
168167
a.args = args[:1]
169-
a.argTypes = []function.Type{function.Any}
168+
a.argTypes = []common.Type{common.Any}
170169
return nil
171170
}
172171

internal/primitive/transform/function/cast.go renamed to internal/primitive/transform/common/cast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package function
15+
package common
1616

1717
import (
1818
"fmt"

internal/primitive/transform/function/types.go renamed to internal/primitive/transform/common/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package function
15+
package common
1616

1717
type Type uint8
1818

internal/primitive/transform/function/format_functions.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import (
1818
"time"
1919

2020
"github.com/linkall-labs/vanus/internal/primitive/transform/function/util"
21+
22+
"github.com/linkall-labs/vanus/internal/primitive/transform/common"
2123
)
2224

2325
var DateFormatFunction = function{
2426
name: "DATE_FORMAT",
25-
fixedArgs: []Type{String, String},
26-
variadicArgs: TypePtr(String),
27+
fixedArgs: []common.Type{common.String, common.String},
28+
variadicArgs: common.TypePtr(common.String),
2729
fn: func(args []interface{}) (interface{}, error) {
2830
t, err := time.ParseInLocation(time.RFC3339, args[0].(string), time.UTC)
2931
if err != nil {
@@ -43,8 +45,8 @@ var DateFormatFunction = function{
4345

4446
var UnixTimeFormatFunction = function{
4547
name: "UNIX_TIME_FORMAT",
46-
fixedArgs: []Type{Number, String},
47-
variadicArgs: TypePtr(String),
48+
fixedArgs: []common.Type{common.Number, common.String},
49+
variadicArgs: common.TypePtr(common.String),
4850
fn: func(args []interface{}) (interface{}, error) {
4951
t := time.Unix(int64(args[0].(float64)), 0)
5052
loc := time.UTC

internal/primitive/transform/function/function.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
package function
1616

17+
import "github.com/linkall-labs/vanus/internal/primitive/transform/common"
18+
1719
type Function interface {
1820
// Name func name
1921
Name() string
2022
// Arity arg number
2123
Arity() int
2224
// ArgType arg type
23-
ArgType(index int) *Type
25+
ArgType(index int) *common.Type
2426
// IsVariadic is exist variadic
2527
IsVariadic() bool
2628
// Execute cal func result
@@ -29,8 +31,8 @@ type Function interface {
2931

3032
type function struct {
3133
name string
32-
fixedArgs []Type
33-
variadicArgs *Type
34+
fixedArgs []common.Type
35+
variadicArgs *common.Type
3436
fn func(args []interface{}) (interface{}, error)
3537
}
3638

@@ -42,7 +44,7 @@ func (f function) Arity() int {
4244
return len(f.fixedArgs)
4345
}
4446

45-
func (f function) ArgType(index int) *Type {
47+
func (f function) ArgType(index int) *common.Type {
4648
if index < len(f.fixedArgs) {
4749
return &f.fixedArgs[index]
4850
}

0 commit comments

Comments
 (0)