Skip to content

Commit e1b6645

Browse files
committed
chore: add deprecation notice to all ToIntXX and ToUIntXX functions
Convert method should be used as a replacement for all ToIntXX and ToUIntXX functions. These methods will be removed in the next major release.
1 parent e16fe30 commit e1b6645

File tree

6 files changed

+230
-19
lines changed

6 files changed

+230
-19
lines changed

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,40 @@ import (
2525
)
2626

2727
func main() {
28-
var a int
28+
var a int64
2929

3030
a = 42
31-
b, err := safecast.ToUint8(a) // everything is fine
31+
b, err := safecast.Convert[int16](a) // everything is fine because 42 fits in int16
3232
if err != nil {
3333
fmt.Println(err)
3434
}
3535
fmt.Println(b)
36-
// Output: 42
3736

3837
a = 255 + 1
39-
_, err = safecast.ToUint8(a) // 256 is greater than uint8 maximum value
38+
_, err = safecast.Convert[uint8](a) // 256 is greater than uint8 maximum value
4039
if err != nil {
40+
// Output: conversion issue: 256 (int64) is greater than 255 (uint8): maximum value for this type exceeded
4141
fmt.Println(err)
42-
// Output: conversion issue: 256 (int) is greater than 255 (uint8): maximum value for this type exceeded
4342
}
4443

4544
a = -1
46-
_, err = safecast.ToUint8(a) // -1 cannot fit in uint8
45+
_, err = safecast.Convert[uint64](a) // -1 cannot fit in uint64
4746
if err != nil {
47+
// Output: conversion issue: -1 (int64) is less than 0 (uint64): minimum value for this type exceeded
4848
fmt.Println(err)
49-
// Output: conversion issue: -1 (int) is less than 0 (uint8): minimum value for this type exceeded
5049
}
5150

5251
str := "\x99" // ASCII code 153 for Trademark symbol
53-
e := str[0]
54-
_, err = safecast.ToInt8(e)
52+
e := str[0] // e is a rune (uint8)
53+
_, err = safecast.Convert[int8](e)
5554
if err != nil {
56-
fmt.Println(err)
5755
// Output: conversion issue: 153 (uint8) is greater than 127 (int8): maximum value for this type exceeded
56+
fmt.Println(err)
5857
}
5958
}
6059
```
6160

62-
[Go Playground](https://go.dev/play/p/nelJshulOnj)
61+
[Go Playground](https://go.dev/play/p/OC9ueLHY0D2)
6362

6463
## Conversion issues
6564

conversion.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,86 +216,110 @@ func getRangeError[NumOut Number, NumIn Number](value NumIn) error {
216216
}
217217
}
218218

219-
// ToInt attempts to convert any [Type] value to an int.
219+
// ToInt attempts to convert any [Number] value to an int.
220220
// If the conversion results in a value outside the range of an int,
221221
// an [ErrConversionIssue] error is returned.
222+
//
223+
// Deprecated: use [Convert] instead with Convert[int](i)
222224
func ToInt[T Number](i T) (int, error) {
223225
return convertFromNumber[int](i)
224226
}
225227

226228
// ToUint attempts to convert any [Number] value to an uint.
227229
// If the conversion results in a value outside the range of an uint,
228230
// an [ErrConversionIssue] error is returned.
231+
//
232+
// Deprecated: use [Convert] instead with Convert[uint](i)
229233
func ToUint[T Number](i T) (uint, error) {
230234
return convertFromNumber[uint](i)
231235
}
232236

233237
// ToInt8 attempts to convert any [Number] value to an int8.
234238
// If the conversion results in a value outside the range of an int8,
235239
// an [ErrConversionIssue] error is returned.
240+
//
241+
// Deprecated: use [Convert] instead with Convert[int8](i)
236242
func ToInt8[T Number](i T) (int8, error) {
237243
return convertFromNumber[int8](i)
238244
}
239245

240246
// ToUint8 attempts to convert any [Number] value to an uint8.
241247
// If the conversion results in a value outside the range of an uint8,
242248
// an [ErrConversionIssue] error is returned.
249+
//
250+
// Deprecated: use [Convert] instead with Convert[uint8](i)
243251
func ToUint8[T Number](i T) (uint8, error) {
244252
return convertFromNumber[uint8](i)
245253
}
246254

247255
// ToInt16 attempts to convert any [Number] value to an int16.
248256
// If the conversion results in a value outside the range of an int16,
249257
// an [ErrConversionIssue] error is returned.
258+
//
259+
// Deprecated: use [Convert] instead with Convert[int16](i)
250260
func ToInt16[T Number](i T) (int16, error) {
251261
return convertFromNumber[int16](i)
252262
}
253263

254264
// ToUint16 attempts to convert any [Number] value to an uint16.
255265
// If the conversion results in a value outside the range of an uint16,
256266
// an [ErrConversionIssue] error is returned.
267+
//
268+
// Deprecated: use [Convert] instead with Convert[uint16](i)
257269
func ToUint16[T Number](i T) (uint16, error) {
258270
return convertFromNumber[uint16](i)
259271
}
260272

261273
// ToInt32 attempts to convert any [Number] value to an int32.
262274
// If the conversion results in a value outside the range of an int32,
263275
// an [ErrConversionIssue] error is returned.
276+
//
277+
// Deprecated: use [Convert] instead with Convert[int32](i)
264278
func ToInt32[T Number](i T) (int32, error) {
265279
return convertFromNumber[int32](i)
266280
}
267281

268282
// ToUint32 attempts to convert any [Number] value to an uint32.
269283
// If the conversion results in a value outside the range of an uint32,
270284
// an [ErrConversionIssue] error is returned.
285+
//
286+
// Deprecated: use [Convert] instead with Convert[uint32](i)
271287
func ToUint32[T Number](i T) (uint32, error) {
272288
return convertFromNumber[uint32](i)
273289
}
274290

275291
// ToInt64 attempts to convert any [Number] value to an int64.
276292
// If the conversion results in a value outside the range of an int64,
277293
// an [ErrConversionIssue] error is returned.
294+
//
295+
// Deprecated: use [Convert] instead with Convert[int64](i)
278296
func ToInt64[T Number](i T) (int64, error) {
279297
return convertFromNumber[int64](i)
280298
}
281299

282300
// ToUint64 attempts to convert any [Number] value to an uint64.
283301
// If the conversion results in a value outside the range of an uint64,
284302
// an [ErrConversionIssue] error is returned.
303+
//
304+
// Deprecated: use [Convert] instead with Convert[uint64](i)
285305
func ToUint64[T Number](i T) (uint64, error) {
286306
return convertFromNumber[uint64](i)
287307
}
288308

289309
// ToFloat32 attempts to convert any [Number] value to a float32.
290310
// If the conversion results in a value outside the range of a float32,
291311
// an [ErrConversionIssue] error is returned.
312+
//
313+
// Deprecated: use [Convert] instead with Convert[float32](i)
292314
func ToFloat32[T Number](i T) (float32, error) {
293315
return convertFromNumber[float32](i)
294316
}
295317

296318
// ToFloat64 attempts to convert any [Number] value to a float64.
297319
// If the conversion results in a value outside the range of a float64,
298320
// an [ErrConversionIssue] error is returned.
321+
//
322+
// Deprecated: use [Convert] instead with Convert[float64](i)
299323
func ToFloat64[T Number](i T) (float64, error) {
300324
return convertFromNumber[float64](i)
301325
}

examples_32bit_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ func ExampleToInt() {
3333
// 42 <nil>
3434
// 0 conversion issue: 3.4028235e+38 (float32) is greater than 2147483647 (int): maximum value for this type exceeded
3535
}
36+
37+
func ExampleToInt_deprecation() {
38+
// Example showing the use of the [safecast.Convert] function as a replacement for the deprecated [safecast.ToInt] function.
39+
40+
a := uint64(42)
41+
i, err := safecast.Convert[int](a)
42+
fmt.Println(i, err)
43+
44+
b := float32(math.MaxFloat32)
45+
_, err = safecast.Convert[int](b)
46+
fmt.Println(err)
47+
48+
// Output:
49+
// 42 <nil>
50+
// conversion issue: 3.4028235e+38 (float32) is greater than 2147483647 (int): maximum value for this type exceeded
51+
}

examples_64bit_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,20 @@ func ExampleToInt() {
3232
// 42 <nil>
3333
// 0 conversion issue: 3.4028235e+38 (float32) is greater than 9223372036854775807 (int): maximum value for this type exceeded
3434
}
35+
36+
func ExampleToInt_deprecation() {
37+
// Example showing the use of the [safecast.Convert] function as a replacement for the deprecated [safecast.ToInt] function.
38+
39+
a := uint64(42)
40+
i, err := safecast.Convert[int](a)
41+
fmt.Println(i, err)
42+
43+
b := float32(math.MaxFloat32)
44+
_, err = safecast.Convert[int](b)
45+
46+
fmt.Println(err)
47+
48+
// Output:
49+
// 42 <nil>
50+
// conversion issue: 3.4028235e+38 (float32) is greater than 9223372036854775807 (int): maximum value for this type exceeded
51+
}

0 commit comments

Comments
 (0)