Skip to content

Commit

Permalink
feat: introduce toduration conversion function (#27)
Browse files Browse the repository at this point in the history
## Description
This pull request introduce the `toDuration` as proposed in #8 and
Masterminds#388 using the `cast`
package.

## Changes
- Add the function `toDuration`

## Fixes #8 

## Checklist
- [x] I have read the **CONTRIBUTING.md** document.
- [x] My code follows the code style of this project.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [ ] I have updated the documentation accordingly.
- [x] This change requires a change to the documentation on the website.
  • Loading branch information
42atomys committed May 9, 2024
1 parent f253487 commit a414bac
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
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

0 comments on commit a414bac

Please sign in to comment.