-
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.
Signed-off-by: delu <[email protected]>
- Loading branch information
Showing
4 changed files
with
184 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,32 @@ | ||
// 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 common | ||
|
||
import ( | ||
"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/function" | ||
) | ||
|
||
// NewLengthAction ["length", targetPath, value]. | ||
func NewLengthAction() action.Action { | ||
a := &action.FunctionAction{} | ||
a.CommonAction = action.CommonAction{ | ||
ActionName: "LENGTH", | ||
FixedArgs: []arg.TypeList{arg.EventList, arg.All}, | ||
Fn: function.LengthFunction, | ||
} | ||
return a | ||
} |
110 changes: 110 additions & 0 deletions
110
internal/primitive/transform/action/common/length_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,110 @@ | ||
// 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 common_test | ||
|
||
import ( | ||
stdJson "encoding/json" | ||
"testing" | ||
|
||
cetest "github.com/cloudevents/sdk-go/v2/test" | ||
"github.com/linkall-labs/vanus/internal/primitive/transform/action/common" | ||
"github.com/linkall-labs/vanus/internal/primitive/transform/context" | ||
"github.com/linkall-labs/vanus/internal/primitive/transform/runtime" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestLengthAction(t *testing.T) { | ||
funcName := common.NewLengthAction().Name() | ||
Convey("test length", t, func() { | ||
jsonStr := `{ | ||
"array": ["123",4,true], | ||
"string": "string", | ||
"map":{ | ||
"key1":"value1", | ||
"key2":"value2" | ||
}, | ||
"number": 10, | ||
"bool": false | ||
}` | ||
Convey("test array", func() { | ||
a, err := runtime.NewAction([]interface{}{funcName, "$.data.length", "$.data.array"}) | ||
So(err, ShouldBeNil) | ||
e := cetest.MinEvent() | ||
var data map[string]interface{} | ||
err = stdJson.Unmarshal([]byte(jsonStr), &data) | ||
So(err, ShouldBeNil) | ||
err = a.Execute(&context.EventContext{ | ||
Event: &e, | ||
Data: data, | ||
}) | ||
So(err, ShouldBeNil) | ||
So(data["length"], ShouldEqual, 3) | ||
}) | ||
Convey("test string", func() { | ||
a, err := runtime.NewAction([]interface{}{funcName, "$.data.length", "$.data.string"}) | ||
So(err, ShouldBeNil) | ||
e := cetest.MinEvent() | ||
var data map[string]interface{} | ||
err = stdJson.Unmarshal([]byte(jsonStr), &data) | ||
So(err, ShouldBeNil) | ||
err = a.Execute(&context.EventContext{ | ||
Event: &e, | ||
Data: data, | ||
}) | ||
So(err, ShouldBeNil) | ||
So(data["length"], ShouldEqual, 6) | ||
}) | ||
Convey("test map", func() { | ||
a, err := runtime.NewAction([]interface{}{funcName, "$.data.length", "$.data.map"}) | ||
So(err, ShouldBeNil) | ||
e := cetest.MinEvent() | ||
var data map[string]interface{} | ||
err = stdJson.Unmarshal([]byte(jsonStr), &data) | ||
So(err, ShouldBeNil) | ||
err = a.Execute(&context.EventContext{ | ||
Event: &e, | ||
Data: data, | ||
}) | ||
So(err, ShouldBeNil) | ||
So(data["length"], ShouldEqual, 2) | ||
}) | ||
Convey("test bool", func() { | ||
a, err := runtime.NewAction([]interface{}{funcName, "$.data.length", "$.data.bool"}) | ||
So(err, ShouldBeNil) | ||
e := cetest.MinEvent() | ||
var data map[string]interface{} | ||
err = stdJson.Unmarshal([]byte(jsonStr), &data) | ||
So(err, ShouldBeNil) | ||
err = a.Execute(&context.EventContext{ | ||
Event: &e, | ||
Data: data, | ||
}) | ||
So(err, ShouldNotBeNil) | ||
}) | ||
Convey("test number", func() { | ||
a, err := runtime.NewAction([]interface{}{funcName, "$.data.length", "$.data.number"}) | ||
So(err, ShouldBeNil) | ||
e := cetest.MinEvent() | ||
var data map[string]interface{} | ||
err = stdJson.Unmarshal([]byte(jsonStr), &data) | ||
So(err, ShouldBeNil) | ||
err = a.Execute(&context.EventContext{ | ||
Event: &e, | ||
Data: data, | ||
}) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 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 function | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/linkall-labs/vanus/internal/primitive/transform/common" | ||
) | ||
|
||
var LengthFunction = function{ | ||
name: "length", | ||
fixedArgs: []common.Type{common.Any}, | ||
fn: func(args []interface{}) (interface{}, error) { | ||
switch v := args[0].(type) { | ||
case string: | ||
return len(v), nil | ||
case []interface{}: | ||
return len(v), nil | ||
case map[string]interface{}: | ||
return len(v), nil | ||
default: | ||
// maybe bool, float64, int32 | ||
return nil, fmt.Errorf("length not support %v", v) | ||
} | ||
}, | ||
} |
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