diff --git a/internal/primitive/transform/action/render/array_test.go b/internal/primitive/transform/action/render/array_test.go index cce109cac..bf6ba7c3f 100644 --- a/internal/primitive/transform/action/render/array_test.go +++ b/internal/primitive/transform/action/render/array_test.go @@ -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") + }) + }) } diff --git a/internal/primitive/transform/common/cast.go b/internal/primitive/transform/common/cast.go index c8301f85c..89f1b0973 100644 --- a/internal/primitive/transform/common/cast.go +++ b/internal/primitive/transform/common/cast.go @@ -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 } @@ -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 diff --git a/internal/trigger/transform/transformer.go b/internal/trigger/transform/transformer.go index d365db9a3..59f63447f 100644 --- a/internal/trigger/transform/transformer.go +++ b/internal/trigger/transform/transformer.go @@ -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 { @@ -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 }