convert

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2023 License: Apache-2.0 Imports: 8 Imported by: 20

README

convert 🚀

GoDoc Build Status Codecov Go Report Card Version

Type Conversion Sugar

It's not rocket science -- it's just a bunch of functions that convert from one type into another (very similar) type. I'm sure someone else out there has already done it, and probably better, too. You should use their library instead.

Pull Requests Welcome

This library is a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making this library better, send in a pull request. We're all in this together! 🚀

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(value any) bool

Bool forces a conversion from an arbitrary value into a boolean. If the value cannot be converted, then the default value for the type is used.

func BoolDefault

func BoolDefault(value any, defaultValue bool) bool

BoolDefault forces a conversion from an arbitrary value into a bool. if the value cannot be converted, then the default value is used.

func BoolOk

func BoolOk(value any, defaultValue bool) (bool, bool)

BoolOk converts an arbitrary value (passed in the first parameter) into a boolean, somehow, no matter what. The first result is the final converted value, or the default value (passed in the second parameter) The second result is TRUE if the value was naturally a bool, and FALSE otherwise

Conversion Rules: Nils return default value and Ok=false Bools are passed through with Ok=true Ints and Floats all convert to FALSE if they are zero, and TRUE if they are non-zero. In these cases, Ok=false String values of "true" and "false" convert normall, and Ok=true. All other strings return the default value, with Ok=false Known interfaces (Booler, Inter, Floater, Stringer) are handled like their corresponding types All other values return the default value with Ok=false

func Bytes added in v0.5.4

func Bytes(value any) []byte

Bytes forces a conversion from an arbitrary value into a slice of bytes.

func Float

func Float(value any) float64

Float forces a conversion from an arbitrary value into a float64. If the value cannot be converted, then the zero value for the type (false) is used.

func FloatDefault

func FloatDefault(value any, defaultValue float64) float64

FloatDefault forces a conversion from an arbitrary value into a float64. if the value cannot be converted, then the default value is used.

func FloatOk

func FloatOk(value any, defaultValue float64) (float64, bool)

FloatOk converts an arbitrary value (passed in the first parameter) into a float64, no matter what. The first result is the final converted value, or the default value (passed in the second parameter) The second result is TRUE if the value was naturally a floating point number, and FALSE otherwise

Conversion Rules: Nils and Bools return default value and Ok=false Ints and Floats are converted into float64, with Ok=true String values are attempted to parse as a float64. If unsuccessful, default value is returned. For all strings, Ok=false Known interfaces (Inter, Floater, Stringer) are handled like their corresponding types. All other values return the default value with Ok=false

func Int

func Int(value any) int

Int forces a conversion from an arbitrary value into an int. If the value cannot be converted, then the zero value for the type (0) is used.

func Int64

func Int64(value any) int64

Int64 forces a conversion from an arbitrary value into an int. If the value cannot be converted, then the zero value for the type (0) is used.

func Int64Default

func Int64Default(value any, defaultValue int64) int64

Int64Default forces a conversion from an arbitrary value into a int. if the value cannot be converted, then the default value is used.

func Int64Ok

func Int64Ok(value any, defaultValue int64) (int64, bool)

Int64Ok converts an arbitrary value (passed in the first parameter) into an int, no matter what. The first result is the final converted value, or the default value (passed in the second parameter) The second result is TRUE if the value was naturally an integer, and FALSE otherwise

Conversion Rules: Nils and Bools return default value and Ok=false Int64s are returned directly with Ok=true Floats are truncated into ints. If there is no decimal value then Ok=true String values are attempted to parse as a int. If unsuccessful, default value is returned. For all strings, Ok=false Known interfaces (Int64er, Floater, Stringer) are handled like their corresponding types. All other values return the default value with Ok=false

func IntDefault

func IntDefault(value any, defaultValue int) int

IntDefault forces a conversion from an arbitrary value into a int. if the value cannot be converted, then the default value is used.

func IntOk

func IntOk(value any, defaultValue int) (int, bool)

IntOk converts an arbitrary value (passed in the first parameter) into an int, no matter what. The first result is the final converted value, or the default value (passed in the second parameter) The second result is TRUE if the value was naturally an integer, and FALSE otherwise

Conversion Rules: Nils and Bools return default value and Ok=false Ints are returned directly with Ok=true Floats are truncated into ints. If there is no decimal value then Ok=true String values are attempted to parse as a int. If unsuccessful, default value is returned. For all strings, Ok=false Known interfaces (Inter, Floater, Stringer) are handled like their corresponding types. All other values return the default value with Ok=false

func Interface

func Interface(value any) any

func IsZeroValue

func IsZeroValue(value any) bool

IsZeroValue returns TRUE if the value is the zero value for its datatype

func JoinString added in v0.5.3

func JoinString(value any, delimiter string) string

func MapOfInterface

func MapOfInterface(value any) map[string]any

MapOfInterface attempts to convert the generic value into a map[string]any The boolean result value returns TRUE if successful. FALSE otherwise

func MapOfInterfaceOk

func MapOfInterfaceOk(value any) (map[string]any, bool)

MapOfInterfaceOk attempts to convert the generic value into a map[string]any The boolean result value returns TRUE if successful. FALSE otherwise

func NullBool

func NullBool(value any) null.Bool

NullBool converts a value into a nullable value. The value is only set if the input value is a natural match for this data type.

func NullFloat

func NullFloat(value any) null.Float

NullFloat converts a value into a nullable value. The value is only set if the input value is a natural match for this data type.

func NullInt

func NullInt(value any) null.Int

NullInt converts a value into a nullable value. The value is only set if the input value is a natural match for this data type.

func NullInt64

func NullInt64(value any) null.Int64

NullInt64 converts a value into a nullable value. The value is only set if the input value is a natural match for this data type.

func ReflectType added in v0.6.0

func ReflectType(value any) reflect.Type

func ReflectValue added in v0.6.0

func ReflectValue(value any) reflect.Value

func SliceLength added in v0.6.0

func SliceLength(value any) int

func SliceOfBool

func SliceOfBool(value any) []bool

SliceOfBool converts the value into a slice of any. It works with any, []any, []string, []int, []float64, string, int, and float64 values. If the passed value cannot be converted, then an empty slice is returned.

func SliceOfFloat

func SliceOfFloat(value any) []float64

SliceOfFloat converts the value into a slice of floats. It works with any, []any, []float64, and float64 values. If the passed value cannot be converted, then an empty slice is returned.

func SliceOfInt

func SliceOfInt(value any) []int

SliceOfInt converts the value into a slice of ints. It works with any, []any, []string, []int, and int values. If the passed value cannot be converted, then an empty slice is returned.

func SliceOfInt64 added in v0.10.0

func SliceOfInt64(value any) []int64

SliceOfInt64 converts the value into a slice of int64s. It works with any, []any, []string, []int, and int values. If the passed value cannot be converted, then an empty slice is returned.

func SliceOfInterface

func SliceOfInterface(value any) []any

SliceOfInterface converts the value into a slice of any. It works with any, []any, []string, []int, []float64, string, int, and float64 values. If the passed value cannot be converted, then an empty slice is returned.

func SliceOfMap

func SliceOfMap(value any) []map[string]any

SliceOfMap converts the value into a slice of map[string]any. It works with []any, []map[string]any. If the passed value cannot be converted, then an empty slice is returned.

func SliceOfString

func SliceOfString(value any) []string

SliceOfString converts the value into a slice of strings. It works with any, []any, []string, and string values. If the passed value cannot be converted, then an empty slice is returned.

func SplitSliceOfString added in v0.5.2

func SplitSliceOfString(value any, sep string) []string

SplitSliceOfString splits is a special case of SliceOfString. If it receives a string, Stringer, or reflect.String, it will make a slice of strings by splitting the value into a slice. All other values are passed to SliceOfString to be processed normally.

func String

func String(value any) string

String forces a conversion from an arbitrary value into an string. If the value cannot be converted, then the default value for the type is used.

func StringDefault

func StringDefault(value any, defaultValue string) string

StringDefault forces a conversion from an arbitrary value into a string. if the value cannot be converted, then the default value is used.

func StringOk

func StringOk(value any, defaultValue string) (string, bool)

StringOk converts an arbitrary value (passed in the first parameter) into a string, no matter what. The first result is the final converted value, or the default value (passed in the second parameter) The second result is TRUE if the value was naturally a string, and FALSE otherwise

Conversion Rules: Nils return default value and Ok=false Bools are formated as "true" or "false" with Ok=false Ints are formated as strings with Ok=false Floats are formatted with 2 decimal places, with Ok=false String are passed through directly, with Ok=true Known interfaces (Inter, Floater, Stringer) are handled like their corresponding types. All other values return the default value with Ok=false

Types

type Booler

type Booler interface {

	// Bool returns the float64 value of the underlying object
	Bool() bool
}

Booler interface wraps the Bool() function that enables custom types to convert themselves to bool.

type Floater

type Floater interface {

	// Float returns the float64 value of the underlying object
	Float() float64
}

Floater interface wraps the Float() function that enables custom types to convert themselves to float64.

type Hexer

type Hexer interface {
	Hex() string
}

Hexer interface wraps the Hex() function that enables a custom type to convert itself into a hexadecimal string

type Inter

type Inter interface {

	// Int returns the int value of the underlying object
	Int() int
}

Inter interface wraps the Int() function that enables custom types to convert themselves to ints.

type Maplike

type Maplike interface {
	AsMapOfInterface() map[string]any
}

type Nuller

type Nuller interface {

	// IsNull returns TRUE if the underlying value is null
	IsNull() bool
}

Nuller wraps the IsNull interface (implemented by the null.* package) that enables custom types to declare that their value is null (zero)

type Stringer

type Stringer interface {

	// String returns the string value of the underlying object
	String() string
}

Stringer interface wraps the String() function that enables a custom type to convert themselves into strings.

Jump to

Keyboard shortcuts

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