conv

package
v0.0.0-...-02bf512 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func BigFloatToBigInt

func BigFloatToBigInt(ival *big.Float, oval **big.Int) error

BigFloatToBigInt converts a *big.Float to a *big.Int. Returns an error if the *big.Float has any fractional digits.

func BigFloatToBigRat

func BigFloatToBigRat(ival *big.Float, oval **big.Rat) error

BigFloatToBigRat converts a *big.Float into a *big.Rat

func BigFloatToFloat32

func BigFloatToFloat32(ival *big.Float, oval *float32) error

BigFloatToFloat32 converts a *big.Float to a float32 Returns an error if the *big.Float cannot be represented as a float32

func BigFloatToFloat64

func BigFloatToFloat64(ival *big.Float, oval *float64) error

BigFloatToFloat64 converts a *big.Float to a float64 Returns an error if the *big.Float cannot be represented as a float64

func BigFloatToInt64

func BigFloatToInt64(ival *big.Float, oval *int64) error

BigFloatToInt64 converts a *big.Float to an int64 Returns an error if the *big.Float cannot be represented as an int64

func BigFloatToString

func BigFloatToString(val *big.Float) string

BigFloatToString converts a *big.Float to a string

func BigFloatToUint64

func BigFloatToUint64(ival *big.Float, oval *uint64) error

BigFloatToUint64 converts a *big.Float to a uint64 Returns an error if the *big.Float cannot be represented as a uint64

func BigIntToBigFloat

func BigIntToBigFloat(ival *big.Int, oval **big.Float)

BigIntToBigFloat converts a *big.Int into a *big.Float

func BigIntToBigRat

func BigIntToBigRat(ival *big.Int, oval **big.Rat)

BigIntToBigRat converts a *big.Int into a *big.Rat

func BigIntToFloat32

func BigIntToFloat32(ival *big.Int, oval *float32) error

BigIntToFloat32 converts a *big.Int to a float32 Returns an error if the *big.Int cannot be represented as a float32

func BigIntToFloat64

func BigIntToFloat64(ival *big.Int, oval *float64) error

BigIntToFloat64 converts a *big.Int to a float64 Returns an error if the *big.Int cannot be represented as a float64

func BigIntToInt64

func BigIntToInt64(ival *big.Int, oval *int64) error

BigIntToInt64 converts a *big.Int to a signed integer Returns an error if the *big.Int cannot be represented as an int64

func BigIntToString

func BigIntToString(val *big.Int) string

BigIntToString converts a *big.Int to a string

func BigIntToUint64

func BigIntToUint64(ival *big.Int, oval *uint64) error

BigIntToUint64 converts a *big.Int to a uint64 Returns an error if the *big.Int cannot be represented as a uint64

func BigRatToBigFloat

func BigRatToBigFloat(ival *big.Rat, oval **big.Float)

BigRatToBigFloat converts a *big.Rat to a *big.Float

func BigRatToBigInt

func BigRatToBigInt(ival *big.Rat, oval **big.Int) error

BigRatToBigInt converts a *big.Rat to a *big.Int Returns an error if the *big.Rat is not an int

func BigRatToFloat32

func BigRatToFloat32(ival *big.Rat, oval *float32) error

BigRatToFloat32 converts a *big.Rat to a float32 Returns an error if the *big.Rat cannot be represented as a float32

func BigRatToFloat64

func BigRatToFloat64(ival *big.Rat, oval *float64) error

BigRatToFloat64 converts a *big.Rat to a float64 Returns an error if the *big.Rat cannot be represented as a float64

func BigRatToInt64

func BigRatToInt64(ival *big.Rat, oval *int64) error

BigRatToInt64 converts a *big.Rat to an int64 Returns an error if the *big.Rat cannot be represented as an int64

func BigRatToNormalizedString

func BigRatToNormalizedString(val *big.Rat) string

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

func BigRatToString(val *big.Rat) string

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

func BigRatToUint64(ival *big.Rat, oval *uint64) error

BigRatToUint64 converts a *big.Rat to a uint64 Returns an error if the *big.Rat cannot be represented as a uint64

func FloatStringToBigRat

func FloatStringToBigRat(ival string, oval **big.Rat) error

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

func LookupConversion(src, tgt goreflect.Type) (func(any, any) error, error)

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

func MustBigFloatToBigInt(ival *big.Float, oval **big.Int)

MustBigFloatToBigInt is a Must version of BigFloatToBigInt

func MustBigFloatToBigRat

func MustBigFloatToBigRat(ival *big.Float, oval **big.Rat)

MustBigFloatToBigRat is a Must version of FloatToBigRat

func MustBigFloatToFloat32

func MustBigFloatToFloat32(ival *big.Float, oval *float32)

MustBigFloatToFloat32 is a Must version of BigFloatToFloat32

func MustBigFloatToFloat64

func MustBigFloatToFloat64(ival *big.Float, oval *float64)

MustBigFloatToFloat64 is a Must version of BigFloatToFloat64

func MustBigFloatToInt64

func MustBigFloatToInt64(ival *big.Float, oval *int64)

MustBigFloatToInt64 is a Must version of BigFloatToInt64

func MustBigFloatToUint64

func MustBigFloatToUint64(ival *big.Float, oval *uint64)

MustBigFloatToUint64 is a Must version of BigFloatToUint64

func MustBigIntToFloat32

func MustBigIntToFloat32(ival *big.Int, oval *float32)

MustBigIntToFloat32 is a Must version of BigIntToFloat32

func MustBigIntToFloat64

func MustBigIntToFloat64(ival *big.Int, oval *float64)

MustBigIntToFloat64 is a Must version of BigIntToFloat64

func MustBigIntToInt64

func MustBigIntToInt64(ival *big.Int, oval *int64)

MustBigIntToInt64 is a Must version of BigIntToInt64

func MustBigIntToUint64

func MustBigIntToUint64(ival *big.Int, oval *uint64)

MustBigIntToUint64 is a Must version of BigIntToUint64

func MustBigRatToBigInt

func MustBigRatToBigInt(ival *big.Rat, oval **big.Int)

MustBigRatToBigInt is a Must version of BigFloatToBigInt

func MustBigRatToFloat32

func MustBigRatToFloat32(ival *big.Rat, oval *float32)

MustBigRatToFloat32 is a Must version of BigRatToFloat32

func MustBigRatToFloat64

func MustBigRatToFloat64(ival *big.Rat, oval *float64)

MustBigRatToFloat64 is a Must version of BigRatToFloat64

func MustBigRatToInt64

func MustBigRatToInt64(ival *big.Rat, oval *int64)

MustBigRatToInt64 is a Must version of BigRatToInt64

func MustBigRatToUint64

func MustBigRatToUint64(ival *big.Rat, oval *uint64)

MustBigRatToUint64 is a Must version of BigRatToUint64

func MustFloatStringToBigRat

func MustFloatStringToBigRat(ival string, oval **big.Rat)

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

func MustRegisterConversion[S, T any](convFn func(S, *T) error)

MustRegisterConversion is a must version of RegisterConversion

func MustStringToBigFloat

func MustStringToBigFloat(ival string, oval **big.Float)

MustStringToBigFloat is a Must version of FloatToBigFloat

func MustStringToBigInt

func MustStringToBigInt(ival string, oval **big.Int)

MustStringToBigInt is a Must version of StringToBigInt

func MustStringToBigRat

func MustStringToBigRat(ival string, oval **big.Rat)

MustStringToBigRat is a Must version of StringToBigRat

func MustStringToFloat32

func MustStringToFloat32(ival string, oval *float32)

MustStringToFloat32 is a Must version of StringToFloat32

func MustStringToFloat64

func MustStringToFloat64(ival string, oval *float64)

MustStringToFloat64 is a Must version of StringToFloat64

func MustStringToInt64

func MustStringToInt64(ival string, oval *int64)

MustStringToInt64 is a Must version of StringToInt64

func MustStringToUint64

func MustStringToUint64(ival string, oval *uint64)

MustStringToUint64 is a Must version of StringToUint64

func MustTo

func MustTo[T any](src any, tgt *T)

MustTo is a Must version of To

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

func ReflectTo(src, tgt goreflect.Value) error

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

func RegisterConversion[S, T any](convFn func(S, *T) error) error

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

func StringToBigFloat(ival string, oval **big.Float) error

StringToBigFloat converts a string to a *big.Float Returns an error if the string is not a valid float string

func StringToBigInt

func StringToBigInt(ival string, oval **big.Int) error

StringtoBigInt converts a string to a *big.Int. Returns an error if the string is not an integer.

func StringToBigRat

func StringToBigRat(ival string, oval **big.Rat) error

StringToBigRat converts a string into a *big.Rat

func StringToFloat32

func StringToFloat32(ival string, oval *float32) error

StringToFloat32 converts a string to a float32 Returns an error if the string cannot be represented as a float32

func StringToFloat64

func StringToFloat64(ival string, oval *float64) error

StringToFloat64 converts a string to a float64 Returns an error if the string cannot be represented as a float64

func StringToInt64

func StringToInt64(ival string, oval *int64) error

StringToInt64 converts a string to an int64 Returns an error if the string cannot be represented as an int64

func StringToUint64

func StringToUint64(ival string, oval *uint64) error

StringToUint64 converts a string to a uint64 Returns an error if the string cannot be represented as a uint64

func To

func To[T any](src any, tgt *T) error

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.

Jump to

Keyboard shortcuts

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