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. Using the cast package.

Signature

ToBool(v any) bool

Must version

{{ "true" | toBool }} // Output: true
{{ "t" | toBool }} // Output: true
{{ 1 | toBool }} // Output: true
{{ 0.0 | toBool }} // Output: false
{{ "invalid" | toBool }} // Output: false

toInt

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

Signature

ToInt(v any) int

Must version

{{ "1" | toInt }} // Output: 1
{{ 1.1 | toInt }} // Output: 1
{{ true | toInt }} // Output: 1
{{ "invalid" | toInt }} // Output: 0

toInt64

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

Signature

ToInt64(v any) int64

Must version

{{ "1" | toInt }} // Output: 1
{{ 1.1 | toInt }} // Output: 1
{{ true | toInt }} // Output: 1
{{ "invalid" | toInt }} // Output: 0

toUint

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

Signature

ToUint(v any) uint

Must version

{{ "1" | toUint }} // Output: 1
{{ 1.1 | toUint }} // Output: 1
{{ true | toUint }} // Output: 1
{{ "invalid" | toUint }} // Output: 0

toUint64

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

Signature

ToUint64(v any) uint64

Must version

{{ "1" | toUint64 }} // Output: 1
{{ 1.1 | toUint64 }} // Output: 1
{{ true | toUint64 }} // Output: 1
{{ "invalid" | toUint64 }} // Output: 0

toFloat64

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

Signature

ToFloat64(v any) float64

Must version

{{ "1" | toFloat64 }} // Output: 1
{{ 1.42 | toFloat64 }} // Output: 1.42
{{ true | toFloat64 }} // Output: 1
{{ "invalid" | toFloat64 }} // Output: 0

toOctal

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

Signature

ToOctal(v any) int64

Must version

{{ 777 | toOctal }} // Output: "511"
{{ "770" | toOctal }} // Output: "504"
{{ true | toOctal }} // Output: "1"
{{ "invalid" | toOctal }} // Output: "0"

toString

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

Signature

ToString(v any) string

Must version

{{ 1 | toString }} // Output: "1"
{{ 1.42 | toString }} // Output: "1.42"
{{ true | toString }} // Output: "true"
{{ nil | toString }} // Output: "<nil>"

Note: toString can handle various types as:

  • error and output err.Error()

  • fmt.Stringer and output o.String()

toDate / toMustDate

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

Signature

ToDate(layout, value string) time.Time
ToMustDate(layout string, value string) (time.Time, error)

Must version

{{ toDate "2006-01-02", "2024-05-10 11:12:42" }}
// Output: 2024-05-10 00:00:00 +0000 UTC

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

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

Must version

{{ 1 | toDuration }} // Output: 1ns
{{ (1000.0 * 1000.0) | toDuration }} // Output: 1ms
{{ "1m" | toDuration }} // Output: 1m
{{ "invalid" | toDuration }} // Output: 0s
{{ (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

#18: v0.5 loaders with handlers and registries

Change request updated