Documentation
¶
Index ¶
- func FromJSON(jsonStr string, target interface{})
- func FromJSONE(jsonStr string, target interface{}) error
- func ToFloat64Map(value interface{}) map[string]float64
- func ToFloat64MapE(value interface{}) (map[string]float64, error)
- func ToFloat64Slice(value interface{}) []float64
- func ToFloat64SliceE(value interface{}) ([]float64, error)
- func ToIntMap(value interface{}) map[string]int
- func ToIntMapE(value interface{}) (map[string]int, error)
- func ToIntSlice(value interface{}) []int
- func ToIntSliceE(value interface{}) ([]int, error)
- func ToJSON(value interface{}) string
- func ToJSONE(value interface{}) (string, error)
- func ToMap(value interface{}) map[string]interface{}
- func ToMapE(value interface{}) (map[string]interface{}, error)
- func ToMapFromJSON(jsonStr string) map[string]interface{}
- func ToMapFromJSONE(jsonStr string) (map[string]interface{}, error)
- func ToMapT[K comparable, V any](value interface{}) map[K]V
- func ToMapTE[K comparable, V any](value interface{}) (map[K]V, error)
- func ToSlice(value interface{}) []interface{}
- func ToSliceE(value interface{}) ([]interface{}, error)
- func ToSliceFromJSON(jsonStr string) []interface{}
- func ToSliceFromJSONE(jsonStr string) ([]interface{}, error)
- func ToSliceT[T any](value interface{}) []T
- func ToSliceTE[T any](value interface{}) ([]T, error)
- func ToStringMap(value interface{}) map[string]string
- func ToStringMapE(value interface{}) (map[string]string, error)
- func ToStringSlice(value interface{}) []string
- func ToStringSliceE(value interface{}) ([]string, error)
- func ToStruct(source, pointer interface{}, hooks ...HookFunc)
- func ToStructE(source, pointer interface{}, hooks ...HookFunc) error
- func ToT[T any](value interface{}, hooks ...HookFunc) T
- func ToTE[T any](value interface{}, hooks ...HookFunc) (T, error)
- type HookFunc
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 ToFloat64Map ¶
ToFloat64Map converts any type to map[string]float64.
func ToFloat64MapE ¶
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 ¶
ToFloat64SliceE converts any type to []float64 with error.
func ToIntSliceE ¶
ToIntSliceE converts any type to []int with error.
func ToMap ¶
func ToMap(value interface{}) map[string]interface{}
ToMap converts any type to map[string]interface{}.
func ToMapFromJSON ¶
ToMapFromJSON converts JSON string to map[string]interface{}.
func ToMapFromJSONE ¶
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. Source keys are converted directly to K without a string intermediate.
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. It rejects key collisions and includes the failing key in ConversionError.Path.
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 ¶
ToSliceFromJSONE converts JSON string to []interface{} with error.
func ToSliceT ¶
func ToSliceT[T any](value interface{}) []T
ToSliceT converts any type to []T. Scalar targets use direct converters; defined and complex targets use the shared reflection decoder.
Examples:
// Convert to []string strSlice := ToSliceT[string](value) // Convert to []int intSlice := ToSliceT[int](value) // Convert to []float64 floatSlice := ToSliceT[float64](value)
func ToSliceTE ¶
ToSliceTE converts any type to []T with error. Errors include the failing element index in ConversionError.Path.
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 ¶
ToStringMap converts any type to map[string]string
func ToStringMapE ¶
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 ¶
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
ToStructE converts source into pointer and reports conversion errors.
Types ¶
type HookFunc ¶ added in v1.1.0
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.