Skip to content

Commit 214bd73

Browse files
committed
doc: improve documentation
The struct of the error is no longer exported. It helps to provide a better documentation, even if it's a breaking change.
1 parent f0cd846 commit 214bd73

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

conversion.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Package go-safecast solves the type conversion issues in Go
2-
//
3-
// In Go, integer type conversion can lead to unexpected behavior and errors if not handled carefully.
4-
// Issues can happen when converting between signed and unsigned integers, or when converting to a smaller integer type.
5-
61
package safecast
72

83
import (
@@ -61,7 +56,7 @@ func Convert[NumOut Number](orig any) (converted NumOut, err error) {
6156
return convertFromString[NumOut](v)
6257
}
6358

64-
return 0, Error{
59+
return 0, errorHelper{
6560
err: fmt.Errorf("%w from %T", ErrUnsupportedConversion, orig),
6661
}
6762
}
@@ -97,7 +92,7 @@ func convertFromNumber[NumOut Number, NumIn Number](orig NumIn) (converted NumOu
9792
errBoundary = ErrExceedMinimumValue
9893
}
9994

100-
return 0, Error{
95+
return 0, errorHelper{
10196
value: orig,
10297
err: errBoundary,
10398
boundary: boundary,
@@ -112,7 +107,7 @@ func convertFromNumber[NumOut Number, NumIn Number](orig NumIn) (converted NumOu
112107
}
113108

114109
if !sameSign(orig, converted) {
115-
return 0, Error{
110+
return 0, errorHelper{
116111
value: orig,
117112
err: errBoundary,
118113
boundary: boundary,
@@ -135,7 +130,7 @@ func convertFromNumber[NumOut Number, NumIn Number](orig NumIn) (converted NumOu
135130
return converted, nil
136131
}
137132

138-
return 0, Error{
133+
return 0, errorHelper{
139134
value: orig,
140135
err: errBoundary,
141136
boundary: boundary,
@@ -155,7 +150,7 @@ func convertFromString[NumOut Number](s string) (converted NumOut, err error) {
155150
if strings.Contains(s, ".") {
156151
o, err := strconv.ParseFloat(s, 64)
157152
if err != nil {
158-
return 0, Error{
153+
return 0, errorHelper{
159154
value: s,
160155
err: fmt.Errorf("%w %v to %T", ErrStringConversion, s, converted),
161156
}
@@ -167,13 +162,13 @@ func convertFromString[NumOut Number](s string) (converted NumOut, err error) {
167162
o, err := strconv.ParseInt(s, 0, 64)
168163
if err != nil {
169164
if errors.Is(err, strconv.ErrRange) {
170-
return 0, Error{
165+
return 0, errorHelper{
171166
value: s,
172167
err: ErrExceedMinimumValue,
173168
boundary: math.MinInt,
174169
}
175170
}
176-
return 0, Error{
171+
return 0, errorHelper{
177172
value: s,
178173
err: fmt.Errorf("%w %v to %T", ErrStringConversion, s, converted),
179174
}
@@ -185,14 +180,14 @@ func convertFromString[NumOut Number](s string) (converted NumOut, err error) {
185180
o, err := strconv.ParseUint(s, 0, 64)
186181
if err != nil {
187182
if errors.Is(err, strconv.ErrRange) {
188-
return 0, Error{
183+
return 0, errorHelper{
189184
value: s,
190185
err: ErrExceedMaximumValue,
191186
boundary: uint(math.MaxUint),
192187
}
193188
}
194189

195-
return 0, Error{
190+
return 0, errorHelper{
196191
value: s,
197192
err: fmt.Errorf("%w %v to %T", ErrStringConversion, s, converted),
198193
}

doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package safecast
2+
3+
// Package go-safecast solves the type conversion issues in Go
4+
//
5+
// In Go, integer type conversion can lead to unexpected behavior and errors if not handled carefully.
6+
// Issues can happen when converting between signed and unsigned integers, or when converting to a smaller integer type.

errors.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,34 @@ import (
55
"fmt"
66
)
77

8-
var (
9-
ErrConversionIssue = errors.New("conversion issue")
10-
ErrRangeOverflow = errors.New("range overflow")
11-
ErrExceedMaximumValue = errors.New("maximum value for this type exceeded")
12-
ErrExceedMinimumValue = errors.New("minimum value for this type exceeded")
13-
ErrUnsupportedConversion = errors.New("unsupported type")
14-
ErrStringConversion = errors.New("cannot convert from string")
15-
)
8+
// ErrConversionIssue is a generic error for type conversion issues
9+
// It is used to wrap other errors
10+
var ErrConversionIssue = errors.New("conversion issue")
11+
12+
// ErrRangeOverflow is an error for when the value is outside the range of the desired type
13+
var ErrRangeOverflow = errors.New("range overflow")
14+
15+
// ErrExceedMaximumValue is an error for when the value is greater than the maximum value of the desired type.
16+
var ErrExceedMaximumValue = errors.New("maximum value for this type exceeded")
17+
18+
// ErrExceedMaximumValue is an error for when the value is less than the minimum value of the desired type.
19+
var ErrExceedMinimumValue = errors.New("minimum value for this type exceeded")
20+
21+
// ErrUnsupportedConversion is an error for when the conversion is not supported from the provided type.
22+
var ErrUnsupportedConversion = errors.New("unsupported type")
23+
24+
// ErrStringConversion is an error for when the conversion fails from string.
25+
var ErrStringConversion = errors.New("cannot convert from string")
1626

17-
type Error struct {
27+
// errorHelper is a helper struct for error messages
28+
// It is used to wrap other errors, and provides additional information
29+
type errorHelper struct {
1830
value any
1931
boundary any
2032
err error
2133
}
2234

23-
func (e Error) Error() string {
35+
func (e errorHelper) Error() string {
2436
errMessage := ErrConversionIssue.Error()
2537

2638
switch {
@@ -36,7 +48,7 @@ func (e Error) Error() string {
3648
return errMessage
3749
}
3850

39-
func (e Error) Unwrap() []error {
51+
func (e errorHelper) Unwrap() []error {
4052
errs := []error{ErrConversionIssue}
4153
if e.err != nil {
4254
switch {

0 commit comments

Comments
 (0)