cast

package
v0.5.21 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: MIT Imports: 4 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CastFloatInterfaceToFloat

func CastFloatInterfaceToFloat[To FloatNumber](i interface{}) (To, bool)

func CastInterfaceListToFloatList

func CastInterfaceListToFloatList[To FloatNumber](list []interface{}) ([]To, bool)

func CastInterfaceListToIntList

func CastInterfaceListToIntList[To SignedInt | UnsignedInt](list []interface{}) ([]To, bool)

func CastInterfaceToFloatList

func CastInterfaceToFloatList[To FloatNumber](i interface{}) ([]To, bool)

func CastInterfaceToIntList

func CastInterfaceToIntList[To SignedInt | UnsignedInt](i interface{}) ([]To, bool)

func CastInterfaceToStringMap

func CastInterfaceToStringMap[To any, From any](m interface{}) (map[string]To, bool)

func CastList

func CastList[To any, From any](list []From) ([]To, bool)

CastList casts a list of From objects to To, by casting it to an interface{} in between.

func CastList2

func CastList2[To any, From any](list interface{}) ([]To, bool)

CastList2 attempts even harder to cast a list of From object to To, by checking if we might be dealing with a list masquerading as a interface{}, then a []interface{}, before checking for []To.

func CastListToFloatList2 added in v0.2.56

func CastListToFloatList2[To FloatNumber](list interface{}) ([]To, bool)

func CastListToIntList2 added in v0.2.56

func CastListToIntList2[To SignedInt | UnsignedInt](list interface{}) ([]To, bool)

func CastListToInterfaceList added in v0.4.10

func CastListToInterfaceList(value interface{}) ([]interface{}, error)

CastListToInterfaceList attempts to convert the given value to a list of interface{}.

The function checks if the provided value is a slice or an array. If so, it returns a slice of interface{}, where each item in the original slice or array is converted to its interface{} representation.

func CastListToStringList added in v0.5.16

func CastListToStringList(list interface{}) ([]string, error)

CastListToStringList casts a list of items to a list of strings. It handles both string aliases and string declarations.

func CastMapToInterfaceMap

func CastMapToInterfaceMap[From any](m map[string]From) map[string]interface{}

func CastNumberInterfaceToFloat

func CastNumberInterfaceToFloat[To FloatNumber](i interface{}) (To, bool)

func CastNumberInterfaceToInt added in v0.2.56

func CastNumberInterfaceToInt[To SignedInt | UnsignedInt](i interface{}) (To, bool)

func CastStringListToList added in v0.5.16

func CastStringListToList[T ~string](list []string) ([]T, error)

CastStringListToList casts a list of strings to a list of a specified type T. The target type T must be a string alias or string declaration.

func CastStringMap

func CastStringMap[To any, From any](m map[string]From) (map[string]To, bool)

func CastToNumberList

func CastToNumberList[To Number, From Number](list []From) ([]To, bool)

CastToNumberList casts a list of From objects to To. This is useful for transform between different int types, for example.

func ConvertMapToInterfaceMap added in v0.4.36

func ConvertMapToInterfaceMap(input interface{}) (map[string]interface{}, error)

func InterfaceAddr added in v0.4.36

func InterfaceAddr[T any](t T) *interface{}

func ToInterfaceValue added in v0.5.9

func ToInterfaceValue(value interface{}) (interface{}, error)

ToInterfaceValue converts a given value of any basic Go type (scalars, slices, arrays, maps) into a corresponding interface{}. Scalar types are returned as is. Slices and arrays are converted to []interface{} with each element being recursively processed. Maps are converted to map[string]interface{} with each value being recursively processed. The function uses reflection to handle various types dynamically. It returns an error if it encounters a type that it cannot process, or if map keys are not strings.

func ToString added in v0.5.16

func ToString(value interface{}) (string, error)

ToString converts a value to a string using reflection. It handles both type aliases and type declarations based on string.

This function is useful for dynamically converting values to strings at runtime, especially when dealing with types that may not be known at compile time.

Types

type FloatNumber

type FloatNumber interface {
	float32 | float64
}

type Number

type Number interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64
}

type SignedInt

type SignedInt interface {
	int | int8 | int16 | int32 | int64
}

type UnsignedInt

type UnsignedInt interface {
	uint | uint8 | uint16 | uint32 | uint64 | uintptr
}

Jump to

Keyboard shortcuts

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