Skip to content

Commit

Permalink
feat: add tobool, touint and touint64 functions to conversions group
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed May 10, 2024
1 parent edab50f commit bb2639b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
51 changes: 51 additions & 0 deletions conversion_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
38 changes: 38 additions & 0 deletions conversion_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
Expand All @@ -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}},
Expand Down
5 changes: 4 additions & 1 deletion sprout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bb2639b

Please sign in to comment.