Skip to content

Commit ca4e7b9

Browse files
committed
Fix all linting problems
1 parent 8b6c8d7 commit ca4e7b9

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

safe_conversion.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Package safe provides utilities for safely converting between various numeric types.
2+
// The package ensures proper range checks, overflow detection, and meaningful error messages
3+
// when conversions fail due to invalid input or range violations.
14
package safe
25

36
import (
@@ -9,7 +12,9 @@ import (
912
)
1013

1114
const (
12-
MinInt16 = -1 << 15 // -32768
15+
// MinInt16 defines the minimum integer value for an int16
16+
MinInt16 = -1 << 15 // -32768
17+
// MaxInt16 defines the maximum integer value for an int16
1318
MaxInt16 = 1<<15 - 1 // 32767
1419
)
1520

@@ -113,7 +118,7 @@ func Int64ToUint32(value int64) (uint32, error) {
113118
}
114119

115120
if value > math.MaxUint32 {
116-
return 0, fmt.Errorf("value exceeds uint32 range: %d", value)
121+
return 0, fmt.Errorf("%w (uint32): %d", ErrValueOutOfRange, value)
117122
}
118123

119124
return uint32(value), nil
@@ -131,7 +136,7 @@ func BigWordToUint32(value big.Word) (uint32, error) {
131136
valueUint64 := uint64(value)
132137

133138
if valueUint64 > math.MaxUint32 {
134-
return 0, fmt.Errorf("big.Word exceeds uint32 range: %d", valueUint64)
139+
return 0, fmt.Errorf("big.Word %w (uint32): %d", ErrValueExceedsLimit, valueUint64)
135140
}
136141

137142
return uint32(valueUint64), nil
@@ -141,11 +146,11 @@ func BigWordToUint32(value big.Word) (uint32, error) {
141146
// Checks if the value is non-negative and within the uint16 range.
142147
func IntToUint16(value int) (uint16, error) {
143148
if value < 0 {
144-
return 0, fmt.Errorf("negative value cannot be converted to uint16: %d", value)
149+
return 0, fmt.Errorf("%w to uint16: %d", ErrNegativeValueCannotBeConverted, value)
145150
}
146151

147152
if value > math.MaxUint16 {
148-
return 0, fmt.Errorf("value exceeds uint16 range: %d", value)
153+
return 0, fmt.Errorf("%w (uint16): %d", ErrValueExceedsLimit, value)
149154
}
150155

151156
return uint16(value), nil
@@ -155,7 +160,7 @@ func IntToUint16(value int) (uint16, error) {
155160
// Checks if the value is within the valid int16 range.
156161
func IntToInt16(value int) (int16, error) {
157162
if value < MinInt16 || value > MaxInt16 {
158-
return 0, fmt.Errorf("value out of int16 range: %d", value)
163+
return 0, fmt.Errorf("%w (int16): %d", ErrValueOutOfRange, value)
159164
}
160165

161166
return int16(value), nil
@@ -165,7 +170,7 @@ func IntToInt16(value int) (int16, error) {
165170
// Checks if the value exceeds the uint32 range.
166171
func UintToUint32(value uint) (uint32, error) {
167172
if value > math.MaxUint32 {
168-
return 0, fmt.Errorf("value exceeds uint32 range: %d", value)
173+
return 0, fmt.Errorf("%w (uint32): %d", ErrValueOutOfRange, value)
169174
}
170175

171176
return uint32(value), nil
@@ -176,11 +181,11 @@ func UintToUint32(value uint) (uint32, error) {
176181
func TimeToUint32(value time.Time) (uint32, error) {
177182
timestamp := value.Unix()
178183
if timestamp < 0 {
179-
return 0, fmt.Errorf("negative timestamp cannot be converted to uint32: %d", timestamp)
184+
return 0, fmt.Errorf("%w to uint32: %d", ErrNegativeValueCannotBeConverted, timestamp)
180185
}
181186

182187
if timestamp > math.MaxUint32 {
183-
return 0, fmt.Errorf("timestamp exceeds uint32 range: %d", timestamp)
188+
return 0, fmt.Errorf("%w (uint32): %d", ErrValueOutOfRange, timestamp)
184189
}
185190

186191
return uint32(timestamp), nil
@@ -190,7 +195,7 @@ func TimeToUint32(value time.Time) (uint32, error) {
190195
// Checks if the value exceeds the uint8 range.
191196
func Uint32ToUint8(value uint32) (uint8, error) {
192197
if value > math.MaxUint8 {
193-
return 0, fmt.Errorf("value exceeds uint8 range: %d", value)
198+
return 0, fmt.Errorf("%w (uint8): %d", ErrValueOutOfRange, value)
194199
}
195200

196201
return uint8(value), nil
@@ -200,7 +205,7 @@ func Uint32ToUint8(value uint32) (uint8, error) {
200205
// Checks if the value exceeds the maximum int range.
201206
func UintptrToInt(value uintptr) (int, error) {
202207
if value > uintptr(math.MaxInt) {
203-
return 0, fmt.Errorf("value exceeds int range: %d", value)
208+
return 0, fmt.Errorf("%w (int): %d", ErrValueOutOfRange, value)
204209
}
205210

206211
return int(value), nil
@@ -210,7 +215,7 @@ func UintptrToInt(value uintptr) (int, error) {
210215
// Checks if the value exceeds the maximum int64 range.
211216
func Uint64ToInt64(value uint64) (int64, error) {
212217
if value > math.MaxInt64 {
213-
return 0, fmt.Errorf("value exceeds int64 range: %d", value)
218+
return 0, fmt.Errorf("%w (int64): %d", ErrValueOutOfRange, value)
214219
}
215220

216221
return int64(value), nil
@@ -220,7 +225,7 @@ func Uint64ToInt64(value uint64) (int64, error) {
220225
// Checks if the value exceeds the maximum int32 range.
221226
func Uint32ToInt32(value uint32) (int32, error) {
222227
if value > math.MaxInt32 {
223-
return 0, fmt.Errorf("value exceeds int32 range: %d", value)
228+
return 0, fmt.Errorf("%w (int32): %d", ErrValueOutOfRange, value)
224229
}
225230

226231
return int32(value), nil
@@ -230,7 +235,7 @@ func Uint32ToInt32(value uint32) (int32, error) {
230235
// Checks if the value exceeds the int32 range or if it's negative.
231236
func Uint64ToInt32(value uint64) (int32, error) {
232237
if value > math.MaxInt32 {
233-
return 0, fmt.Errorf("value exceeds int32 range: %d", value)
238+
return 0, fmt.Errorf("%w (int32): %d", ErrValueOutOfRange, value)
234239
}
235240

236241
return int32(value), nil
@@ -252,7 +257,7 @@ func Uint32ToUint64(value uint32) (uint64, error) {
252257
// Checks if the value exceeds the uint16 range.
253258
func Uint64ToUint16(value uint64) (uint16, error) {
254259
if value > math.MaxUint16 {
255-
return 0, fmt.Errorf("value exceeds uint16 range: %d", value)
260+
return 0, fmt.Errorf("%w (uint16): %d", ErrValueOutOfRange, value)
256261
}
257262

258263
return uint16(value), nil

0 commit comments

Comments
 (0)