Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement function "today" #464

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions internal/primitive/transform/action/datetime/today.go
Original file line number Diff line number Diff line change
@@ -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"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the param path is target path ,it isn't an original path

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
}
75 changes: 75 additions & 0 deletions internal/primitive/transform/action/datetime/today_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
})
}
21 changes: 21 additions & 0 deletions internal/primitive/transform/function/datatime_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
5 changes: 5 additions & 0 deletions internal/primitive/transform/function/util/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
1 change: 1 addition & 0 deletions internal/primitive/transform/runtime/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func init() {
// datetime
datetime.NewDateFormatAction,
datetime.NewUnixTimeFormatAction,
datetime.NewTodayAction,
// string
strings.NewJoinAction,
strings.NewUpperAction,
Expand Down