complex

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromJSON

func FromJSON(jsonStr string, target interface{})

FromJSON converts JSON string to specified type.

func FromJSONE

func FromJSONE(jsonStr string, target interface{}) error

FromJSONE converts JSON string to specified type with error.

func ToFloat64Map

func ToFloat64Map(value interface{}) map[string]float64

ToFloat64Map converts any type to map[string]float64.

func ToFloat64MapE

func ToFloat64MapE(value interface{}) (map[string]float64, error)

ToFloat64MapE converts any type to map[string]float64 with error.

func ToFloat64Slice

func ToFloat64Slice(value interface{}) []float64

ToFloat64Slice converts any type to []float64.

func ToFloat64SliceE

func ToFloat64SliceE(value interface{}) ([]float64, error)

ToFloat64SliceE converts any type to []float64 with error.

func ToIntMap

func ToIntMap(value interface{}) map[string]int

ToIntMap converts any type to map[string]int.

func ToIntMapE

func ToIntMapE(value interface{}) (map[string]int, error)

ToIntMapE converts any type to map[string]int with error.

func ToIntSlice

func ToIntSlice(value interface{}) []int

ToIntSlice converts any type to []int.

func ToIntSliceE

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

ToIntSliceE converts any type to []int with error.

func ToJSON

func ToJSON(value interface{}) string

ToJSON converts any type to JSON string.

func ToJSONE

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

ToJSONE converts any type to JSON string with error.

func ToMap

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

ToMap converts any type to map[string]interface{}.

func ToMapE

func ToMapE(value interface{}) (map[string]interface{}, error)

ToMapE converts any type to map[string]interface{} with error.

func ToMapFromJSON

func ToMapFromJSON(jsonStr string) map[string]interface{}

ToMapFromJSON converts JSON string to map[string]interface{}.

func ToMapFromJSONE

func ToMapFromJSONE(jsonStr string) (map[string]interface{}, error)

ToMapFromJSONE converts JSON string to map[string]interface{} with error.

func ToMapT

func ToMapT[K comparable, V any](value interface{}) map[K]V

ToMapT converts any type to map[K]V. This is a generic version of ToMap that returns a map with key type K and value type V. It uses reflection caching to improve performance for repeated conversions.

Examples:

// Convert to map[string]string
strMap := ToMapT[string, string](value)

// Convert to map[string]int
intMap := ToMapT[string, int](value)

// Convert to map[int]float64
floatMap := ToMapT[int, float64](value)

func ToMapTE

func ToMapTE[K comparable, V any](value interface{}) (map[K]V, error)

ToMapTE converts any type to map[K]V with error. This is a generic version of ToMapE that returns a map with key type K and value type V. It uses reflection caching to improve performance for repeated conversions.

Examples:

// Convert to map[string]string with error handling
strMap, err := ToMapTE[string, string](value)

// Convert to map[string]int with error handling
intMap, err := ToMapTE[string, int](value)

// Convert to map[int]float64 with error handling
floatMap, err := ToMapTE[int, float64](value)

func ToSlice

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

ToSlice converts any type to []interface{}.

func ToSliceE

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

ToSliceE converts any type to []interface{} with error.

func ToSliceFromJSON

func ToSliceFromJSON(jsonStr string) []interface{}

ToSliceFromJSON converts JSON string to []interface{}.

func ToSliceFromJSONE

func ToSliceFromJSONE(jsonStr string) ([]interface{}, error)

ToSliceFromJSONE converts JSON string to []interface{} with error.

func ToSliceT

func ToSliceT[T any](value interface{}) []T

ToSliceT converts any type to []T. This is a generic version of ToSlice that returns a slice of type T. It uses reflection caching to improve performance for repeated conversions.

Examples:

// Convert to []string
strSlice := ToSliceT[string](value)

// Convert to []int
intSlice := ToSliceT[int](value)

// Convert to []float64
floatSlice := ToSliceT[float64](value)

func ToSliceTE

func ToSliceTE[T any](value interface{}) ([]T, error)

ToSliceTE converts any type to []T with error. This is a generic version of ToSliceE that returns a slice of type T. It uses reflection caching to improve performance for repeated conversions.

Examples:

// Convert to []string with error handling
strSlice, err := ToSliceTE[string](value)

// Convert to []int with error handling
intSlice, err := ToSliceTE[int](value)

// Convert to []float64 with error handling
floatSlice, err := ToSliceTE[float64](value)

func ToStringMap

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

ToStringMap converts any type to map[string]string

func ToStringMapE

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

ToStringMapE converts any type to map[string]string with error

func ToStringSlice

func ToStringSlice(value interface{}) []string

ToStringSlice converts any type to []string

func ToStringSliceE

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

ToStringSliceE converts any type to []string with error

func ToStruct added in v1.1.3

func ToStruct(source, pointer interface{}, hooks ...HookFunc)

ToStruct converts a map or a struct to a struct. The `pointer` parameter should be a pointer to a struct. It supports `mconv` tag for custom field mapping. It accepts optional HookFuncs to provide custom conversion logic.

func ToStructE added in v1.1.3

func ToStructE(source, pointer interface{}, hooks ...HookFunc) error

StructE is the same as Struct but returns an error.

Types

type HookFunc added in v1.1.0

type HookFunc func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error)

HookFunc is a function type for decoding hooks. It's used to add custom conversion logic to the Struct function. The function takes a source type, a destination type, and the data to be converted. If it can handle the conversion, it should return the converted value and nil error. If it cannot handle the conversion, it should return the original data and nil error. If an error occurs during conversion, it should return nil and the error.

Jump to

Keyboard shortcuts

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