Skip to content

Commit

Permalink
add time provider
Browse files Browse the repository at this point in the history
Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
  • Loading branch information
FogDong authored and Somefive committed Jun 5, 2023
1 parent ced6c7e commit bb82087
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 14 deletions.
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@ require (
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/cel-go v0.12.6 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
Expand All @@ -91,7 +89,6 @@ require (
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/oam-dev/cluster-gateway v1.9.0-alpha.1 // indirect
github.com/onsi/ginkgo/v2 v2.9.2 // indirect
github.com/openshift/library-go v0.0.0-20230327085348-8477ec72b725 // indirect
github.com/pierrec/lz4 v2.6.0+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down Expand Up @@ -122,7 +119,6 @@ require (
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221202195650-67e5cbc046fd // indirect
google.golang.org/grpc v1.50.1 // indirect
Expand Down
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
Expand Down Expand Up @@ -165,7 +162,6 @@ github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-zookeeper/zk v1.0.2/go.mod h1:nOB03cncLtlp4t+UAkGSV+9beXP/akpekBwL+UX1Qcw=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down Expand Up @@ -223,7 +219,6 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian/v3 v3.2.1/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -271,7 +266,6 @@ github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOn
github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hudl/fargo v1.4.0/go.mod h1:9Ai6uvFy5fQNq6VPKtg+Ceq1+eTY4nKUlR2JElEOcDo=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
Expand Down Expand Up @@ -384,7 +378,6 @@ github.com/onsi/ginkgo v1.16.2/go.mod h1:CObGmKUOKaSC0RjmoAK7tKyn4Azo5P2IWuoMnvw
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
Expand Down Expand Up @@ -651,7 +644,6 @@ golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -719,7 +711,6 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
13 changes: 12 additions & 1 deletion pkg/providers/legacy/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/kubevela/workflow/pkg/providers/legacy/http"
"github.com/kubevela/workflow/pkg/providers/legacy/kube"
"github.com/kubevela/workflow/pkg/providers/legacy/metrics"
"github.com/kubevela/workflow/pkg/providers/legacy/time"
"github.com/kubevela/workflow/pkg/providers/legacy/util"
"github.com/kubevela/workflow/pkg/providers/legacy/workspace"
)
Expand All @@ -27,12 +28,22 @@ func GetLegacyProviders() map[string]cuexruntime.ProviderFn {
registerProviders(providers, http.GetProviders())
registerProviders(providers, kube.GetProviders())
registerProviders(providers, metrics.GetProviders())
registerProviders(providers, time.GetProviders())
registerProviders(providers, util.GetProviders())
registerProviders(providers, workspace.GetProviders())
return providers
}

// GetLegacyTemplate get legacy template
func GetLegacyTemplate() string {
return strings.Join([]string{email.GetTemplate(), http.GetTemplate(), kube.GetTemplate(), metrics.GetTemplate(), util.GetTemplate(), workspace.GetTemplate()}, "\n")
return strings.Join([]string{
email.GetTemplate(),
http.GetTemplate(),
kube.GetTemplate(),
metrics.GetTemplate(),
time.GetTemplate(),
util.GetTemplate(),
workspace.GetTemplate(),
},
"\n")
}
23 changes: 23 additions & 0 deletions pkg/providers/legacy/time/time.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// time.cue

#DateToTimestamp: {
#do: "timestamp"
#provider: "op"

date: string
layout: *"" | string

timestamp?: int64
...
}

#TimestampToDate: {
#do: "date"
#provider: "op"

timestamp: int64
layout: *"" | string

date?: string
...
}
85 changes: 85 additions & 0 deletions pkg/providers/legacy/time/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2021. The KubeVela Authors.
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 time

import (
"context"
_ "embed"
"fmt"
"time"

cuexruntime "github.com/kubevela/pkg/cue/cuex/runtime"
providertypes "github.com/kubevela/workflow/pkg/providers/types"
)

// Vars .
type Vars struct {
Date string `json:"date,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
Layout string `json:"layout,omitempty"`
}

// Params .
type Params = providertypes.LegacyParams[Vars]

// Timestamp convert date to timestamp
func Timestamp(ctx context.Context, params *Params) (*Vars, error) {
date := params.Params.Date
layout := params.Params.Layout
if date == "" {
return nil, fmt.Errorf("empty date to convert")
}
if layout == "" {
layout = time.RFC3339
}
t, err := time.Parse(layout, date)
if err != nil {
return nil, err
}
return &Vars{
Timestamp: t.Unix(),
}, nil
}

// Date convert timestamp to date
func Date(ctx context.Context, params *Params) (*Vars, error) {
timestamp := params.Params.Timestamp
layout := params.Params.Layout
if layout == "" {
layout = time.RFC3339
}
t := time.Unix(timestamp, 0)
return &Vars{
Date: t.UTC().Format(layout),
}, nil
}

//go:embed time.cue
var template string

// GetTemplate return the template
func GetTemplate() string {
return template
}

// GetProviders return the provider
func GetProviders() map[string]cuexruntime.ProviderFn {
return map[string]cuexruntime.ProviderFn{
"timestamp": providertypes.LegacyGenericProviderFn[Vars, Vars](Timestamp),
"date": providertypes.LegacyGenericProviderFn[Vars, Vars](Date),
}
}
131 changes: 131 additions & 0 deletions pkg/providers/legacy/time/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
Copyright 2021. The KubeVela Authors.
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 time

import (
"context"
"fmt"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

func TestTimestamp(t *testing.T) {
ctx := context.Background()
testcases := map[string]struct {
from Vars
expected int64
expectedErr error
}{
"test convert date with default time layout": {
from: Vars{
Date: "2021-11-07T01:47:51Z",
},
expected: 1636249671,
expectedErr: nil,
},
"test convert date with RFC3339 layout": {
from: Vars{
Date: "2021-11-07T01:47:51Z",
Layout: "2006-01-02T15:04:05Z07:00",
},
expected: 1636249671,
expectedErr: nil,
},
"test convert date with RFC1123 layout": {
from: Vars{
Date: "Fri, 01 Mar 2019 15:00:00 GMT",
Layout: "Mon, 02 Jan 2006 15:04:05 MST",
},
expected: 1551452400,
expectedErr: nil,
},
"test convert without date": {
from: Vars{},
expected: 0,
expectedErr: fmt.Errorf("empty date to convert"),
},
"test convert date with wrong time layout": {
from: Vars{
Date: "2021-11-07T01:47:51Z",
Layout: "Mon, 02 Jan 2006 15:04:05 MST",
},
expected: 0,
expectedErr: errors.New(`parsing time "2021-11-07T01:47:51Z" as "Mon, 02 Jan 2006 15:04:05 MST": cannot parse "2021-11-07T01:47:51Z" as "Mon"`),
},
}

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
r := require.New(t)
res, err := Timestamp(ctx, &Params{
Params: tc.from,
})
if tc.expectedErr != nil {
r.Equal(tc.expectedErr.Error(), err.Error())
return
}
r.NoError(err)
r.Equal(tc.expected, res.Timestamp)
})
}
}

func TestDate(t *testing.T) {
ctx := context.Background()
testcases := map[string]struct {
from Vars
expected string
}{
"test convert timestamp to default time layout": {
from: Vars{
Timestamp: 1636249671,
},
expected: "2021-11-07T01:47:51Z",
},
"test convert date to RFC3339 layout": {
from: Vars{
Timestamp: 1636249671,
Layout: "2006-01-02T15:04:05Z07:00",
},
expected: "2021-11-07T01:47:51Z",
},
"test convert date to RFC1123 layout": {
from: Vars{
Timestamp: 1551452400,
Layout: "Mon, 02 Jan 2006 15:04:05 MST",
},
expected: "Fri, 01 Mar 2019 15:00:00 UTC",
},
"test convert without timestamp": {
from: Vars{},
expected: "1970-01-01T00:00:00Z",
},
}

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
r := require.New(t)
res, err := Date(ctx, &Params{
Params: tc.from,
})
r.NoError(err)
r.Equal(tc.expected, res.Date)
})
}
}
4 changes: 4 additions & 0 deletions pkg/providers/legacy/workspace/workspace.cue
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@
message?: string
}

#Steps: {
...
}
NoExist: _|_
context: _

0 comments on commit bb82087

Please sign in to comment.