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 committed Apr 27, 2024
1 parent 0bc92e8 commit 45b2929
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 1 deletion.
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")

Check warning on line 44 in pkg/providers/legacy/time/time.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/legacy/time/time.go#L41-L44

Added lines #L41 - L44 were not covered by tests
}
if layout == "" {
layout = time.RFC3339

Check warning on line 47 in pkg/providers/legacy/time/time.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/legacy/time/time.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}
t, err := time.Parse(layout, date)
if err != nil {
return nil, err

Check warning on line 51 in pkg/providers/legacy/time/time.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/legacy/time/time.go#L49-L51

Added lines #L49 - L51 were not covered by tests
}
return &Vars{
Timestamp: t.Unix(),
}, nil

Check warning on line 55 in pkg/providers/legacy/time/time.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/legacy/time/time.go#L53-L55

Added lines #L53 - L55 were not covered by tests
}

// 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

Check warning on line 63 in pkg/providers/legacy/time/time.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/legacy/time/time.go#L60-L63

Added lines #L60 - L63 were not covered by tests
}
t := time.Unix(timestamp, 0)
return &Vars{
Date: t.UTC().Format(layout),
}, nil

Check warning on line 68 in pkg/providers/legacy/time/time.go

View check run for this annotation

Codecov / codecov/patch

pkg/providers/legacy/time/time.go#L65-L68

Added lines #L65 - L68 were not covered by tests
}

//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 45b2929

Please sign in to comment.