Skip to content

Commit

Permalink
fix: fix transformer convert array (#368)
Browse files Browse the repository at this point in the history
* fix: transformer data type not array convert to array

Signed-off-by: xdlbdy <[email protected]>

* feat: transformer add panic conver

Signed-off-by: xdlbdy <[email protected]>

Signed-off-by: xdlbdy <[email protected]>
  • Loading branch information
xdlbdy authored Dec 26, 2022
1 parent f578245 commit 41b10a8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
27 changes: 27 additions & 0 deletions internal/primitive/transform/action/render/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,31 @@ func TestRenderArrayAction(t *testing.T) {
})
})
})
Convey("test array length 1", t, func() {
jsonStr := `{
"array": [
{
"name": "name1",
"number": 1
}
]
}`
Convey("render other", func() {
a, err := runtime.NewAction([]interface{}{funcName, "$.data.render", "$.data.array", "Name: <@.name> Num: <@.number> <@abc"})
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)
render, exist := data["render"]
So(exist, ShouldBeTrue)
So(len(render.([]string)), ShouldEqual, 1)
So(render.([]string)[0], ShouldEqual, "Name: name1 Num: 1 <@abc")
})
})
}
11 changes: 9 additions & 2 deletions internal/primitive/transform/common/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ func Cast(val interface{}, target Type) (interface{}, error) {
case String:
switch value := val.(type) {
case int32: // ce attribute
return strconv.Itoa(int(value)), nil
return strconv.FormatInt(int64(value), 10), nil
case int64: // ce attribute
return strconv.FormatInt(value, 10), nil
case float64: // ce data json marshal
return strconv.FormatFloat(value, 'f', -1, 64), nil
return fmt.Sprintf("%v", val), nil
case bool:
return strconv.FormatBool(value), nil
}
Expand Down Expand Up @@ -77,6 +79,11 @@ func Cast(val interface{}, target Type) (interface{}, error) {
}
return stringArr, nil
}
case Array:
switch value := val.(type) {
case string, int32, int64, float64, bool:
return []interface{}{value}, nil
}
}

// AnyType doesn't need casting
Expand Down
18 changes: 14 additions & 4 deletions internal/trigger/transform/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ package transform

import (
"encoding/json"

"github.com/linkall-labs/vanus/internal/primitive/transform/context"
"runtime"

ce "github.com/cloudevents/sdk-go/v2"
"github.com/linkall-labs/vanus/internal/primitive"
"github.com/linkall-labs/vanus/internal/primitive/transform/context"
"github.com/linkall-labs/vanus/internal/trigger/transform/define"
"github.com/linkall-labs/vanus/internal/trigger/transform/pipeline"
"github.com/linkall-labs/vanus/internal/trigger/transform/template"
"github.com/pkg/errors"
)

type Transformer struct {
Expand All @@ -47,9 +48,18 @@ func NewTransformer(transformer *primitive.Transformer) *Transformer {
return tf
}

func (tf *Transformer) Execute(event *ce.Event) error {
func (tf *Transformer) Execute(event *ce.Event) (err error) {
defer func() {
if r := recover(); r != nil {
size := 1024
stacktrace := make([]byte, size)
stacktrace = stacktrace[:runtime.Stack(stacktrace, false)]
err = errors.New(string(stacktrace))
}
}()

var data interface{}
err := json.Unmarshal(event.Data(), &data)
err = json.Unmarshal(event.Data(), &data)
if err != nil {
return err
}
Expand Down

0 comments on commit 41b10a8

Please sign in to comment.