diff --git a/conversion_functions.go b/conversion_functions.go index 4e0dc2e..1979645 100644 --- a/conversion_functions.go +++ b/conversion_functions.go @@ -8,6 +8,23 @@ import ( "github.com/spf13/cast" ) +// ToBool converts a value to a boolean. +// +// Parameters: +// +// v any - the value to convert to a boolean. This can be any types reasonably be converted to true or false. +// +// Returns: +// +// bool - the boolean representation of the value. +// +// Example: +// +// {{ "true" | toBool }} // Output: true +func (fh *FunctionHandler) ToBool(v any) bool { + return cast.ToBool(v) +} + // ToInt converts a value to an int using robust type casting. // // Parameters: @@ -42,6 +59,40 @@ func (fh *FunctionHandler) ToInt64(v any) int64 { return cast.ToInt64(v) } +// ToUint converts a value to a uint. +// +// Parameters: +// +// v any - the value to convert to uint. This value can be of any type that is numerically convertible. +// +// Returns: +// +// uint - the uint representation of the value. +// +// Example: +// +// {{ "123" | toUint }} // Output: 123 +func (fh *FunctionHandler) ToUint(v any) uint { + return cast.ToUint(v) +} + +// ToUint64 converts a value to a uint64. +// +// Parameters: +// +// v any - the value to convert to uint64. This value can be of any type that is numerically convertible. +// +// Returns: +// +// uint64 - the uint64 representation of the value. +// +// Example: +// +// {{ "123456789012345" | toUint64 }} // Output: 123456789012345 +func (fh *FunctionHandler) ToUint64(v any) uint64 { + return cast.ToUint64(v) +} + // ToFloat64 converts a value to a float64. // // Parameters: diff --git a/conversion_functions_test.go b/conversion_functions_test.go index 6f7dbe2..bb3ea7e 100644 --- a/conversion_functions_test.go +++ b/conversion_functions_test.go @@ -5,6 +5,20 @@ import ( "testing" ) +func TestToBool(t *testing.T) { + var tests = testCases{ + {"TestBool", `{{$v := toBool .V }}{{kindOf $v}}-{{$v}}`, "bool-true", map[string]any{"V": true}}, + {"TestInt", `{{$v := toBool .V }}{{kindOf $v}}-{{$v}}`, "bool-true", map[string]any{"V": 1}}, + {"TestInt32", `{{$v := toBool .V }}{{kindOf $v}}-{{$v}}`, "bool-true", map[string]any{"V": int32(1)}}, + {"TestFloat64", `{{$v := toBool .V }}{{kindOf $v}}-{{$v}}`, "bool-true", map[string]any{"V": float64(1.42)}}, + {"TestString", `{{$v := toBool .V }}{{kindOf $v}}-{{$v}}`, "bool-true", map[string]any{"V": "true"}}, + {"TestStringFalse", `{{$v := toBool .V }}{{kindOf $v}}-{{$v}}`, "bool-false", map[string]any{"V": "false"}}, + {"TestStringInvalid", `{{$v := toBool .V }}{{kindOf $v}}-{{$v}}`, "bool-false", map[string]any{"V": "invalid"}}, + } + + runTestCases(t, tests) +} + func TestToInt(t *testing.T) { var tests = testCases{ {"TestInt", `{{$v := toInt .V }}{{kindOf $v}}-{{$v}}`, "int-1", map[string]any{"V": 1}}, @@ -29,6 +43,30 @@ func TestToInt64(t *testing.T) { runTestCases(t, tests) } +func TestToUint(t *testing.T) { + var tests = testCases{ + {"TestInt", `{{$v := toUint .V }}{{kindOf $v}}-{{$v}}`, "uint-1", map[string]any{"V": 1}}, + {"TestInt32", `{{$v := toUint .V }}{{kindOf $v}}-{{$v}}`, "uint-1", map[string]any{"V": int32(1)}}, + {"TestFloat64", `{{$v := toUint .V }}{{kindOf $v}}-{{$v}}`, "uint-1", map[string]any{"V": float64(1.42)}}, + {"TestBool", `{{$v := toUint .V }}{{kindOf $v}}-{{$v}}`, "uint-1", map[string]any{"V": true}}, + {"TestString", `{{$v := toUint .V }}{{kindOf $v}}-{{$v}}`, "uint-1", map[string]any{"V": "1"}}, + } + + runTestCases(t, tests) +} + +func TestToUint64(t *testing.T) { + var tests = testCases{ + {"TestInt", `{{$v := toUint64 .V }}{{typeOf $v}}-{{$v}}`, "uint64-1", map[string]any{"V": 1}}, + {"TestInt32", `{{$v := toUint64 .V }}{{typeOf $v}}-{{$v}}`, "uint64-1", map[string]any{"V": int32(1)}}, + {"TestFloat64", `{{$v := toUint64 .V }}{{typeOf $v}}-{{$v}}`, "uint64-1", map[string]any{"V": float64(1.42)}}, + {"TestBool", `{{$v := toUint64 .V }}{{typeOf $v}}-{{$v}}`, "uint64-1", map[string]any{"V": true}}, + {"TestString", `{{$v := toUint64 .V }}{{typeOf $v}}-{{$v}}`, "uint64-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}}, diff --git a/sprout.go b/sprout.go index ecd7a2d..2e7feec 100644 --- a/sprout.go +++ b/sprout.go @@ -149,9 +149,12 @@ func FuncMap(opts ...FunctionHandlerOption) template.FuncMap { fnHandler.funcMap["sha256sum"] = fnHandler.Sha256sum fnHandler.funcMap["adler32sum"] = fnHandler.Adler32sum fnHandler.funcMap["toString"] = fnHandler.ToString - fnHandler.funcMap["toInt64"] = fnHandler.ToInt64 fnHandler.funcMap["toInt"] = fnHandler.ToInt + fnHandler.funcMap["toInt64"] = fnHandler.ToInt64 + fnHandler.funcMap["toUint"] = fnHandler.ToUint + fnHandler.funcMap["toUint64"] = fnHandler.ToUint64 fnHandler.funcMap["toFloat64"] = fnHandler.ToFloat64 + fnHandler.funcMap["toBool"] = fnHandler.ToBool fnHandler.funcMap["seq"] = fnHandler.Seq fnHandler.funcMap["toOctal"] = fnHandler.ToOctal fnHandler.funcMap["toDuration"] = fnHandler.ToDuration