Documentation
¶
Overview ¶
Package typeutil provides general-purpose type conversion utilities.
This package contains safe conversion functions for working with heterogeneous any values, particularly those arising from JSON/YAML parsing where types may vary at runtime.
Key Functions ¶
Strict Conversions (return (value, ok) to distinguish zero from missing/invalid):
- ParseIntValue() - Strictly parse numeric types to int; returns (value, ok). Use when the caller needs to distinguish "missing/invalid" from a zero value, or when string inputs are not expected (e.g. YAML config field parsing).
Bool Extraction:
- ParseBool() - Extract a bool from map[string]any by key; returns false on missing, nil map, or non-bool.
Safe Conversions (return 0 on overflow or invalid input):
- SafeUint64ToInt() - Convert uint64 to int, returning 0 on overflow
- SafeUintToInt() - Convert uint to int, returning 0 on overflow
Lenient Conversions (also handle strings, return 0 on failure):
- ConvertToInt() - Leniently convert any value (int/int64/float64/string) to int, returning 0 on failure. Use for heterogeneous sources such as JSON metrics, log-parsed data, or user-provided strings where a zero default is acceptable.
- ConvertToFloat() - Safely convert any value (float64/int/int64/string) to float64, returning 0 on failure.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertToFloat ¶
ConvertToFloat safely converts any value to float64, returning 0 on failure.
Supported input types: float64, int, int64, and string (parsed via strconv.ParseFloat). Returns 0 for any other type or for strings that cannot be parsed as a float.
func ConvertToInt ¶
ConvertToInt leniently converts any value to int, returning 0 on failure.
Unlike ParseIntValue, this function also handles string inputs via strconv.Atoi, making it suitable for heterogeneous sources such as JSON metrics, log-parsed data, or user-provided configuration where a zero default on failure is acceptable and the caller does not need to distinguish "invalid" from a genuine zero.
For strict numeric-only parsing where the caller must distinguish missing/invalid values from zero, use ParseIntValue instead.
func ParseBool ¶
ParseBool extracts a boolean value from a map[string]any by key. Returns false if the map is nil, the key is absent, or the value is not a bool.
func ParseIntValue ¶
ParseIntValue strictly parses numeric types (int, int64, uint64, float64) to int, returning (value, true) on success and (0, false) for any unrecognized or non-numeric type.
Use this when the caller needs to distinguish a missing/invalid value from a legitimate zero, or when string inputs are not expected (e.g. YAML config field parsing where the YAML library has already produced a typed numeric value).
For lenient conversion that also handles string inputs and returns 0 on failure, use ConvertToInt instead.
func SafeUint64ToInt ¶
SafeUint64ToInt converts uint64 to int, returning 0 if overflow would occur.
func SafeUintToInt ¶
SafeUintToInt converts uint to int, returning 0 if overflow would occur. This is a thin wrapper around SafeUint64ToInt that widens the uint argument first.
Types ¶
This section is empty.