Skip to content

Commit

Permalink
feat: add function length
Browse files Browse the repository at this point in the history
Signed-off-by: delu <[email protected]>
  • Loading branch information
xdlbdy committed Dec 24, 2022
1 parent f4de8b1 commit 6d7884e
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
32 changes: 32 additions & 0 deletions internal/primitive/transform/action/common/length.go
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 internal/primitive/transform/action/common/length_test.go
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)
})
})
}
39 changes: 39 additions & 0 deletions internal/primitive/transform/function/common_function.go
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)
}
},
}
3 changes: 3 additions & 0 deletions internal/primitive/transform/runtime/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package runtime

import (
"github.com/linkall-labs/vanus/internal/primitive/transform/action/common"
"github.com/linkall-labs/vanus/internal/primitive/transform/action/condition"
"github.com/linkall-labs/vanus/internal/primitive/transform/action/datetime"
"github.com/linkall-labs/vanus/internal/primitive/transform/action/math"
Expand Down Expand Up @@ -50,6 +51,8 @@ func init() {
condition.NewConditionIfAction,
// render
render.NewRenderArrayAction,
// common
common.NewLengthAction,
} {
if err := AddAction(fn); err != nil {
panic(err)
Expand Down

0 comments on commit 6d7884e

Please sign in to comment.