Skip to content

Commit 885b019

Browse files
committed
feat: Convert now returns the converted value on error
BREAKING CHANGE: It was previously returning 0 on error.
1 parent a68adae commit 885b019

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

conversion.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func Convert[NumOut Number, NumIn Number](orig NumIn) (NumOut, error) {
6060
if isFloat64[NumIn]() {
6161
floatOrig := float64(orig)
6262
if math.IsInf(floatOrig, 1) || math.IsInf(floatOrig, -1) {
63-
return 0, getRangeError[NumOut](orig)
63+
return converted, getRangeError[NumOut](orig)
6464
}
6565
if math.IsNaN(floatOrig) {
66-
return 0, errorHelper[NumOut]{
66+
return converted, errorHelper[NumOut]{
6767
value: orig,
6868
err: ErrUnsupportedConversion,
6969
}
@@ -84,11 +84,11 @@ func Convert[NumOut Number, NumIn Number](orig NumIn) (NumOut, error) {
8484

8585
// TODO: check for numbers close to math.MaxFloat32
8686

87-
return 0, getRangeError[NumOut](orig)
87+
return converted, getRangeError[NumOut](orig)
8888
}
8989

9090
if !sameSign(orig, converted) {
91-
return 0, getRangeError[NumOut](orig)
91+
return converted, getRangeError[NumOut](orig)
9292
}
9393

9494
// and compare
@@ -100,7 +100,7 @@ func Convert[NumOut Number, NumIn Number](orig NumIn) (NumOut, error) {
100100
// convert back to the original type
101101
cast := NumIn(converted)
102102
if cast != base {
103-
return 0, getRangeError[NumOut](orig)
103+
return converted, getRangeError[NumOut](orig)
104104
}
105105

106106
return converted, nil

conversion_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,8 @@ func TestRequireConvert(t *testing.T) {
529529

530530
t.Run("test fail on error", func(t *testing.T) {
531531
for name, tt := range map[string]TestRunner{
532-
"negative": MapRequireConvertTest[int, uint8]{Input: -1, ExpectedTestFailure: true},
533-
"overflow": MapRequireConvertTest[int, uint8]{Input: math.MaxInt, ExpectedTestFailure: true},
532+
"negative": MapRequireConvertTest[int, uint8]{Input: -1, ExpectedTestFailure: true, ExpectedOutput: 255},
533+
"overflow": MapRequireConvertTest[int, uint8]{Input: math.MaxInt, ExpectedTestFailure: true, ExpectedOutput: 255},
534534
} {
535535
t.Run(name, func(t *testing.T) {
536536
tt.Run(t)

examples_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ func ExampleConvert() {
1414
fmt.Println(c, err)
1515

1616
a := int8(-1)
17-
i, err := safecast.Convert[uint](a)
17+
i, err := safecast.Convert[uint16](a)
1818
fmt.Println(i, err)
1919

2020
// Output:
2121
// 17 <nil>
2222
// 17 <nil>
23-
// 0 conversion issue: -1 (int8) is less than 0 (uint): minimum value for this type exceeded
23+
// 65535 conversion issue: -1 (int8) is less than 0 (uint16): minimum value for this type exceeded
2424
}

0 commit comments

Comments
 (0)