cast

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2020 License: MIT Imports: 9 Imported by: 0

README

cast

Mature This project adheres to Semantic Versioning. This package is considered mature, you should expect package stability in Minor and Patch version releases

  • Major: backwards incompatible package updates
  • Minor: feature additions
  • Patch: bug fixes, backward compatible model and function changes, etc.

CHANGELOG

Release GoDoc Build status Coverage status Go Report Card Github issues Github pull requests MIT

cast is forked from spf13/cast.

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<Type> 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. In order to differentiate between success and the nil value a set of To<Type>E methods are provided that return both the cast value and any error that occurrs.

Examples
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"
ToInt
intVal := cast.ToInt(8)           // 8
intVal := cast.ToInt(8.31)        // 8
intVal := cast.ToInt("8")         // 8
intVal := cast.ToInt(true)        // 1
intVal := cast.ToInt(false)       // 0

var eight interface{} = 8
intVal := cast.ToInt(eight)         // 8
intVal := cast.ToInt(nil)           // 0
Error checking
intVal := cast.ToInt("Hi!")         // 0
intVal, err := cast.ToIntE("Hi!")   // 0, unable to cast "Hi!" of type string to int

Documentation

Index

Constants

View Source
const (
	// ISO8601Date defines the ISO-8601 date format.
	ISO8601Date = "2006-01-02"
	// ISO8601DateTime1 defines the ISO-8601 date/time format without a
	// timezone offset.
	ISO8601DateTime1 = "2006-01-02 15:04:05"
	// ISO8601DateTime2 defines the ISO-8601 date/time format without a
	// timezone offset.
	ISO8601DateTime2 = "2006-01-02T15:04:05"
	// ISO8601DateTimeOffset1 defines the ISO-8601 date/time format with a
	// timezone offset.
	ISO8601DateTimeOffset1 = "2006-01-02 15:04:05Z07:00"
	// ISO8601DateTimeOffset2 defines the ISO-8601 date/time format with a
	// timezone offset.
	ISO8601DateTimeOffset2 = "2006-01-02 15:04:05 -07:00"
	// ISO8601DateTimeOffset3 defines the ISO-8601 date/time format with a
	// timezone offset.
	ISO8601DateTimeOffset3 = "2006-01-02 15:04:05 -0700"
	// TimeString defines the Time.String() date/time format.
	TimeString = "2006-01-02 15:04:05.999999999 -0700 MST"
)

Variables

View Source
var ErrUintBelowZero = errors.New("cannot cast negative value as uint")

ErrUintBelowZero defines the error returned when attempting to cast a negative value as a uint.

Functions

func StringToDate

func StringToDate(s string) time.Time

StringToDate attempts to parse a string into a time.Time type using a predefined list of formats. If no suitable format is found, an error is returned. Any errors are discarded

func StringToDateE

func StringToDateE(s string) (time.Time, error)

StringToDateE attempts to parse a string into a time.Time type using a predefined list of formats. If no suitable format is found, an error is returned.

func ToBool

func ToBool(i interface{}) bool

ToBool casts an interface to a bool type, discarding any errors.

func ToBoolE

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

ToBoolE casts an interface to a bool type.

func ToBoolSlice

func ToBoolSlice(i interface{}) []bool

ToBoolSlice casts an interface to a []bool type, discarding any errors.

func ToBoolSliceE

func ToBoolSliceE(i interface{}) ([]bool, error)

ToBoolSliceE casts an interface to a []bool type.

func ToDuration

func ToDuration(i interface{}) time.Duration

ToDuration casts an interface to a time.Duration type, discarding any errors.

func ToDurationE

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

ToDurationE casts an interface to a time.Duration type.

func ToDurationSlice

func ToDurationSlice(i interface{}) []time.Duration

ToDurationSlice casts an interface to a []time.Duration type, discarding any errors.

func ToDurationSliceE

func ToDurationSliceE(i interface{}) ([]time.Duration, error)

ToDurationSliceE casts an interface to a []time.Duration type.

func ToFloat32

func ToFloat32(i interface{}) float32

ToFloat32 casts an interface to a float32 type, discarding any errors.

func ToFloat32E

func ToFloat32E(i interface{}) (float32, error)

ToFloat32E casts an interface to a float32 type.

func ToFloat64

func ToFloat64(i interface{}) float64

ToFloat64 casts an interface to a float64 type, discarding any errors.

func ToFloat64E

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

ToFloat64E casts an interface to a float64 type.

func ToInt

func ToInt(i interface{}) int

ToInt casts an interface to an int type, discarding any errors.

func ToInt16

func ToInt16(i interface{}) int16

ToInt16 casts an interface to an int16 type, discarding any errors.

func ToInt16E

func ToInt16E(i interface{}) (int16, error)

ToInt16E casts an interface to an int16 type.

func ToInt32

func ToInt32(i interface{}) int32

ToInt32 casts an interface to an int32 type, discarding any errors.

func ToInt32E

func ToInt32E(i interface{}) (int32, error)

ToInt32E casts an interface to an int32 type.

func ToInt64

func ToInt64(i interface{}) int64

ToInt64 casts an interface to an int64 type, discarding any errors.

func ToInt64E

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

ToInt64E casts an interface to an int64 type.

func ToInt64Slice added in v1.0.1

func ToInt64Slice(i interface{}) []int64

ToInt64Slice casts an interface to a []int type, discarding any errors.

func ToInt64SliceE added in v1.0.1

func ToInt64SliceE(i interface{}) ([]int64, error)

ToInt64SliceE casts an interface to a []int type.

func ToInt8

func ToInt8(i interface{}) int8

ToInt8 casts an interface to an int8 type, discarding any errors.

func ToInt8E

func ToInt8E(i interface{}) (int8, error)

ToInt8E casts an interface to an int8 type.

func ToIntE

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

ToIntE casts an interface to an int type.

func ToIntSlice

func ToIntSlice(i interface{}) []int

ToIntSlice casts an interface to a []int type, discarding any errors.

func ToIntSliceE

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

ToIntSliceE casts an interface to a []int type.

func ToSlice

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

ToSlice casts an interface to a []interface{} type, discarding any errors.

func ToSliceE

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

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

func ToString

func ToString(i interface{}) string

ToString casts an interface to a string type, discarding any errors.

func ToStringE

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

ToStringE casts an interface to a string type.

func ToStringMap

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

ToStringMap casts an interface to a map[string]interface{} type, discarding any errors.

func ToStringMapBool

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

ToStringMapBool casts an interface to a map[string]bool type, discarding any errors.

func ToStringMapBoolE

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

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

func ToStringMapE

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

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

func ToStringMapString

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

ToStringMapString casts an interface to a map[string]string type, discarding any errors.

func ToStringMapStringE

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

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

func ToStringMapStringSlice

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

ToStringMapStringSlice casts an interface to a map[string][]string type, discarding any errors.

func ToStringMapStringSliceE

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

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

func ToStringSlice

func ToStringSlice(i interface{}) []string

ToStringSlice casts an interface to a []string type, discarding any errors.

func ToStringSliceE

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

ToStringSliceE casts an interface to a []string type.

func ToTime

func ToTime(i interface{}) time.Time

ToTime casts an interface to a time.Time type, discarding any errors.

func ToTimeE

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

ToTimeE casts an interface to a time.Time type.

func ToUint

func ToUint(i interface{}) uint

ToUint casts an interface to a uint type, discarding any errors.

func ToUint16

func ToUint16(i interface{}) uint16

ToUint16 casts an interface to a uint16 type, discarding any errors.

func ToUint16E

func ToUint16E(i interface{}) (uint16, error)

ToUint16E casts an interface to a uint16 type.

func ToUint32

func ToUint32(i interface{}) uint32

ToUint32 casts an interface to a uint32 type, discarding any errors.

func ToUint32E

func ToUint32E(i interface{}) (uint32, error)

ToUint32E casts an interface to a uint32 type.

func ToUint64

func ToUint64(i interface{}) uint64

ToUint64 casts an interface to a uint64 type, discarding any errors.

func ToUint64E

func ToUint64E(i interface{}) (uint64, error)

ToUint64E casts an interface to a uint64 type.

func ToUint64Slice added in v1.0.2

func ToUint64Slice(i interface{}) []uint64

ToUint64Slice casts an interface to a []int type, discarding any errors.

func ToUint64SliceE added in v1.0.2

func ToUint64SliceE(i interface{}) ([]uint64, error)

ToUint64SliceE casts an interface to a []int type.

func ToUint8

func ToUint8(i interface{}) uint8

ToUint8 casts an interface to a uint type, discarding any errors.

func ToUint8E

func ToUint8E(i interface{}) (uint8, error)

ToUint8E casts an interface to a uint type.

func ToUintE

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

ToUintE casts an interface to a uint type.

Types

This section is empty.

Jump to

Keyboard shortcuts

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