as

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2022 License: BSD-3-Clause Imports: 6 Imported by: 2

README

As

Build Status Go Report Card Maintainability codecov

As is a library to convert numeric types with overflow check in Go.

Instalation

go get -u github.com/lunemec/as

Example

package main

import (
    "fmt"

    "github.com/lunemec/as"
)

func main() {
	for _, n := range []int{127, 128} {
		num, err := as.Int8(n)
		if err != nil {
			fmt.Printf("Input invalid: %d, err: %s\n", num, err)
		} else {
			fmt.Printf("Input valid: %d\n", num)
		}
	}
	// Output: Input valid: 127
	// Input invalid: -128, err: 128 (int) overflows int8
}

Example using generics

package main

import (
    "fmt"

    "github.com/lunemec/as"
)

func main() {
	for _, n := range []int{127, 128} {
		num, err := as.T[int8](n)
		if err != nil {
			fmt.Printf("Input invalid: %d, err: %s\n", num, err)
		} else {
			fmt.Printf("Input valid: %d\n", num)
		}
	}
	// Output: Input valid: 127
	// Input invalid: -128, err: 128 (int) overflows int8
}

Example slices type conversion with overflow check

package main

import (
    "fmt"

    "github.com/lunemec/as"
)

func main() {
	out, err := as.SliceT[int, int8]([]int{127, 128})
	fmt.Printf("Output: %+v, error: %+v\n", out, err)
	// Output: Output: [127 0], error: 1 error occurred:
	// 	* at index [1]: 128 (int) overflows int8
}

Other considerations

Note this checking is not free, check benchmarks for each checked cast.

There are several Go proposals to have this functionality in the language but many of them even predate this library.

There are also libraries that allow for math operations while checking for overflow at the output.

Architecture support

As currently supports these architectures and correctly handles overflows for 64/32 bit numbers:

  • 386
  • amd64
  • arm
  • arm64

It may work for s390x or ppc64le, but I have no way to test that.

Documentation

Overview

Package as provides a easy way to convert numeric types with overflow check.

Example
package main

import (
	"fmt"

	"github.com/lunemec/as"
)

func main() {
	for _, n := range []int{127, 128} {
		num, err := as.Int8(n)
		if err != nil {
			fmt.Printf("Input invalid: %d, err: %s\n", num, err)
		} else {
			fmt.Printf("Input valid: %d\n", num)
		}
	}
}
Output:

Input valid: 127
Input invalid: -128, err: 128 (int) overflows int8

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Int

func Int(v interface{}) (int, error)

Int converts any numeric type to int, and returns error if there was overflow.

func Int16

func Int16(v interface{}) (int16, error)

Int16 converts any numeric type to int16, and returns error if there was overflow.

func Int32

func Int32(v interface{}) (int32, error)

Int32 converts any numeric type to int32, and returns error if there was overflow.

func Int64

func Int64(v interface{}) (int64, error)

Int64 converts any numeric type to int64, and returns error if there was overflow.

func Int8

func Int8(v interface{}) (int8, error)

Int8 converts any numeric type to int8, and returns error if there was overflow.

func SliceT added in v1.1.0

func SliceT[From Number, To Number](src []From) ([]To, error)

SliceT is generic function to convert between slices of numeric types while checking for overflow on each item. NOTE: this function is quite slow, and especially when high % of numbers overflow, the allocations are significant.

Example
package main

import (
	"fmt"

	"github.com/lunemec/as"
)

func main() {
	out, err := as.SliceT[int, int8]([]int{127, 128})
	fmt.Printf("Output: %+v, error: %+v\n", out, err)
}
Output:

Output: [127 0], error: 1 error occurred:
	* at index [1]: 128 (int) overflows int8

func SliceTUnchecked added in v1.1.0

func SliceTUnchecked[From SliceNumber, To SliceNumber](src []From) []To

SliceTUnchecked is generic function to convert between slices of any numeric type without overflow check.

Example
package main

import (
	"fmt"

	"github.com/lunemec/as"
)

func main() {
	out := as.SliceTUnchecked[int, int8]([]int{127, 128})
	fmt.Printf("Output: %+v\n", out)
}
Output:

Output: [127 -128]

func T added in v1.1.0

func T[To Number](v any) (To, error)

T is generic function to allow for easier checked type cast from any type to any Number type.

Example
package main

import (
	"fmt"

	"github.com/lunemec/as"
)

func main() {
	for _, n := range []int{127, 128} {
		num, err := as.T[int8](n)
		if err != nil {
			fmt.Printf("Input invalid: %d, err: %s\n", num, err)
		} else {
			fmt.Printf("Input valid: %d\n", num)
		}
	}
}
Output:

Input valid: 127
Input invalid: -128, err: 128 (int) overflows int8

func Uint

func Uint(v interface{}) (uint, error)

Uint converts any numeric type to uint64, and returns error if there was overflow.

func Uint16

func Uint16(v interface{}) (uint16, error)

Uint16 converts any numeric type to uint16, and returns error if there was overflow.

func Uint32

func Uint32(v interface{}) (uint32, error)

Uint32 converts any numeric type to uint32, and returns error if there was overflow.

func Uint64

func Uint64(v interface{}) (uint64, error)

Uint64 converts any numeric type to uint64, and returns error if there was overflow.

func Uint8

func Uint8(v interface{}) (uint8, error)

Uint8 converts any numeric type to uint8, and returns error if there was overflow.

Types

type InvalidTypeError

type InvalidTypeError struct {
	ToType string
	Value  interface{}
}

InvalidTypeError is returned when user supplied invalid type to convert.

func (InvalidTypeError) Error

func (e InvalidTypeError) Error() string

type Number added in v1.1.0

type Number interface {
	constraints.Integer
}

Number constraint is used for type cast into any number. Only Integers for now until float support is added.

type OverflowError

type OverflowError struct {
	ToType string
	Value  interface{}
}

OverflowError is returned when given value overflows maximum number a type can hold.

func (OverflowError) Error

func (e OverflowError) Error() string

type SliceNumber added in v1.1.0

type SliceNumber interface {
	constraints.Integer | constraints.Float
}

SliceNumber is type constraint for SliceTUnchecked

Jump to

Keyboard shortcuts

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