safecast

package
v1.154.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: Apache-2.0 Imports: 3 Imported by: 1

README

Safecast

the purpose of this utilities is to perform safe number conversion in go similarly to go-safecast from which they are inspired from. It should help tackling gosec G115 rule

G115: Potential overflow when converting between integer types.

and CWE-190

infinite loop
access to wrong resource by id
grant access to someone who exhausted their quota

Contrary to go-safecast no error is returned when attempting casting and the MAX or MIN value of the type is returned instead if the value is beyond the allowed window. For instance, toInt8(255)-> 127 and toInt8(-255)-> -128

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cast added in v1.154.0

func Cast[F, T IConvertible](i F, f CastFunc[F, T]) T

Cast converts an IConvertible value to another IConvertible type using the supplied conversion function.

func CastRef added in v1.154.0

func CastRef[F, T IConvertible](i *F, f CastFunc[F, T]) *T

CastRef converts a reference to an IConvertible value to a reference to another IConvertible type using the supplied conversion function. A nil input returns nil.

func RobustCast added in v1.154.0

func RobustCast[F, T IConvertible](i F) (c T, err error)

RobustCast converts an IConvertible value to the target type T by dispatching to the appropriate safe helper for T.

Conversions to bounded integer and float32 types use the corresponding helper, so out-of-range values are clamped to the nearest valid boundary. If types are not supported, an error is returned.

func RobustCastRef added in v1.154.0

func RobustCastRef[F, T IConvertible](i *F) (c *T, err error)

RobustCastRef converts a reference to an IConvertible value to a reference to the target type T by dispatching to the appropriate safe helper for T. A nil input returns nil. If types are not supported, an error is returned.

func ToFloat32 added in v1.154.0

func ToFloat32[C IConvertible](i C) float32

ToFloat32 attempts to convert an IConvertible value to a float32. If the converted value falls outside the range of float32, the nearest boundary value is returned instead.

func ToFloat32Ref added in v1.154.0

func ToFloat32Ref[C IConvertible](i *C) *float32

ToFloat32Ref attempts to convert a reference to an IConvertible value to a reference to a float32. A nil input returns nil.

func ToFloat64 added in v1.147.0

func ToFloat64[C IConvertible](i C) float64

ToFloat64 attempts to convert an IConvertible value to a float64.

func ToFloat64Ref added in v1.154.0

func ToFloat64Ref[C IConvertible](i *C) *float64

ToFloat64Ref attempts to convert a reference to an IConvertible value to a reference to a float64. A nil input returns nil.

func ToInt

func ToInt[C IConvertible](i C) int

ToInt attempts to convert an IConvertible value to an int. If the converted value falls outside the range of int, the nearest boundary value is returned instead.

func ToInt8

func ToInt8[C IConvertible](i C) int8

ToInt8 attempts to convert an IConvertible value to an int8. If the converted value falls outside the range of int8, the nearest boundary value is returned instead.

func ToInt8Ref added in v1.154.0

func ToInt8Ref[C IConvertible](i *C) *int8

ToInt8Ref attempts to convert a reference to an IConvertible value to a reference to an int8. A nil input returns nil.

func ToInt16

func ToInt16[C IConvertible](i C) int16

ToInt16 attempts to convert an IConvertible value to an int16. If the converted value falls outside the range of int16, the nearest boundary value is returned instead.

func ToInt16Ref added in v1.154.0

func ToInt16Ref[C IConvertible](i *C) *int16

ToInt16Ref attempts to convert a reference to an IConvertible value to a reference to an int16. A nil input returns nil.

func ToInt32

func ToInt32[C IConvertible](i C) int32

ToInt32 attempts to convert an IConvertible value to an int32. If the converted value falls outside the range of int32, the nearest boundary value is returned instead.

func ToInt32Ref added in v1.154.0

func ToInt32Ref[C IConvertible](i *C) *int32

ToInt32Ref attempts to convert a reference to an IConvertible value to a reference to an int32. A nil input returns nil.

func ToInt64

func ToInt64[C IConvertible](i C) int64

ToInt64 attempts to convert an IConvertible value to an int64. If the converted value falls outside the range of int64, the nearest boundary value is returned instead.

func ToInt64Ref added in v1.154.0

func ToInt64Ref[C IConvertible](i *C) *int64

ToInt64Ref attempts to convert a reference to an IConvertible value to a reference to an int64. A nil input returns nil.

func ToIntRef added in v1.154.0

func ToIntRef[C IConvertible](i *C) *int

ToIntRef attempts to convert a reference to an IConvertible value to a reference to an int. A nil input returns nil.

func ToUint

func ToUint[C IConvertible](i C) uint

ToUint attempts to convert an IConvertible value to a uint. If the converted value falls outside the range of uint, the nearest boundary value is returned instead.

func ToUint8

func ToUint8[C IConvertible](i C) uint8

ToUint8 attempts to convert an IConvertible value to a uint8. If the converted value falls outside the range of uint8, the nearest boundary value is returned instead.

func ToUint8Ref added in v1.154.0

func ToUint8Ref[C IConvertible](i *C) *uint8

ToUint8Ref attempts to convert a reference to an IConvertible value to a reference to a uint8. A nil input returns nil.

func ToUint16

func ToUint16[C IConvertible](i C) uint16

ToUint16 attempts to convert an IConvertible value to a uint16. If the converted value falls outside the range of uint16, the nearest boundary value is returned instead.

func ToUint16Ref added in v1.154.0

func ToUint16Ref[C IConvertible](i *C) *uint16

ToUint16Ref attempts to convert a reference to an IConvertible value to a reference to a uint16. A nil input returns nil.

func ToUint32

func ToUint32[C IConvertible](i C) uint32

ToUint32 attempts to convert an IConvertible value to a uint32. If the converted value falls outside the range of uint32, the nearest boundary value is returned instead.

func ToUint32Ref added in v1.154.0

func ToUint32Ref[C IConvertible](i *C) *uint32

ToUint32Ref attempts to convert a reference to an IConvertible value to a reference to a uint32. A nil input returns nil.

func ToUint64

func ToUint64[C IConvertible](i C) uint64

ToUint64 attempts to convert an IConvertible value to a uint64. If the converted value falls outside the range of uint64, the nearest boundary value is returned instead.

func ToUint64Ref added in v1.154.0

func ToUint64Ref[C IConvertible](i *C) *uint64

ToUint64Ref attempts to convert a reference to an IConvertible value to a reference to a uint64. A nil input returns nil.

func ToUintRef added in v1.154.0

func ToUintRef[C IConvertible](i *C) *uint

ToUintRef attempts to convert a reference to an IConvertible value to a reference to a uint. A nil input returns nil.

Types

type CastFunc added in v1.154.0

type CastFunc[F, T IConvertible] func(F) T

CastFunc defines a function that converts a value of type F to type T, where both types satisfy IConvertible.

type IConvertable deprecated

type IConvertable = IConvertible

Deprecated: IConvertable is an alias for IConvertible. Use IConvertible instead

type IConvertible added in v1.154.0

type IConvertible interface {
	INumber
}

IConvertible is an alias for everything that can be converted

type IFloat

type IFloat interface {
	~float32 | ~float64
}

IFloat is an alias for the float32 and float64 types.

type IInteger

type IInteger interface {
	ISignedInteger | IUnsignedInteger
}

IInteger is an alias for the all unsigned and signed integers

type INumber

type INumber interface {
	IInteger | IFloat
}

INumber is an alias for all integers and floats

type ISignedInteger

type ISignedInteger interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

ISignedInteger is an alias for all signed integers: int, int8, int16, int32, and int64 types.

type IUnsignedInteger

type IUnsignedInteger interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

IUnsignedInteger is an alias for all unsigned integers: uint, uint8, uint16, uint32, and uint64 types.

Jump to

Keyboard shortcuts

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