Skip to content

Commit 8b6c8d7

Browse files
committed
Start fixing lint errors
1 parent 1c5f42b commit 8b6c8d7

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

safe_conversion.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package safe
22

33
import (
4+
"errors"
45
"fmt"
56
"math"
67
"math/big"
@@ -12,11 +13,22 @@ const (
1213
MaxInt16 = 1<<15 - 1 // 32767
1314
)
1415

16+
var (
17+
// ErrValueOutOfRange defines when a value is out of range
18+
ErrValueOutOfRange = errors.New("value out of range")
19+
// ErrValueOverflow defines when a value is overflowing the data type
20+
ErrValueOverflow = errors.New("value overflow")
21+
// ErrNegativeValueCannotBeConverted defines when a negative value is trying to be used with an unsigned integer
22+
ErrNegativeValueCannotBeConverted = errors.New("negative value cannot be converted to unsigned integer")
23+
// ErrValueExceedsLimit defines when a converted value exceeds the limit of the data type
24+
ErrValueExceedsLimit = errors.New("value exceeds limit")
25+
)
26+
1527
// IntToUint32 converts an int to uint32 after ensuring it’s in range.
1628
// Returns an error if the input is negative or exceeds the maximum value of a uint32.
1729
func IntToUint32(v int) (uint32, error) {
1830
if v < 0 || v > math.MaxUint32 {
19-
return 0, fmt.Errorf("value %d out of range", v)
31+
return 0, fmt.Errorf("%w: %d", ErrValueOutOfRange, v)
2032
}
2133

2234
return uint32(v), nil
@@ -27,7 +39,7 @@ func IntToUint32(v int) (uint32, error) {
2739
func Uint64ToUint32(v uint64) (uint32, error) {
2840
// ^uint32(0) is the maximum value of a uint32 (all bits set).
2941
if v > uint64(math.MaxUint32) {
30-
return 0, fmt.Errorf("value %d overflows uint32 (max %d)", v, math.MaxUint32)
42+
return 0, fmt.Errorf("uint32 %w: %d (max %d)", ErrValueOverflow, v, math.MaxUint32)
3143
}
3244

3345
return uint32(v), nil
@@ -37,7 +49,7 @@ func Uint64ToUint32(v uint64) (uint32, error) {
3749
// Returns an error if the input is negative.
3850
func Int64ToUint64(value int64) (uint64, error) {
3951
if value < 0 {
40-
return 0, fmt.Errorf("negative value cannot be converted to uint64: %d", value)
52+
return 0, fmt.Errorf("%w (uint64): %d", ErrNegativeValueCannotBeConverted, value)
4153
}
4254

4355
return uint64(value), nil
@@ -47,7 +59,7 @@ func Int64ToUint64(value int64) (uint64, error) {
4759
// Returns an error if the input is negative.
4860
func IntToUint64(value int) (uint64, error) {
4961
if value < 0 {
50-
return 0, fmt.Errorf("negative value cannot be converted to uint64: %d", value)
62+
return 0, fmt.Errorf("%w (uint64): %d", ErrNegativeValueCannotBeConverted, value)
5163
}
5264

5365
return uint64(value), nil
@@ -57,7 +69,7 @@ func IntToUint64(value int) (uint64, error) {
5769
// Returns an error if the value exceeds the limits of an int.
5870
func Uint64ToInt(value uint64) (int, error) {
5971
if value > math.MaxInt {
60-
return 0, fmt.Errorf("value exceeds int limit: %d", value)
72+
return 0, fmt.Errorf("%w (int): %d", ErrValueExceedsLimit, value)
6173
}
6274

6375
return int(value), nil
@@ -67,7 +79,7 @@ func Uint64ToInt(value uint64) (int, error) {
6779
// Returns an error if the value is outside the range of int32.
6880
func Int64ToInt32(value int64) (int32, error) {
6981
if value < math.MinInt32 || value > math.MaxInt32 {
70-
return 0, fmt.Errorf("value out of int32 range: %d", value)
82+
return 0, fmt.Errorf("%w (int32): %d", ErrValueOutOfRange, value)
7183
}
7284

7385
return int32(value), nil
@@ -77,7 +89,7 @@ func Int64ToInt32(value int64) (int32, error) {
7789
// Checks if the value is within the valid int32 range.
7890
func IntToInt32(value int) (int32, error) {
7991
if value < math.MinInt32 || value > math.MaxInt32 {
80-
return 0, fmt.Errorf("value out of int32 range: %d", value)
92+
return 0, fmt.Errorf("%w (int32): %d", ErrValueOutOfRange, value)
8193
}
8294

8395
return int32(value), nil
@@ -87,7 +99,7 @@ func IntToInt32(value int) (int32, error) {
8799
// Checks only for negative values, as positive int32 values are always within uint32 range.
88100
func Int32ToUint32(value int32) (uint32, error) {
89101
if value < 0 {
90-
return 0, fmt.Errorf("negative value cannot be converted to uint32: %d", value)
102+
return 0, fmt.Errorf("%w to uint32: %d", ErrNegativeValueCannotBeConverted, value)
91103
}
92104

93105
return uint32(value), nil
@@ -97,7 +109,7 @@ func Int32ToUint32(value int32) (uint32, error) {
97109
// Checks if the value is non-negative and within the uint32 range.
98110
func Int64ToUint32(value int64) (uint32, error) {
99111
if value < 0 {
100-
return 0, fmt.Errorf("negative value cannot be converted to uint32: %d", value)
112+
return 0, fmt.Errorf("%w to uint32: %d", ErrNegativeValueCannotBeConverted, value)
101113
}
102114

103115
if value > math.MaxUint32 {

0 commit comments

Comments
 (0)