convert

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2022 License: Apache-2.0 Imports: 7 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 interface{}) 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 interface{}, 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 interface{}, 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 Float

func Float(value interface{}) 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 interface{}, 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 interface{}, 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 interface{}) 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 interface{}) 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 interface{}, 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 interface{}, 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 interface{}, 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 interface{}, 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 interface{}) bool

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

func MapOfInterface

func MapOfInterface(value interface{}) map[string]interface{}

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

func MapOfInterfaceOk

func MapOfInterfaceOk(value interface{}) (map[string]interface{}, bool)

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

func NullBool

func NullBool(value interface{}) 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 interface{}) 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 interface{}) 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 interface{}) 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 SliceOfBool

func SliceOfBool(value interface{}) []bool

SliceOfBool converts the value into a slice of interface{}. It works with interface{}, []interface{}, []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 interface{}) []float64

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

func SliceOfInt

func SliceOfInt(value interface{}) []int

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

func SliceOfInterface

func SliceOfInterface(value interface{}) []interface{}

SliceOfInterface converts the value into a slice of interface{}. It works with interface{}, []interface{}, []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 interface{}) []map[string]interface{}

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

func SliceOfString

func SliceOfString(value interface{}) []string

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

func String

func String(value interface{}) 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 interface{}, 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 interface{}, 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]interface{}
}

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