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: introduce toduration conversion function #27

Merged
merged 1 commit into from
May 9, 2024
Merged
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
17 changes: 17 additions & 0 deletions conversion_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ func (fh *FunctionHandler) ToDate(fmt, str string) time.Time {
return result
}

// ToDuration converts a value to a time.Duration.
//
// Parameters:
//
// v any - the value to convert to time.Duration. This value can be a string, int, or another compatible type.
//
// Returns:
//
// time.Duration - the duration representation of the value.
//
// Example:
//
// {{ (toDuration "1h30m").Seconds }} // Output: 5400
func (fh *FunctionHandler) ToDuration(v any) time.Duration {
return cast.ToDuration(v)
}

// MustToDate tries to parse a string into a time.Time object based on a format,
// returning an error if parsing fails.
//
Expand Down
13 changes: 13 additions & 0 deletions conversion_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ func TestToDate(t *testing.T) {
runTestCases(t, tests)
}

func TestToDuration(t *testing.T) {
var tests = testCases{
{"TestInt", `{{$v := toDuration .V }}{{typeOf $v}}-{{$v}}`, "time.Duration-1ns", map[string]any{"V": 1}},
{"TestInt32", `{{$v := toDuration .V }}{{typeOf $v}}-{{$v}}`, "time.Duration-1µs", map[string]any{"V": int32(1000)}},
{"TestFloat64", `{{$v := toDuration .V }}{{typeOf $v}}-{{$v}}`, "time.Duration-1.00042ms", map[string]any{"V": float64(1000 * 1000.42)}},
{"TestString", `{{$v := toDuration .V }}{{typeOf $v}}-{{$v}}`, "time.Duration-1m0s", map[string]any{"V": "1m"}},
{"TestInvalid", `{{$v := toDuration .V }}{{typeOf $v}}-{{$v}}`, "time.Duration-0s", map[string]any{"V": "aaaa"}},
{"TestCallingOnIt", `{{ (toDuration "1h30m").Seconds }}`, "5400", nil},
}

runTestCases(t, tests)
}

func TestMustToDate(t *testing.T) {
var tests = mustTestCases{
{testCase{"TestDate", `{{$v := mustToDate "2006-01-02" .V }}{{typeOf $v}}-{{$v}}`, "time.Time-2024-05-09 00:00:00 +0000 UTC", map[string]any{"V": "2024-05-09"}}, ""},
Expand Down
1 change: 1 addition & 0 deletions sprout.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func FuncMap(opts ...FunctionHandlerOption) template.FuncMap {
fnHandler.funcMap["toFloat64"] = fnHandler.ToFloat64
fnHandler.funcMap["seq"] = fnHandler.Seq
fnHandler.funcMap["toOctal"] = fnHandler.ToOctal
fnHandler.funcMap["toDuration"] = fnHandler.ToDuration
fnHandler.funcMap["split"] = fnHandler.Split
fnHandler.funcMap["splitList"] = fnHandler.SplitList
fnHandler.funcMap["splitn"] = fnHandler.Splitn
Expand Down
Loading