convert

package module
v0.13.5 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2022 License: Apache-2.0 Imports: 7 Imported by: 8

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 added in v0.2.2

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 added in v0.4.0

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 added in v0.2.2

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 added in v0.7.0

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 added in v0.7.0

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 added in v0.7.0

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 added in v0.1.1

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 added in v0.4.0

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 added in v0.2.0

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 added in v0.4.0

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 added in v0.2.0

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 added in v0.13.2

func Interface(value any) any

func IsZeroValue added in v0.10.0

func IsZeroValue(value interface{}) bool

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

func MapOfInterface added in v0.10.0

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 added in v0.11.0

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 added in v0.7.0

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 added in v0.7.0

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 added in v0.7.0

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 added in v0.13.0

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 added in v0.11.0

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 added in v0.9.1

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 added in v0.9.1

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 added in v0.11.0

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 added in v0.10.0

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 added in v0.8.0

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 added in v0.1.3

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 added in v0.4.0

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 added in v0.2.0

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 added in v0.7.0

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 added in v0.7.0

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 added in v0.13.0

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 added in v0.7.0

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 added in v0.10.0

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

type Nuller added in v0.13.3

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 added in v0.2.1

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