-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: complete conversion category migration 🌱✨
- Loading branch information
Showing
9 changed files
with
257 additions
and
200 deletions.
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
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,149 @@ | ||
package sprout | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/spf13/cast" | ||
) | ||
|
||
// ToInt converts a value to an int using robust type casting. | ||
// | ||
// Parameters: | ||
// | ||
// v any - the value to convert to an int. | ||
// | ||
// Returns: | ||
// | ||
// int - the integer representation of the value. | ||
// | ||
// Example: | ||
// | ||
// {{ "123" | toInt }} // Output: 123 | ||
func (fh *FunctionHandler) ToInt(v any) int { | ||
return cast.ToInt(v) | ||
} | ||
|
||
// ToInt64 converts a value to an int64, accommodating larger integer values. | ||
// | ||
// Parameters: | ||
// | ||
// v any - the value to convert to an int64. | ||
// | ||
// Returns: | ||
// | ||
// int64 - the int64 representation of the value. | ||
// | ||
// Example: | ||
// | ||
// {{ "123456789012" | toInt64 }} // Output: 123456789012 | ||
func (fh *FunctionHandler) ToInt64(v any) int64 { | ||
return cast.ToInt64(v) | ||
} | ||
|
||
// ToFloat64 converts a value to a float64. | ||
// | ||
// Parameters: | ||
// | ||
// v any - the value to convert to a float64. | ||
// | ||
// Returns: | ||
// | ||
// float64 - the float64 representation of the value. | ||
// | ||
// Example: | ||
// | ||
// {{ "123.456" | toFloat64 }} // Output: 123.456 | ||
func (fh *FunctionHandler) ToFloat64(v any) float64 { | ||
return cast.ToFloat64(v) | ||
} | ||
|
||
// ToOctal parses a string value as an octal (base 8) integer. | ||
// | ||
// Parameters: | ||
// | ||
// v any - the string representing an octal number. | ||
// | ||
// Returns: | ||
// | ||
// int64 - the decimal (base 10) representation of the octal value. | ||
// If parsing fails, returns 0. | ||
// | ||
// Example: | ||
// | ||
// {{ "123" | toOctal }} // Output: 83 (since "123" in octal is 83 in decimal) | ||
func (fh *FunctionHandler) ToOctal(v any) int64 { | ||
result, err := strconv.ParseInt(fmt.Sprint(v), 8, 64) | ||
if err != nil { | ||
return 0 | ||
} | ||
return result | ||
} | ||
|
||
// ToString converts a value to a string, handling various types effectively. | ||
// | ||
// Parameters: | ||
// | ||
// v any - the value to convert to a string. | ||
// | ||
// Returns: | ||
// | ||
// string - the string representation of the value. | ||
// | ||
// Example: | ||
// | ||
// {{ 123 | toString }} // Output: "123" | ||
func (fh *FunctionHandler) ToString(v any) string { | ||
switch v := v.(type) { | ||
case string: | ||
return v | ||
case []byte: | ||
return string(v) | ||
case error: | ||
return v.Error() | ||
case fmt.Stringer: | ||
return v.String() | ||
default: | ||
return fmt.Sprintf("%v", v) | ||
} | ||
} | ||
|
||
// ToDate converts a string to a time.Time object based on a format specification. | ||
// | ||
// Parameters: | ||
// | ||
// fmt string - the date format string. | ||
// str string - the date string to parse. | ||
// | ||
// Returns: | ||
// | ||
// time.Time - the parsed date. | ||
// | ||
// Example: | ||
// | ||
// {{ "2006-01-02", "2023-05-04" | toDate }} // Output: 2023-05-04 00:00:00 +0000 UTC | ||
func (fh *FunctionHandler) ToDate(fmt, str string) time.Time { | ||
result, _ := fh.MustToDate(fmt, str) | ||
return result | ||
} | ||
|
||
// MustToDate tries to parse a string into a time.Time object based on a format, | ||
// returning an error if parsing fails. | ||
// | ||
// Parameters: | ||
// | ||
// fmt string - the date format string. | ||
// str string - the date string to parse. | ||
// | ||
// Returns: | ||
// | ||
// time.Time - the parsed date. | ||
// error - error if the date string does not conform to the format. | ||
// | ||
// Example: | ||
// | ||
// {{ "2006-01-02", "2023-05-04" | mustToDate }} // Output: 2023-05-04 00:00:00 +0000 UTC, nil | ||
func (fh *FunctionHandler) MustToDate(fmt, str string) (time.Time, error) { | ||
return time.ParseInLocation(fmt, str, time.Local) | ||
} |
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,92 @@ | ||
package sprout | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestToInt(t *testing.T) { | ||
var tests = testCases{ | ||
{"TestInt", `{{$v := toInt .V }}{{kindOf $v}}-{{$v}}`, "int-1", map[string]any{"V": 1}}, | ||
{"TestInt32", `{{$v := toInt .V }}{{kindOf $v}}-{{$v}}`, "int-1", map[string]any{"V": int32(1)}}, | ||
{"TestFloat64", `{{$v := toInt .V }}{{kindOf $v}}-{{$v}}`, "int-1", map[string]any{"V": float64(1.42)}}, | ||
{"TestBool", `{{$v := toInt .V }}{{kindOf $v}}-{{$v}}`, "int-1", map[string]any{"V": true}}, | ||
{"TestString", `{{$v := toInt .V }}{{kindOf $v}}-{{$v}}`, "int-1", map[string]any{"V": "1"}}, | ||
} | ||
|
||
runTestCases(t, tests) | ||
} | ||
|
||
func TestToInt64(t *testing.T) { | ||
var tests = testCases{ | ||
{"TestInt", `{{$v := toInt64 .V }}{{typeOf $v}}-{{$v}}`, "int64-1", map[string]any{"V": 1}}, | ||
{"TestInt32", `{{$v := toInt64 .V }}{{typeOf $v}}-{{$v}}`, "int64-1", map[string]any{"V": int32(1)}}, | ||
{"TestFloat64", `{{$v := toInt64 .V }}{{typeOf $v}}-{{$v}}`, "int64-1", map[string]any{"V": float64(1.42)}}, | ||
{"TestBool", `{{$v := toInt64 .V }}{{typeOf $v}}-{{$v}}`, "int64-1", map[string]any{"V": true}}, | ||
{"TestString", `{{$v := toInt64 .V }}{{typeOf $v}}-{{$v}}`, "int64-1", map[string]any{"V": "1"}}, | ||
} | ||
|
||
runTestCases(t, tests) | ||
} | ||
|
||
func TestToFloat64(t *testing.T) { | ||
var tests = testCases{ | ||
{"TestInt", `{{$v := toFloat64 .V }}{{typeOf $v}}-{{$v}}`, "float64-1", map[string]any{"V": 1}}, | ||
{"TestInt32", `{{$v := toFloat64 .V }}{{typeOf $v}}-{{$v}}`, "float64-1", map[string]any{"V": int32(1)}}, | ||
{"TestFloat64", `{{$v := toFloat64 .V }}{{typeOf $v}}-{{$v}}`, "float64-1.42", map[string]any{"V": float64(1.42)}}, | ||
{"TestBool", `{{$v := toFloat64 .V }}{{typeOf $v}}-{{$v}}`, "float64-1", map[string]any{"V": true}}, | ||
{"TestString", `{{$v := toFloat64 .V }}{{typeOf $v}}-{{$v}}`, "float64-1", map[string]any{"V": "1"}}, | ||
} | ||
|
||
runTestCases(t, tests) | ||
} | ||
|
||
func TestToDecimal(t *testing.T) { | ||
var tests = testCases{ | ||
{"TestInt", `{{$v := toDecimal .V }}{{typeOf $v}}-{{$v}}`, "int64-511", map[string]any{"V": 777}}, | ||
{"TestInt32", `{{$v := toDecimal .V }}{{typeOf $v}}-{{$v}}`, "int64-504", map[string]any{"V": int32(770)}}, | ||
{"TestString", `{{$v := toDecimal .V }}{{typeOf $v}}-{{$v}}`, "int64-1", map[string]any{"V": "1"}}, | ||
} | ||
|
||
runTestCases(t, tests) | ||
} | ||
|
||
type testStringer struct{} | ||
|
||
func (s testStringer) String() string { | ||
return "stringer" | ||
} | ||
|
||
func TestToString(t *testing.T) { | ||
|
||
var tests = testCases{ | ||
{"TestInt", `{{$v := toString .V }}{{typeOf $v}}-{{$v}}`, "string-1", map[string]any{"V": 1}}, | ||
{"TestInt32", `{{$v := toString .V }}{{typeOf $v}}-{{$v}}`, "string-1", map[string]any{"V": int32(1)}}, | ||
{"TestFloat64", `{{$v := toString .V }}{{typeOf $v}}-{{$v}}`, "string-1.42", map[string]any{"V": float64(1.42)}}, | ||
{"TestBool", `{{$v := toString .V }}{{typeOf $v}}-{{$v}}`, "string-true", map[string]any{"V": true}}, | ||
{"TestString", `{{$v := toString .V }}{{typeOf $v}}-{{$v}}`, "string-1", map[string]any{"V": "1"}}, | ||
{"TestError", `{{$v := toString .V }}{{typeOf $v}}-{{$v}}`, "string-error", map[string]any{"V": fmt.Errorf("error")}}, | ||
{"TestStringer", `{{$v := toString .V }}{{typeOf $v}}-{{$v}}`, "string-stringer", map[string]any{"V": testStringer{}}}, | ||
{"TestSliceOfBytes", `{{$v := toString .V }}{{typeOf $v}}-{{$v}}`, "string-abc", map[string]any{"V": []byte("abc")}}, | ||
} | ||
|
||
runTestCases(t, tests) | ||
} | ||
|
||
func TestToDate(t *testing.T) { | ||
var tests = testCases{ | ||
{"TestDate", `{{$v := toDate "2006-01-02" .V }}{{typeOf $v}}-{{$v}}`, "time.Time-2024-05-09 00:00:00 +0000 UTC", map[string]any{"V": "2024-05-09"}}, | ||
} | ||
|
||
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"}}, ""}, | ||
{testCase{"TestInvalidValue", `{{$v := mustToDate "2006-01-02" .V }}{{typeOf $v}}-{{$v}}`, "", map[string]any{"V": ""}}, "cannot parse \"\" as \"2006\""}, | ||
{testCase{"TestInvalidLayout", `{{$v := mustToDate "invalid" .V }}{{typeOf $v}}-{{$v}}`, "", map[string]any{"V": "2024-05-09"}}, "cannot parse \"2024-05-09\" as \"invalid\""}, | ||
} | ||
|
||
runMustTestCases(t, tests) | ||
} |
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
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
Oops, something went wrong.