From 0d8c9590b407213bbbd6ebde557f25b998ceabf1 Mon Sep 17 00:00:00 2001 From: Avinash Bhardwaj Date: Sat, 25 Feb 2023 15:38:37 +0530 Subject: [PATCH 1/3] feat: add util time function that returns yyyy-mm-dd format --- internal/primitive/transform/function/util/time.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/primitive/transform/function/util/time.go b/internal/primitive/transform/function/util/time.go index 1092eb33d..8d779ad5c 100644 --- a/internal/primitive/transform/function/util/time.go +++ b/internal/primitive/transform/function/util/time.go @@ -46,3 +46,8 @@ func ConvertFormat2Go(format string) string { } return buffer.String() } + +// GetYMDFormat returns yyyy-mm-dd format. +func GetYMDFormat() string { + return ConvertFormat2Go("Y-m-d") +} From 37ee3c816dca7d213ce777e81f54d7a586ee6d3f Mon Sep 17 00:00:00 2001 From: Avinash Bhardwaj Date: Sat, 25 Feb 2023 15:57:22 +0530 Subject: [PATCH 2/3] feat: implement today function --- .../transform/action/datetime/today.go | 33 +++++++++++++++++++ .../transform/function/datatime_functions.go | 21 ++++++++++++ internal/primitive/transform/runtime/init.go | 1 + 3 files changed, 55 insertions(+) create mode 100644 internal/primitive/transform/action/datetime/today.go diff --git a/internal/primitive/transform/action/datetime/today.go b/internal/primitive/transform/action/datetime/today.go new file mode 100644 index 000000000..5cda75f7f --- /dev/null +++ b/internal/primitive/transform/action/datetime/today.go @@ -0,0 +1,33 @@ +// 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 datetime + +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" +) + +// NewTodayAction ["today", "path", "timeZone"] +func NewTodayAction() action.Action { + a := &action.SourceTargetSameAction{} + a.CommonAction = action.CommonAction{ + ActionName: "TODAY", + FixedArgs: []arg.TypeList{arg.EventList}, + VariadicArg: arg.All, + Fn: function.TodayFunction, + } + return a +} diff --git a/internal/primitive/transform/function/datatime_functions.go b/internal/primitive/transform/function/datatime_functions.go index 908befb39..5fdd9cf3a 100644 --- a/internal/primitive/transform/function/datatime_functions.go +++ b/internal/primitive/transform/function/datatime_functions.go @@ -60,3 +60,24 @@ var UnixTimeFormatFunction = function{ return t.In(loc).Format(format), nil }, } + +var TodayFunction = function{ + name: "TODAY", + fixedArgs: []common.Type{common.String}, + variadicArgs: common.TypePtr(common.String), + fn: func(args []interface{}) (interface{}, error) { + t, err := time.ParseInLocation(time.RFC3339, args[0].(string), time.UTC) + if err != nil { + return nil, err + } + loc := time.UTC + if len(args) > 1 && args[1].(string) != "" { + loc, err = time.LoadLocation(args[1].(string)) + if err != nil { + return nil, err + } + } + format := util.GetYMDFormat() + return t.In(loc).Format(format), nil + }, +} diff --git a/internal/primitive/transform/runtime/init.go b/internal/primitive/transform/runtime/init.go index ec19d0b05..add1c974b 100644 --- a/internal/primitive/transform/runtime/init.go +++ b/internal/primitive/transform/runtime/init.go @@ -42,6 +42,7 @@ func init() { // datetime datetime.NewDateFormatAction, datetime.NewUnixTimeFormatAction, + datetime.NewTodayAction, // string strings.NewJoinAction, strings.NewUpperAction, From b2ba1964030911cc54778198227700579ffaedf7 Mon Sep 17 00:00:00 2001 From: Avinash Bhardwaj Date: Sat, 25 Feb 2023 15:58:02 +0530 Subject: [PATCH 3/3] test: add unit tests for today function --- .../transform/action/datetime/today_test.go | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 internal/primitive/transform/action/datetime/today_test.go diff --git a/internal/primitive/transform/action/datetime/today_test.go b/internal/primitive/transform/action/datetime/today_test.go new file mode 100644 index 000000000..dd825ed22 --- /dev/null +++ b/internal/primitive/transform/action/datetime/today_test.go @@ -0,0 +1,75 @@ +// 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 datetime_test + +import ( + "testing" + + cetest "github.com/cloudevents/sdk-go/v2/test" + "github.com/linkall-labs/vanus/internal/primitive/transform/action/datetime" + "github.com/linkall-labs/vanus/internal/primitive/transform/context" + "github.com/linkall-labs/vanus/internal/primitive/transform/runtime" + . "github.com/smartystreets/goconvey/convey" +) + +func TestTodayAction(t *testing.T) { + funcName := datetime.NewTodayAction().Name() + Convey("test today date", t, func() { + Convey("test utc time with default time zone", func() { + e := cetest.MinEvent() + e.SetExtension("test", "2022-11-15T15:41:25Z") + a, err := runtime.NewAction([]interface{}{funcName, "$.test"}) + So(err, ShouldBeNil) + err = a.Execute(&context.EventContext{ + Event: &e, + }) + So(err, ShouldBeNil) + So(e.Extensions()["test"], ShouldEqual, "2022-11-15") + }) + Convey("test with time zone that has same date", func() { + e := cetest.MinEvent() + e.SetExtension("test", "2022-11-15T15:41:25Z") + a, err := runtime.NewAction([]interface{}{funcName, "$.test", "EST"}) + So(err, ShouldBeNil) + err = a.Execute(&context.EventContext{ + Event: &e, + }) + So(err, ShouldBeNil) + So(e.Extensions()["test"], ShouldEqual, "2022-11-15") + }) + Convey("test utc time with time zone that has the next date", func() { + e := cetest.MinEvent() + e.SetExtension("test", "2022-11-15T23:59:00Z") + a, err := runtime.NewAction([]interface{}{funcName, "$.test", "Asia/Kolkata"}) + So(err, ShouldBeNil) + err = a.Execute(&context.EventContext{ + Event: &e, + }) + So(err, ShouldBeNil) + So(e.Extensions()["test"], ShouldEqual, "2022-11-16") + }) + Convey("test utc time with time zone that has the previous date", func() { + e := cetest.MinEvent() + e.SetExtension("test", "2022-11-15T00:59:00Z") + a, err := runtime.NewAction([]interface{}{funcName, "$.test", "America/Chicago"}) + So(err, ShouldBeNil) + err = a.Execute(&context.EventContext{ + Event: &e, + }) + So(err, ShouldBeNil) + So(e.Extensions()["test"], ShouldEqual, "2022-11-14") + }) + }) +}