Documentation
¶
Overview ¶
Package mconv converts scalar values, containers, JSON, and structs with a consistent error model. The root package is the canonical public API.
Functions ending in E return conversion errors and are preferred for data from requests, configuration, storage, or other external systems. Their convenience counterparts return the target type's zero value on failure.
Example ¶
package main
import (
"fmt"
"github.com/graingo/mconv"
)
func main() {
fmt.Println(mconv.ToString(42))
fmt.Println(mconv.ToBool("yes"))
fmt.Println(mconv.ToInt("invalid"))
}
Output: 42 true 0
Index ¶
- Variables
- func ClearAllCaches()
- func ClearConversionCache()
- func ClearStringCache()
- func ClearTimeCache()
- func ClearTypeInfoCache()
- func FromJSON(jsonStr string, target interface{})
- func FromJSONE(jsonStr string, target interface{}) error
- func SetConversionCacheSize(size int)
- func SetStringCacheSize(size int)
- func SetTimeCacheSize(size int)
- func SetTypeInfoCacheSize(size int)
- func To[T any](value interface{}, hooks ...HookFunc) T
- func ToBool(value interface{}) bool
- func ToBoolE(value interface{}) (bool, error)
- func ToComplex64(value interface{}) complex64
- func ToComplex64E(value interface{}) (complex64, error)
- func ToComplex128(value interface{}) complex128
- func ToComplex128E(value interface{}) (complex128, error)
- func ToDuration(value interface{}) time.Duration
- func ToDurationE(value interface{}) (time.Duration, error)
- func ToE[T any](value interface{}, hooks ...HookFunc) (T, error)
- func ToFloat32(value interface{}) float32
- func ToFloat32E(value interface{}) (float32, error)
- func ToFloat64(value interface{}) float64
- func ToFloat64E(value interface{}) (float64, 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 ToInt(value interface{}) int
- func ToInt8(value interface{}) int8
- func ToInt8E(value interface{}) (int8, error)
- func ToInt16(value interface{}) int16
- func ToInt16E(value interface{}) (int16, error)
- func ToInt32(value interface{}) int32
- func ToInt32E(value interface{}) (int32, error)
- func ToInt64(value interface{}) int64
- func ToInt64E(value interface{}) (int64, error)
- func ToIntE(value interface{}) (int, 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 ToString(value interface{}) string
- func ToStringE(value interface{}) (string, 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 ToTime(value interface{}, formats ...string) time.Time
- func ToTimeE(value interface{}, formats ...string) (time.Time, error)
- func ToUint(value interface{}) uint
- func ToUint8(value interface{}) uint8
- func ToUint8E(value interface{}) (uint8, error)
- func ToUint16(value interface{}) uint16
- func ToUint16E(value interface{}) (uint16, error)
- func ToUint32(value interface{}) uint32
- func ToUint32E(value interface{}) (uint32, error)
- func ToUint64(value interface{}) uint64
- func ToUint64E(value interface{}) (uint64, error)
- func ToUintE(value interface{}) (uint, error)
- type ConversionError
- type HookFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnsupportedType = internal.ErrUnsupportedType ErrConversionFailed = internal.ErrConversionFailed ErrOverflow = internal.ErrOverflow ErrInvalidTimeFormat = internal.ErrInvalidTimeFormat ErrInvalidJSONFormat = internal.ErrInvalidJSONFormat )
Functions ¶
func ClearConversionCache ¶
func ClearConversionCache()
ClearConversionCache is retained for source compatibility. Deprecated: direct type checks are faster than a separate conversion cache.
func ClearStringCache ¶
func ClearStringCache()
ClearStringCache clears the string conversion cache.
func ClearTypeInfoCache ¶
func ClearTypeInfoCache()
ClearTypeInfoCache is retained for source compatibility. Deprecated: mconv uses an automatic decoder cache and has no separate type-info cache.
func FromJSON ¶
func FromJSON(jsonStr string, target interface{})
FromJSON converts JSON into target.
func SetConversionCacheSize ¶
func SetConversionCacheSize(size int)
SetConversionCacheSize is retained for source compatibility. Deprecated: direct type checks are faster than a separate conversion cache.
func SetStringCacheSize ¶
func SetStringCacheSize(size int)
SetStringCacheSize sets the optional string conversion cache size. The cache is disabled by default; a non-positive size disables it.
func SetTimeCacheSize ¶
func SetTimeCacheSize(size int)
SetTimeCacheSize sets the optional time conversion cache size. The cache is disabled by default; a non-positive size disables it.
func SetTypeInfoCacheSize ¶
func SetTypeInfoCacheSize(size int)
SetTypeInfoCacheSize is retained for source compatibility. Deprecated: mconv uses an automatic decoder cache and has no separate type-info cache.
func ToComplex64 ¶
func ToComplex64(value interface{}) complex64
ToComplex64 converts any type to complex64.
func ToComplex64E ¶
ToComplex64E converts any type to complex64 with error.
func ToComplex128 ¶
func ToComplex128(value interface{}) complex128
ToComplex128 converts any type to complex128.
func ToComplex128E ¶
func ToComplex128E(value interface{}) (complex128, error)
ToComplex128E converts any type to complex128 with error.
func ToDuration ¶ added in v0.1.2
ToDuration converts any type to time.Duration.
func ToDurationE ¶ added in v0.1.2
ToDurationE converts any type to time.Duration with error.
func ToE ¶ added in v1.2.0
ToE converts value to T and returns a conversion error with path context.
Example ¶
package main
import (
"fmt"
"github.com/graingo/mconv"
)
func main() {
type user struct {
ID int `json:"id"`
Name string `json:"name"`
}
converted, err := mconv.ToE[user](map[string]interface{}{
"id": "7",
"name": "Maltose",
})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%d %s\n", converted.ID, converted.Name)
}
Output: 7 Maltose
func ToFloat32E ¶
ToFloat32E converts any type to float32 with error.
func ToFloat64E ¶
ToFloat64E converts any type to float64 with error.
func ToFloat64Map ¶
ToFloat64Map converts any type to a float64 map.
func ToFloat64MapE ¶
ToFloat64MapE converts any type to a float64 map with error.
func ToFloat64Slice ¶
func ToFloat64Slice(value interface{}) []float64
ToFloat64Slice converts any type to a float64 slice.
func ToFloat64SliceE ¶
ToFloat64SliceE converts any type to a float64 slice with error.
func ToIntSlice ¶
func ToIntSlice(value interface{}) []int
ToIntSlice converts any type to an int slice.
func ToIntSliceE ¶
ToIntSliceE converts any type to an int slice with error.
func ToMapFromJSON ¶
ToMapFromJSON converts JSON to a map.
func ToMapFromJSONE ¶
ToMapFromJSONE converts JSON to a map with error.
func ToMapT ¶ added in v1.2.0
func ToMapT[K comparable, V any](value interface{}) map[K]V
ToMapT converts value to map[K]V and returns nil when conversion fails.
func ToMapTE ¶ added in v1.2.0
func ToMapTE[K comparable, V any](value interface{}) (map[K]V, error)
ToMapTE converts value to map[K]V and returns a conversion error with key context.
func ToSliceE ¶
func ToSliceE(value interface{}) ([]interface{}, error)
ToSliceE converts any type to slice with error.
func ToSliceFromJSON ¶
func ToSliceFromJSON(jsonStr string) []interface{}
ToSliceFromJSON converts JSON to a slice.
func ToSliceFromJSONE ¶
ToSliceFromJSONE converts JSON to a slice with error.
func ToSliceT ¶ added in v1.2.0
func ToSliceT[T any](value interface{}) []T
ToSliceT converts value to []T and returns nil when conversion fails.
func ToSliceTE ¶ added in v1.2.0
ToSliceTE converts value to []T and returns a conversion error with index context.
func ToStringMap ¶
ToStringMap converts any type to a string map.
func ToStringMapE ¶
ToStringMapE converts any type to a string map with error.
func ToStringSlice ¶
func ToStringSlice(value interface{}) []string
ToStringSlice converts any type to a string slice.
func ToStringSliceE ¶
ToStringSliceE converts any type to a string slice with error.
func ToStruct ¶ added in v1.1.3
func ToStruct(source, pointer interface{}, hooks ...HookFunc)
ToStruct converts a map or struct to a struct.
Types ¶
type ConversionError ¶ added in v1.2.0
type ConversionError = internal.ConversionError
ConversionError describes a failed conversion and its destination path.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/graingo/mconv"
)
func main() {
_, err := mconv.ToInt8E(128)
fmt.Println(errors.Is(err, mconv.ErrOverflow))
var conversionErr *mconv.ConversionError
if errors.As(err, &conversionErr) {
fmt.Println(conversionErr.TargetType)
}
}
Output: true int8
Directories
¶
| Path | Synopsis |
|---|---|
|
Package basic implements scalar conversions used by mconv.
|
Package basic implements scalar conversions used by mconv. |
|
Package complex implements container, JSON, generic, and struct conversions.
|
Package complex implements container, JSON, generic, and struct conversions. |