Skip to content

Commit

Permalink
doc: update documentation as errors messages changed
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoVeille committed Sep 9, 2024
1 parent 25e698e commit de17f87
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 15 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
[![codecov](https://codecov.io/gh/ccoVeille/go-safecast/graph/badge.svg?token=VW0VO503U6)](https://codecov.io/gh/ccoVeille/go-safecast)
[![Code Climate](https://codeclimate.com/github/ccoVeille/go-safecast.png)](https://codeclimate.com/github/ccoVeille/go-safecast)

## Project purpose
go-safecast solves the type conversion issues in Go

In Go, integer type conversion can lead to a silent and unexpected behavior and errors if not handled carefully.

This package is made to help to convert any number to another, and report an error when if there would be a [loss or overflow in the conversion](#conversion-overflows)
This package helps to convert any number to another, and report an error when if there would be a [loss or overflow in the conversion](#conversion-issues)

## Usage

Expand All @@ -35,31 +35,31 @@ func main() {
// when there is an overflow
//
fmt.Println(safecast.ToInt8(float64(20000)))
// Output: 0 conversion issue: 20000 is greater than 127
// Output: 0 conversion issue: 20000.0 (float64) is greater than 127 (int8): maximum value for this type exceeded
fmt.Println(safecast.ToUint8(int64(-1)))
// Output: 0 conversion issue: -1 is negative
// Output: 0 conversion issue: -1 (int64) is smaller than 0 (uint8): minimum value for this type exceeded
fmt.Println(safecast.ToInt16(int32(40000)))
// Output: 0 conversion issue: 40000 is greater than 32767
// Output: 0 conversion issue: 40000 (int32) is greater than 32767 (int16): maximum value for this type exceeded
fmt.Println(safecast.ToUint16(int64(-1)))
// Output: 0 conversion issue: -1 is negative
// Output: 0 conversion issue: -1 (int64) is smaller than 0 (uint16): minimum value for this type exceeded
fmt.Println(safecast.ToInt32(math.MaxUint32 + 1))
// Output: 0 conversion issue: 4294967296 is greater than 2147483647
// Output: 0 conversion issue: 4294967296 (int) is greater than 2147483647 (int32): maximum value for this type exceeded
fmt.Println(safecast.ToUint32(int64(-1)))
// Output: 0 conversion issue: -1 is negative
// Output: 0 conversion issue: -1 (int64) is smaller than 0 (uint32): minimum value for this type exceeded
fmt.Println(safecast.ToInt64(uint64(math.MaxInt64) + 1))
// Output: 0 conversion issue: 9223372036854775808 is greater than 9223372036854775807
// Output: 0 conversion issue: 9223372036854775808 (uint64) is greater than 9223372036854775807 (int64): maximum value for this type exceeded
fmt.Println(safecast.ToUint64(int8(-1)))
// Output: 0 conversion issue: -1 is negative
// Output: 0 conversion issue: -1 (int8) is smaller than 0 (uint64): minimum value for this type exceeded
fmt.Println(safecast.ToInt(uint64(math.MaxInt) + 1))
// Output: 0 conversion issue: 9223372036854775808 is greater than 9223372036854775807
// Output: 0 conversion issue: 9223372036854775808 (uint64) is greater than 9223372036854775807 (int): maximum value for this type exceeded
fmt.Println(safecast.ToUint(int8(-1)))
// Output: 0 conversion issue: -1 is negative
// Output: 0 conversion issue: -1 (int8) is smaller than 0 (uint): minimum value for this type exceeded
}
```

[Go Playground](https://go.dev/play/p/VCrv1aLJjMQ)
[Go Playground](https://go.dev/play/p/cmnHATVyJf2)

## Conversion overflows
## Conversion issues

Issues can happen when converting between signed and unsigned integers, or when converting to a smaller integer type.

Expand Down
2 changes: 1 addition & 1 deletion conversion.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package safecast aims to solve the issue of type conversion
// Package go-safecast solves the type conversion issues in Go
//
// In Go, integer type conversion can lead to unexpected behavior and errors if not handled carefully.
// Issues can happen when converting between signed and unsigned integers, or when converting to a smaller integer type.
Expand Down
58 changes: 58 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package safecast_test

import (
"fmt"
"math"

"github.com/ccoveille/go-safecast"
)

func ExampleToInt8() {
fmt.Println(safecast.ToInt8(float64(20000)))
// Output: 0 conversion issue: 20000 (float64) is greater than 127 (int8): maximum value for this type exceeded
}

func ExampleToUint8() {
fmt.Println(safecast.ToUint8(int64(-1)))
// Output: 0 conversion issue: -1 (int64) is less than 0 (uint8): minimum value for this type exceeded
}

func ExampleToInt16() {
fmt.Println(safecast.ToInt16(int32(40000)))
// Output: 0 conversion issue: 40000 (int32) is greater than 32767 (int16): maximum value for this type exceeded
}

func ExampleToUint16() {
fmt.Println(safecast.ToUint16(int64(-1)))
// Output: 0 conversion issue: -1 (int64) is less than 0 (uint16): minimum value for this type exceeded
}

func ExampleToInt32() {
fmt.Println(safecast.ToInt32(math.MaxUint32 + 1))
// Output: 0 conversion issue: 4294967296 (int) is greater than 2147483647 (int32): maximum value for this type exceeded
}

func ExampleToUint32() {
fmt.Println(safecast.ToUint32(int64(-1)))
// Output: 0 conversion issue: -1 (int64) is less than 0 (uint32): minimum value for this type exceeded
}

func ExampleToInt64() {
fmt.Println(safecast.ToInt64(uint64(math.MaxInt64) + 1))
// Output: 0 conversion issue: 9223372036854775808 (uint64) is greater than 9223372036854775807 (int64): maximum value for this type exceeded
}

func ExampleToUint64() {
fmt.Println(safecast.ToUint64(int8(-1)))
// Output: 0 conversion issue: -1 (int8) is less than 0 (uint64): minimum value for this type exceeded
}

func ExampleToInt() {
fmt.Println(safecast.ToInt(uint64(math.MaxInt) + 1))
// Output: 0 conversion issue: 9223372036854775808 (uint64) is greater than 9223372036854775807 (int): maximum value for this type exceeded
}

func ExampleToUint() {
fmt.Println(safecast.ToUint(int8(-1)))
// Output: 0 conversion issue: -1 (int8) is less than 0 (uint): minimum value for this type exceeded
}

0 comments on commit de17f87

Please sign in to comment.