-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
- Loading branch information
Showing
5 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,4 +56,8 @@ | |
message?: string | ||
} | ||
|
||
#Steps: { | ||
... | ||
} | ||
NoExist: _|_ | ||
context: _ |