Documentation ¶
Overview ¶
Package conv is conversions between various types, without loss of precision. Functions panic if a conversion cannot be done precisely.
Provides a registration mechanism for conv.To function, in a way that prevents import cycles, with following example: - encoding/json imports conv to register conversions between json.Value and other types (maps, slices, etc) via init - encoding/json imports conv to take advantage of conv.To - conv never immports encoding/json, so no import cycle - any other package can use conv.To to convert between json.Value and other types.
SPDX-License-Identifier: Apache-2.0
Index ¶
- func BigFloatToBigInt(ival *big.Float, oval **big.Int) error
- func BigFloatToBigRat(ival *big.Float, oval **big.Rat) error
- func BigFloatToFloat32(ival *big.Float, oval *float32) error
- func BigFloatToFloat64(ival *big.Float, oval *float64) error
- func BigFloatToInt64(ival *big.Float, oval *int64) error
- func BigFloatToString(val *big.Float) string
- func BigFloatToUint64(ival *big.Float, oval *uint64) error
- func BigIntToBigFloat(ival *big.Int, oval **big.Float)
- func BigIntToBigRat(ival *big.Int, oval **big.Rat)
- func BigIntToFloat32(ival *big.Int, oval *float32) error
- func BigIntToFloat64(ival *big.Int, oval *float64) error
- func BigIntToInt64(ival *big.Int, oval *int64) error
- func BigIntToString(val *big.Int) string
- func BigIntToUint64(ival *big.Int, oval *uint64) error
- func BigRatToBigFloat(ival *big.Rat, oval **big.Float)
- func BigRatToBigInt(ival *big.Rat, oval **big.Int) error
- func BigRatToFloat32(ival *big.Rat, oval *float32) error
- func BigRatToFloat64(ival *big.Rat, oval *float64) error
- func BigRatToInt64(ival *big.Rat, oval *int64) error
- func BigRatToNormalizedString(val *big.Rat) string
- func BigRatToString(val *big.Rat) string
- func BigRatToUint64(ival *big.Rat, oval *uint64) error
- func FloatStringToBigRat(ival string, oval **big.Rat) error
- func FloatToBigFloat[T constraint.Float](ival T, oval **big.Float) error
- func FloatToBigInt[T constraint.Float](ival T, oval **big.Int) error
- func FloatToBigRat[T constraint.Float](ival T, oval **big.Rat) error
- func FloatToFloat[I constraint.Float, O constraint.Float](ival I, oval *O) error
- func FloatToInt[F constraint.Float, I constraint.SignedInteger](ival F, oval *I) error
- func FloatToString[T constraint.Float](val T) string
- func FloatToUint[F constraint.Float, I constraint.UnsignedInteger](ival F, oval *I) error
- func IntToBigFloat[T constraint.SignedInteger](ival T, oval **big.Float)
- func IntToBigInt[T constraint.SignedInteger](ival T, oval **big.Int)
- func IntToBigRat[T constraint.SignedInteger](ival T, oval **big.Rat)
- func IntToFloat[I constraint.Integer, F constraint.Float](ival I, oval *F) error
- func IntToInt[S constraint.SignedInteger, T constraint.SignedInteger](ival S, oval *T) error
- func IntToString[T constraint.SignedInteger](val T) string
- func IntToUint[I constraint.SignedInteger, U constraint.UnsignedInteger](ival I, oval *U) error
- func LookupConversion(src, tgt goreflect.Type) (func(any, any) error, error)
- func MustBigFloatToBigInt(ival *big.Float, oval **big.Int)
- func MustBigFloatToBigRat(ival *big.Float, oval **big.Rat)
- func MustBigFloatToFloat32(ival *big.Float, oval *float32)
- func MustBigFloatToFloat64(ival *big.Float, oval *float64)
- func MustBigFloatToInt64(ival *big.Float, oval *int64)
- func MustBigFloatToUint64(ival *big.Float, oval *uint64)
- func MustBigIntToFloat32(ival *big.Int, oval *float32)
- func MustBigIntToFloat64(ival *big.Int, oval *float64)
- func MustBigIntToInt64(ival *big.Int, oval *int64)
- func MustBigIntToUint64(ival *big.Int, oval *uint64)
- func MustBigRatToBigInt(ival *big.Rat, oval **big.Int)
- func MustBigRatToFloat32(ival *big.Rat, oval *float32)
- func MustBigRatToFloat64(ival *big.Rat, oval *float64)
- func MustBigRatToInt64(ival *big.Rat, oval *int64)
- func MustBigRatToUint64(ival *big.Rat, oval *uint64)
- func MustFloatStringToBigRat(ival string, oval **big.Rat)
- func MustFloatToBigFloat[T constraint.Float](ival T, oval **big.Float)
- func MustFloatToBigInt[T constraint.Float](ival T, oval **big.Int)
- func MustFloatToBigRat[T constraint.Float](ival T, oval **big.Rat)
- func MustFloatToFloat[I constraint.Float, O constraint.Float](ival I, oval *O)
- func MustFloatToInt[F constraint.Float, I constraint.SignedInteger](ival F, oval *I)
- func MustFloatToUint[F constraint.Float, I constraint.UnsignedInteger](ival F, oval *I)
- func MustIntToFloat[I constraint.Integer, F constraint.Float](ival I, oval *F)
- func MustIntToInt[S constraint.SignedInteger, T constraint.SignedInteger](ival S, oval *T)
- func MustIntToUint[I constraint.SignedInteger, U constraint.UnsignedInteger](ival I, oval *U)
- func MustRegisterConversion[S, T any](convFn func(S, *T) error)
- func MustStringToBigFloat(ival string, oval **big.Float)
- func MustStringToBigInt(ival string, oval **big.Int)
- func MustStringToBigRat(ival string, oval **big.Rat)
- func MustStringToFloat32(ival string, oval *float32)
- func MustStringToFloat64(ival string, oval *float64)
- func MustStringToInt64(ival string, oval *int64)
- func MustStringToUint64(ival string, oval *uint64)
- func MustTo[T any](src any, tgt *T)
- func MustToBigOps[S constraint.Numeric | ~string, T constraint.BigOps[T]](src S, tgt *T)
- func MustUintToInt[U constraint.UnsignedInteger, I constraint.SignedInteger](ival U, oval *I)
- func MustUintToUint[S constraint.UnsignedInteger, T constraint.UnsignedInteger](ival S, oval *T)
- func NumBits[T constraint.Signed | constraint.UnsignedInteger](val T) int
- func ReflectTo(src, tgt goreflect.Value) error
- func RegisterConversion[S, T any](convFn func(S, *T) error) error
- func StringToBigFloat(ival string, oval **big.Float) error
- func StringToBigInt(ival string, oval **big.Int) error
- func StringToBigRat(ival string, oval **big.Rat) error
- func StringToFloat32(ival string, oval *float32) error
- func StringToFloat64(ival string, oval *float64) error
- func StringToInt64(ival string, oval *int64) error
- func StringToUint64(ival string, oval *uint64) error
- func To[T any](src any, tgt *T) error
- func ToBigOps[S constraint.Numeric | ~string, T constraint.BigOps[T]](src S, tgt *T) error
- func UintToBigFloat[T constraint.UnsignedInteger](ival T, oval **big.Float)
- func UintToBigInt[T constraint.UnsignedInteger](ival T, oval **big.Int)
- func UintToBigRat[T constraint.UnsignedInteger](ival T, oval **big.Rat)
- func UintToInt[U constraint.UnsignedInteger, I constraint.SignedInteger](ival U, oval *I) error
- func UintToString[T constraint.UnsignedInteger](val T) string
- func UintToUint[S constraint.UnsignedInteger, T constraint.UnsignedInteger](ival S, oval *T) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BigFloatToBigInt ¶
BigFloatToBigInt converts a *big.Float to a *big.Int. Returns an error if the *big.Float has any fractional digits.
func BigFloatToBigRat ¶
BigFloatToBigRat converts a *big.Float into a *big.Rat
func BigFloatToFloat32 ¶
BigFloatToFloat32 converts a *big.Float to a float32 Returns an error if the *big.Float cannot be represented as a float32
func BigFloatToFloat64 ¶
BigFloatToFloat64 converts a *big.Float to a float64 Returns an error if the *big.Float cannot be represented as a float64
func BigFloatToInt64 ¶
BigFloatToInt64 converts a *big.Float to an int64 Returns an error if the *big.Float cannot be represented as an int64
func BigFloatToString ¶
BigFloatToString converts a *big.Float to a string
func BigFloatToUint64 ¶
BigFloatToUint64 converts a *big.Float to a uint64 Returns an error if the *big.Float cannot be represented as a uint64
func BigIntToBigFloat ¶
BigIntToBigFloat converts a *big.Int into a *big.Float
func BigIntToBigRat ¶
BigIntToBigRat converts a *big.Int into a *big.Rat
func BigIntToFloat32 ¶
BigIntToFloat32 converts a *big.Int to a float32 Returns an error if the *big.Int cannot be represented as a float32
func BigIntToFloat64 ¶
BigIntToFloat64 converts a *big.Int to a float64 Returns an error if the *big.Int cannot be represented as a float64
func BigIntToInt64 ¶
BigIntToInt64 converts a *big.Int to a signed integer Returns an error if the *big.Int cannot be represented as an int64
func BigIntToString ¶
BigIntToString converts a *big.Int to a string
func BigIntToUint64 ¶
BigIntToUint64 converts a *big.Int to a uint64 Returns an error if the *big.Int cannot be represented as a uint64
func BigRatToBigFloat ¶
BigRatToBigFloat converts a *big.Rat to a *big.Float
func BigRatToBigInt ¶
BigRatToBigInt converts a *big.Rat to a *big.Int Returns an error if the *big.Rat is not an int
func BigRatToFloat32 ¶
BigRatToFloat32 converts a *big.Rat to a float32 Returns an error if the *big.Rat cannot be represented as a float32
func BigRatToFloat64 ¶
BigRatToFloat64 converts a *big.Rat to a float64 Returns an error if the *big.Rat cannot be represented as a float64
func BigRatToInt64 ¶
BigRatToInt64 converts a *big.Rat to an int64 Returns an error if the *big.Rat cannot be represented as an int64
func BigRatToNormalizedString ¶
BigRatToNormalizedString converts a *big.Rat to a string. The string will be formatted like an integer if the ratio is an int, else formatted like a float if it is not an int.
func BigRatToString ¶
BigRatToString converts a *big.Rat to a string. The string will be a ratio like 5/4, if it is int it will be a ratio like 5/1.
func BigRatToUint64 ¶
BigRatToUint64 converts a *big.Rat to a uint64 Returns an error if the *big.Rat cannot be represented as a uint64
func FloatStringToBigRat ¶
FloatStringToBigRat converts a float string to a *big.Rat. Unlike StringToBigRat, it will not accept a ratio string like 5/4.
func FloatToBigFloat ¶
func FloatToBigFloat[T constraint.Float](ival T, oval **big.Float) error
FloatToBigFloat converts any float type into a *big.Float
func FloatToBigInt ¶
func FloatToBigInt[T constraint.Float](ival T, oval **big.Int) error
FloatToBigInt converts any float type to a *big.Int Returns an error if the float has fractional digits
func FloatToBigRat ¶
func FloatToBigRat[T constraint.Float](ival T, oval **big.Rat) error
FloatToBigRat converts any float type into a *big.Rat
func FloatToFloat ¶
func FloatToFloat[I constraint.Float, O constraint.Float](ival I, oval *O) error
FloatToFloat converts a float32 or float64 to a float32 or float64 Returns an error if the float64 is outside the range of a float32
func FloatToInt ¶
func FloatToInt[F constraint.Float, I constraint.SignedInteger](ival F, oval *I) error
FloatToInt converts and float type to any signed int type Returns an error if the float value cannot be represented by the int type
func FloatToString ¶
func FloatToString[T constraint.Float](val T) string
FloatToString converts any float type into a string
func FloatToUint ¶
func FloatToUint[F constraint.Float, I constraint.UnsignedInteger](ival F, oval *I) error
FloatToUint converts and float type to any unsigned int type Returns an error if the float value cannot be represented by the unsigned int type
func IntToBigFloat ¶
func IntToBigFloat[T constraint.SignedInteger](ival T, oval **big.Float)
IntToBigFloat converts any signed int type into a *big.Float
func IntToBigInt ¶
func IntToBigInt[T constraint.SignedInteger](ival T, oval **big.Int)
IntToBigInt converts any signed int type into a *big.Int
func IntToBigRat ¶
func IntToBigRat[T constraint.SignedInteger](ival T, oval **big.Rat)
IntToBigRat converts any signed int type into a *big.Rat
func IntToFloat ¶
func IntToFloat[I constraint.Integer, F constraint.Float](ival I, oval *F) error
IntToFloat converts any kind of signed or unssigned integer into any kind of float. Returns an error if the int value cannot be exactly represented without rounding.
func IntToInt ¶
func IntToInt[S constraint.SignedInteger, T constraint.SignedInteger](ival S, oval *T) error
IntToInt converts any signed integer type into any signed integer type Returns an error if the source value cannot be represented by the target type
func IntToString ¶
func IntToString[T constraint.SignedInteger](val T) string
IntToString converts any signed int type into a string
func IntToUint ¶
func IntToUint[I constraint.SignedInteger, U constraint.UnsignedInteger](ival I, oval *U) error
IntToUint converts any signed integer type into any unsigned integer type Returns an error if the signed int cannot be represented by the unsigned type
func LookupConversion ¶
LookupConversion looks for a conversion from a source type to a target type.
It is an error if either type is more than one pointer, or a uintptr, chan, func, or unsafe pointer. If the source and target types are the same, a conversion function that just copies the source to the target is returned. The source type may be of the following forms, where T represents any accepted value type: - T - *T - Maybe[T] - Maybe[*T]
The target types are the same as the source types. For any of the above forms, T may be a primitive sub type (eg, type subint int). In such cases, up to four lookups are performed (first match is used): - A conversion using the src sub type and tgt sub type - A conversion using the src base type and tgt sub type - A conversion using the src sub type and tgt base type - A conversion using the src base type and tgt base type
Example lookups, listing possible conversions in search order (shown without the extra * the target type has to have): - int to string -> int to string - subint to *string -> subint to string, int to string - Maybe[int] to string -> int to string - int to int -> copy - subint to Maybe[*int] -> subint to *int, subint to int, copy
Additionally, other packages can register custom functions to test if an instance of a type is effectively nil, similar to an empty Maybe. The returned conversion will do a precheck to see if the src value is effectively nil, and if so, proceed as if it is nil.
This function returns func, error: If a conversion is found (or it is a copy) : returns func, nil If a conversion is not found : returns nil, nil Conversion is not allowed : returns nil, err
func MustBigFloatToBigInt ¶
MustBigFloatToBigInt is a Must version of BigFloatToBigInt
func MustBigFloatToBigRat ¶
MustBigFloatToBigRat is a Must version of FloatToBigRat
func MustBigFloatToFloat32 ¶
MustBigFloatToFloat32 is a Must version of BigFloatToFloat32
func MustBigFloatToFloat64 ¶
MustBigFloatToFloat64 is a Must version of BigFloatToFloat64
func MustBigFloatToInt64 ¶
MustBigFloatToInt64 is a Must version of BigFloatToInt64
func MustBigFloatToUint64 ¶
MustBigFloatToUint64 is a Must version of BigFloatToUint64
func MustBigIntToFloat32 ¶
MustBigIntToFloat32 is a Must version of BigIntToFloat32
func MustBigIntToFloat64 ¶
MustBigIntToFloat64 is a Must version of BigIntToFloat64
func MustBigIntToInt64 ¶
MustBigIntToInt64 is a Must version of BigIntToInt64
func MustBigIntToUint64 ¶
MustBigIntToUint64 is a Must version of BigIntToUint64
func MustBigRatToBigInt ¶
MustBigRatToBigInt is a Must version of BigFloatToBigInt
func MustBigRatToFloat32 ¶
MustBigRatToFloat32 is a Must version of BigRatToFloat32
func MustBigRatToFloat64 ¶
MustBigRatToFloat64 is a Must version of BigRatToFloat64
func MustBigRatToInt64 ¶
MustBigRatToInt64 is a Must version of BigRatToInt64
func MustBigRatToUint64 ¶
MustBigRatToUint64 is a Must version of BigRatToUint64
func MustFloatStringToBigRat ¶
MustFloatStringToBigRat is a Must version of FloatStringToBigRat
func MustFloatToBigFloat ¶
func MustFloatToBigFloat[T constraint.Float](ival T, oval **big.Float)
MustFloatToBigFloat is a Must version of FloatToBigFloat
func MustFloatToBigInt ¶
func MustFloatToBigInt[T constraint.Float](ival T, oval **big.Int)
MustFloatToBigInt is a Must version of FloatToBigInt
func MustFloatToBigRat ¶
func MustFloatToBigRat[T constraint.Float](ival T, oval **big.Rat)
MustFloatToBigRat is a Must version of FloatToBigRat
func MustFloatToFloat ¶
func MustFloatToFloat[I constraint.Float, O constraint.Float](ival I, oval *O)
MustFloatToFloat is a Must version of FloatToFloat
func MustFloatToInt ¶
func MustFloatToInt[F constraint.Float, I constraint.SignedInteger](ival F, oval *I)
MustFloatToInt is a Must version of FloatToInt
func MustFloatToUint ¶
func MustFloatToUint[F constraint.Float, I constraint.UnsignedInteger](ival F, oval *I)
MustFloatToUint is a Must version of FloatToUint
func MustIntToFloat ¶
func MustIntToFloat[I constraint.Integer, F constraint.Float](ival I, oval *F)
MustIntToFloat is a Must version of IntToFloat
func MustIntToInt ¶
func MustIntToInt[S constraint.SignedInteger, T constraint.SignedInteger](ival S, oval *T)
MustIntToInt is a Must version of IntToInt
func MustIntToUint ¶
func MustIntToUint[I constraint.SignedInteger, U constraint.UnsignedInteger](ival I, oval *U)
MustIntToUint is a Must version of IntToUint
func MustRegisterConversion ¶
MustRegisterConversion is a must version of RegisterConversion
func MustStringToBigFloat ¶
MustStringToBigFloat is a Must version of FloatToBigFloat
func MustStringToBigInt ¶
MustStringToBigInt is a Must version of StringToBigInt
func MustStringToBigRat ¶
MustStringToBigRat is a Must version of StringToBigRat
func MustStringToFloat32 ¶
MustStringToFloat32 is a Must version of StringToFloat32
func MustStringToFloat64 ¶
MustStringToFloat64 is a Must version of StringToFloat64
func MustStringToInt64 ¶
MustStringToInt64 is a Must version of StringToInt64
func MustStringToUint64 ¶
MustStringToUint64 is a Must version of StringToUint64
func MustToBigOps ¶
func MustToBigOps[S constraint.Numeric | ~string, T constraint.BigOps[T]](src S, tgt *T)
MustToBigOps is a Must version of ToBigOps
func MustUintToInt ¶
func MustUintToInt[U constraint.UnsignedInteger, I constraint.SignedInteger](ival U, oval *I)
MustUintToInt is a Must version of UintToInt
func MustUintToUint ¶
func MustUintToUint[S constraint.UnsignedInteger, T constraint.UnsignedInteger](ival S, oval *T)
MustUintToInt is a Must version of UintToInt
func NumBits ¶
func NumBits[T constraint.Signed | constraint.UnsignedInteger](val T) int
NumBits provides the number of bits of any integer or float type
func ReflectTo ¶
ReflectTo uses reflection objects to convert from source to target. This function is useful for reflection algorithms that need to do conversions. The tgt must wrap a pointer.
func RegisterConversion ¶
RegisterConversion registers a conversion from a value of type S to a value of type T. Note the conversion function must accept a pointer type for the target. If the target is already a pointer type, an additional level of pointer is required.
Examples: RegisterConversion(0, Foo{}, func(int, *Foo) error {...}) RegisterConversion((*int)(nil), (*Foo)(nil), func(*int, **Foo) error {...})
See LookupConversion for details
func StringToBigFloat ¶
StringToBigFloat converts a string to a *big.Float Returns an error if the string is not a valid float string
func StringToBigInt ¶
StringtoBigInt converts a string to a *big.Int. Returns an error if the string is not an integer.
func StringToBigRat ¶
StringToBigRat converts a string into a *big.Rat
func StringToFloat32 ¶
StringToFloat32 converts a string to a float32 Returns an error if the string cannot be represented as a float32
func StringToFloat64 ¶
StringToFloat64 converts a string to a float64 Returns an error if the string cannot be represented as a float64
func StringToInt64 ¶
StringToInt64 converts a string to an int64 Returns an error if the string cannot be represented as an int64
func StringToUint64 ¶
StringToUint64 converts a string to a uint64 Returns an error if the string cannot be represented as a uint64
func To ¶
To converts any supported combination of source and target types.
The actual conversion is performed by: - functions declared in this source file - functions registered by other packages in this library - functions registered by other packages outside this library
The source is typed any for two reasons: - to allow for cases where the caller accepts type any - arbitrary new conversions can be registered (ideally via an init function)
The target is a *T because Go can infer generic parameters, but not generic return types. So instead of writing this:
var str = conv.To[int, string](0)
We write this:
var str string conv.To(0, &str)
It is a design choice to not make the user constantly repeat generic types for every conversion.
See LookupConversion for the algorithm to find a registered conversion function. There are 4 cases: 1. LookupConversion does not find a conversion, the conversion is allowable, the types are the same
- The source value is copied to the target
- If the source is a pointer, the target gets a copy of the pointer, so source and target point to same address
2. LookupConversion does not find a conversion, the conversion is allowable, the types are different
- Returns error that source cannot be converted to target
3. LookupConversion returns an error
- The error is returned as is
4. LookupConversion finds a conversion:
- The conversion is applied
func ToBigOps ¶
func ToBigOps[S constraint.Numeric | ~string, T constraint.BigOps[T]](src S, tgt *T) error
ToBigOps is the BigOps version of To
func UintToBigFloat ¶
func UintToBigFloat[T constraint.UnsignedInteger](ival T, oval **big.Float)
UintToBigFloat converts any unsigned int type into a *big.Float
func UintToBigInt ¶
func UintToBigInt[T constraint.UnsignedInteger](ival T, oval **big.Int)
UintToBigInt converts any unsigned int type into a *big.Int
func UintToBigRat ¶
func UintToBigRat[T constraint.UnsignedInteger](ival T, oval **big.Rat)
UintToBigRat converts any unsigned int type into a *big.Rat
func UintToInt ¶
func UintToInt[U constraint.UnsignedInteger, I constraint.SignedInteger](ival U, oval *I) error
UintToInt converts any unsigned integer type into any signed integer type Returns an error if the unsigned int cannot be represented by the signed type
func UintToString ¶
func UintToString[T constraint.UnsignedInteger](val T) string
UintToString converts any unsigned int type into a string
func UintToUint ¶
func UintToUint[S constraint.UnsignedInteger, T constraint.UnsignedInteger](ival S, oval *T) error
UintToUint converts any unsigned integer type into any unsigned integer type Returns an error if the source value cannot be represented by the target type
Types ¶
This section is empty.