Conversion

The Conversion registry includes a collection of functions designed to convert one data type to another directly within your templates. This allows for seamless type transformations.

You can easily import all the functions from the conversion registry by including the following import statement in your code

import "github.com/go-sprout/sprout/registry/conversion"

toBool

toBool converts a value from any types reasonably be converted to a boolean value. Using the cast package.

Signature

ToBool(v any) (bool, error)
{{ "true" | toBool }} // Output: true
{{ "t" | toBool }} // Output: true
{{ 1 | toBool }} // Output: true
{{ 0.0 | toBool }} // Output: false
{{ "invalid" | toBool }} // Error

toInt

toInt converts a value into an int. Using the cast package.

Signature

ToInt(v any) (int, error)
{{ "1" | toInt }} // Output: 1
{{ 1.1 | toInt }} // Output: 1
{{ true | toInt }} // Output: 1
{{ "invalid" | toInt }} // Error

toInt64

toInt64 converts a value into an int64. Using the cast package.

Signature

ToInt64(v any) (int64, error)
{{ "1" | toInt }} // Output: 1
{{ 1.1 | toInt }} // Output: 1
{{ true | toInt }} // Output: 1
{{ "invalid" | toInt }} // Error

toUint

toUint converts a value into a uint. Utilizes the cast package for conversion.

Signature

ToUint(v any) (uint, error)
{{ "1" | toUint }} // Output: 1
{{ 1.1 | toUint }} // Output: 1
{{ true | toUint }} // Output: 1
{{ "invalid" | toUint }} // Error

toUint64

toUint64 converts a value into a uint64. Utilizes the cast package for conversion.

Signature

ToUint64(v any) (uint64, error)
{{ "1" | toUint64 }} // Output: 1
{{ 1.1 | toUint64 }} // Output: 1
{{ true | toUint64 }} // Output: 1
{{ "invalid" | toUint64 }} // Error

toFloat64

toFloat64 converts a value into a float64. Utilizes the cast package for conversion.

Signature

ToFloat64(v any) (float64, error)
{{ "1" | toFloat64 }} // Output: 1
{{ 1.42 | toFloat64 }} // Output: 1.42
{{ true | toFloat64 }} // Output: 1
{{ "invalid" | toFloat64 }} // Error

toOctal

toOctal parses a value as an octal (base 8) integer.

Signature

ToOctal(v any) (int64, error)
{{ 777 | toOctal }} // Output: "511"
{{ "770" | toOctal }} // Output: "504"
{{ true | toOctal }} // Error
{{ "invalid" | toOctal }} // Error

toString

toString converts a value to a string, handling various types effectively.

Signature

ToString(v any) string
{{ 1 | toString }} // Output: "1"
{{ 1.42 | toString }} // Output: "1.42"
{{ true | toString }} // Output: "true"
{{ .UnknownOrNil | toString }} // Output: "<nil>"

Note: toString can handle various types as:

  • error and output err.Error()

  • fmt.Stringer and output o.String()

toDate

toDate converts a string to a time.Time object based on a format specification.

Signature

ToDate(layout string, value string) (time.Time, error)
{{ toDate "2006-01-02" "2024-05-10" }}
// Output: 2024-05-10 00:00:00 +0000 UTC

This example will takes the "2024-05-10" string and convert it with the layout "2006-01-02".

See more about Golang Layout on the official documentation.

toLocalDate

toLocalDate converts a string to a time.Time object based on a format specification and the local timezone.

Signature

ToLocalDate(layout, timezone, value string) (time.Time, error)
{{ "2024-09-17" | toLocalDate "2006-01-02" "Europe/Paris" }}
// Output: 2024-09-17 00:00:00 +0200 CEST
{{ "2024-09-17" | toLocalDate "2006-01-02" "MST" }}
// Output: 2024-09-17 00:00:00 -0700 MST
{{ "2024-09-17" | toLocalDate "2006-01-02" "invalid" }}
// Error

See more about Golang Layout on the official documentation.

toDuration

toDuration converts a value to a time.Duration. Taking a possibly signed sequence of decimal numbers, each optional fraction and a unit suffix, such 300ms, -1.5h or 2h45m.

Valid time units are ns, us (or µs), ms, s, m and h.

Signature

ToDuration(v any) (time.Duration, error)
{{ 1 | toDuration }} // Output: 1ns
{{ 1000.0 | toDuration }} // Output: 1µs
{{ "1m" | toDuration }} // Output: 1m0s
{{ "invalid" | toDuration }} // Error
{{ (toDuration "1h30m").Seconds }} // Output: 5400

Deprecated functions

atoi ⚠️

[DEPRECATED] Use toIntinstead.

{{ "42" | atoi }} // Output: 42

No error handling

int ⚠️

[DEPRECATED] Use toIntinstead.

{{ "42" | int }} // Output: 42

int64 ⚠️

[DEPRECATED] Use toInt64instead.

{{ "42" | int64 }} // Output: 42

float64 ⚠️

[DEPRECATED] Use toFloat64instead.

{{ "42.42" | float64 }} // Output: 42.42

Last updated