conv

package
v0.0.0-...-202847b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2023 License: Apache-2.0, MIT Imports: 10 Imported by: 10

README

cast

Easy and safe casting from one type to another in Go

Don’t Panic! ... Cast.

Optimized version for CoreStore.

What is Cast?

Cast is a library to convert between different go types in a consistent and easy way.

Cast provides simple functions to easily convert a number to a string, an interface into a bool, etc. Cast does this intelligently when an obvious conversion is possible. It doesn’t make any attempts to guess what you meant, for example you can only convert a string to an int when it is a string representation of an int such as “8”. Cast was developed for use in Hugo, a website engine which uses YAML, TOML or JSON for meta data.

Why use Cast?

When working with dynamic data in Go you often need to cast or convert the data from one type into another. Cast goes beyond just using type assertion (though it uses that when possible) to provide a very straightforward and convenient library.

If you are working with interfaces to handle things like dynamic content you’ll need an easy way to convert an interface into a given type. This is the library for you.

If you are taking in data from YAML, TOML or JSON or other formats which lack full types, then Cast is the library for you.

Usage

Cast provides a handful of To_____ methods. These methods will always return the desired type. If input is provided that will not convert to that type, the 0 or nil value for that type will be returned.

Cast also provides identical methods To_____E. These return the same result as the To_____ methods, plus an additional error which tells you if it successfully converted. Using these methods you can tell the difference between when the input matched the zero value or when the conversion failed and the zero value was returned.

The following examples are merely a sample of what is available. Please review the code for a complete set.

Example ‘ToString’:
cast.ToString("mayonegg")         // "mayonegg"
cast.ToString(8)                  // "8"
cast.ToString(8.31)               // "8.31"
cast.ToString([]byte("one time")) // "one time"
cast.ToString(nil)                // ""

var foo interface{} = "one more time"
cast.ToString(foo)                // "one more time"
Example ‘ToInt’:
cast.ToInt(8)                  // 8
cast.ToInt(8.31)               // 8
cast.ToInt("8")                // 8
cast.ToInt(true)               // 1
cast.ToInt(false)              // 0

var eight interface{} = 8
cast.ToInt(eight)              // 8
cast.ToInt(nil)                // 0

Documentation

Overview

Package conv - easy and safe casting from one type to another.

CoreStore specific types have been additionally implemented for example in function ToStringE().

Functions ending with ...E() return an error which has always the bahaviour of not being valid.

Index

Constants

This section is empty.

Variables

View Source
var TimeFormats = [...]string{
	time.RFC3339,
	"2006-01-02T15:04:05",
	"2006-01-02 15:04:05",
	time.RFC1123Z,
	time.RFC1123,
	time.RFC822Z,
	time.RFC822,
	time.ANSIC,
	time.UnixDate,
	time.RubyDate,
	"2006-01-02 15:04:05Z07:00",
	"02 Jan 06 15:04 MST",
	"2006-01-02",
	"02 Jan 2006",
	"2006-01-02 15:04:05 -07:00",
	"2006-01-02 15:04:05 -0700",
}

TimeFormats available time format to parse

Functions

func StringToDate

func StringToDate(s string, loc *time.Location) (time.Time, error)

StringToDate casts an empty interface to a time.Time. Location can be nil, then time.Local is the default value.

func ToBool

func ToBool(i interface{}) bool

func ToBoolE

func ToBoolE(i interface{}) (bool, error)

ToBoolE casts an empty interface to a bool. If a type implements function

ToBool() bool

this function will get called.

func ToByte

func ToByte(i interface{}) []byte

func ToByteE

func ToByteE(i interface{}) ([]byte, error)

ToByteE casts an empty interface to a byte slice. Faster than ToStringE because ToByteE avoids some copying of data. Use wisely.

func ToDataSlice

func ToDataSlice(i interface{}) [][]string

func ToDataSliceE

func ToDataSliceE(i interface{}) ([][]string, error)

ToDataSliceE casts an empty interface to a [][]string. Supports: map[string]string and []string if it contains semicolon (as in CSV)

func ToDuration

func ToDuration(i interface{}) time.Duration

func ToDurationE

func ToDurationE(i interface{}) (d time.Duration, err error)

ToDurationE casts an empty interface to time.Duration.

func ToFloat64

func ToFloat64(i interface{}) float64

func ToFloat64E

func ToFloat64E(i interface{}) (float64, error)

ToFloat64E casts an empty interface to a float64.

func ToInt

func ToInt(i interface{}) int

func ToInt64

func ToInt64(i interface{}) int64

func ToInt64E

func ToInt64E(i interface{}) (int64, error)

ToInt64E casts an empty interface to an int64.

func ToIntE

func ToIntE(i interface{}) (int, error)

ToIntE casts an empty interface to an int.

func ToIntSlice

func ToIntSlice(i interface{}) []int

func ToIntSliceE

func ToIntSliceE(i interface{}) ([]int, error)

ToIntSliceE casts an empty interface to a []int.

func ToSlice

func ToSlice(i interface{}) []interface{}

func ToSliceE

func ToSliceE(i interface{}) ([]interface{}, error)

ToSliceE casts an empty interface to a []interface{}.

func ToString

func ToString(i interface{}) string

func ToStringE

func ToStringE(i interface{}) (string, error)

ToStringE casts an empty interface to a string.

func ToStringMap

func ToStringMap(i interface{}) map[string]interface{}

func ToStringMapBool

func ToStringMapBool(i interface{}) map[string]bool

func ToStringMapBoolE

func ToStringMapBoolE(i interface{}) (map[string]bool, error)

ToStringMapBoolE casts an empty interface to a map[string]bool.

func ToStringMapE

func ToStringMapE(i interface{}) (map[string]interface{}, error)

ToStringMapE casts an empty interface to a map[string]interface{}.

func ToStringMapString

func ToStringMapString(i interface{}) map[string]string

func ToStringMapStringE

func ToStringMapStringE(i interface{}) (map[string]string, error)

ToStringMapStringE casts an empty interface to a map[string]string.

func ToStringMapStringSlice

func ToStringMapStringSlice(i interface{}) map[string][]string

func ToStringMapStringSliceE

func ToStringMapStringSliceE(i interface{}) (map[string][]string, error)

ToStringMapStringSliceE casts an empty interface to a map[string][]string.

func ToStringSlice

func ToStringSlice(i interface{}) []string

func ToStringSliceE

func ToStringSliceE(i interface{}) ([]string, error)

ToStringSliceE casts an empty interface to a []string.

func ToTime

func ToTime(i interface{}) time.Time

func ToTimeE

func ToTimeE(i interface{}) (time.Time, error)

ToTimeE casts an empty interface to time.Time. Supported types: time.Time, string, int64 and float64. float64 fractals gets applied as nsec.

func ToUint

func ToUint(i interface{}) uint

func ToUintE

func ToUintE(i interface{}) (uint, error)

ToUintE casts an empty interface to an uint.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL