Skip to content

Commit

Permalink
doc: improve documentation
Browse files Browse the repository at this point in the history
The struct of the error is no longer exported.

It helps to provide a better documentation, even if it's a breaking change.
  • Loading branch information
ccoVeille committed Dec 17, 2024
1 parent f0cd846 commit 214bd73
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
23 changes: 9 additions & 14 deletions conversion.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
// Package go-safecast solves the type conversion issues in Go
//
// In Go, integer type conversion can lead to unexpected behavior and errors if not handled carefully.
// Issues can happen when converting between signed and unsigned integers, or when converting to a smaller integer type.

package safecast

import (
Expand Down Expand Up @@ -61,7 +56,7 @@ func Convert[NumOut Number](orig any) (converted NumOut, err error) {
return convertFromString[NumOut](v)
}

return 0, Error{
return 0, errorHelper{
err: fmt.Errorf("%w from %T", ErrUnsupportedConversion, orig),
}
}
Expand Down Expand Up @@ -97,7 +92,7 @@ func convertFromNumber[NumOut Number, NumIn Number](orig NumIn) (converted NumOu
errBoundary = ErrExceedMinimumValue
}

return 0, Error{
return 0, errorHelper{
value: orig,
err: errBoundary,
boundary: boundary,
Expand All @@ -112,7 +107,7 @@ func convertFromNumber[NumOut Number, NumIn Number](orig NumIn) (converted NumOu
}

if !sameSign(orig, converted) {
return 0, Error{
return 0, errorHelper{
value: orig,
err: errBoundary,
boundary: boundary,
Expand All @@ -135,7 +130,7 @@ func convertFromNumber[NumOut Number, NumIn Number](orig NumIn) (converted NumOu
return converted, nil
}

return 0, Error{
return 0, errorHelper{
value: orig,
err: errBoundary,
boundary: boundary,
Expand All @@ -155,7 +150,7 @@ func convertFromString[NumOut Number](s string) (converted NumOut, err error) {
if strings.Contains(s, ".") {
o, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, Error{
return 0, errorHelper{
value: s,
err: fmt.Errorf("%w %v to %T", ErrStringConversion, s, converted),
}
Expand All @@ -167,13 +162,13 @@ func convertFromString[NumOut Number](s string) (converted NumOut, err error) {
o, err := strconv.ParseInt(s, 0, 64)
if err != nil {
if errors.Is(err, strconv.ErrRange) {
return 0, Error{
return 0, errorHelper{
value: s,
err: ErrExceedMinimumValue,
boundary: math.MinInt,
}
}
return 0, Error{
return 0, errorHelper{
value: s,
err: fmt.Errorf("%w %v to %T", ErrStringConversion, s, converted),
}
Expand All @@ -185,14 +180,14 @@ func convertFromString[NumOut Number](s string) (converted NumOut, err error) {
o, err := strconv.ParseUint(s, 0, 64)
if err != nil {
if errors.Is(err, strconv.ErrRange) {
return 0, Error{
return 0, errorHelper{
value: s,
err: ErrExceedMaximumValue,
boundary: uint(math.MaxUint),
}
}

return 0, Error{
return 0, errorHelper{
value: s,
err: fmt.Errorf("%w %v to %T", ErrStringConversion, s, converted),
}
Expand Down
6 changes: 6 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package safecast

// Package go-safecast solves the type conversion issues in Go
//
// In Go, integer type conversion can lead to unexpected behavior and errors if not handled carefully.
// Issues can happen when converting between signed and unsigned integers, or when converting to a smaller integer type.
34 changes: 23 additions & 11 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@ import (
"fmt"
)

var (
ErrConversionIssue = errors.New("conversion issue")
ErrRangeOverflow = errors.New("range overflow")
ErrExceedMaximumValue = errors.New("maximum value for this type exceeded")
ErrExceedMinimumValue = errors.New("minimum value for this type exceeded")
ErrUnsupportedConversion = errors.New("unsupported type")
ErrStringConversion = errors.New("cannot convert from string")
)
// ErrConversionIssue is a generic error for type conversion issues
// It is used to wrap other errors
var ErrConversionIssue = errors.New("conversion issue")

// ErrRangeOverflow is an error for when the value is outside the range of the desired type
var ErrRangeOverflow = errors.New("range overflow")

// ErrExceedMaximumValue is an error for when the value is greater than the maximum value of the desired type.
var ErrExceedMaximumValue = errors.New("maximum value for this type exceeded")

// ErrExceedMaximumValue is an error for when the value is less than the minimum value of the desired type.
var ErrExceedMinimumValue = errors.New("minimum value for this type exceeded")

// ErrUnsupportedConversion is an error for when the conversion is not supported from the provided type.
var ErrUnsupportedConversion = errors.New("unsupported type")

// ErrStringConversion is an error for when the conversion fails from string.
var ErrStringConversion = errors.New("cannot convert from string")

type Error struct {
// errorHelper is a helper struct for error messages
// It is used to wrap other errors, and provides additional information
type errorHelper struct {
value any
boundary any
err error
}

func (e Error) Error() string {
func (e errorHelper) Error() string {
errMessage := ErrConversionIssue.Error()

switch {
Expand All @@ -36,7 +48,7 @@ func (e Error) Error() string {
return errMessage
}

func (e Error) Unwrap() []error {
func (e errorHelper) Unwrap() []error {
errs := []error{ErrConversionIssue}
if e.err != nil {
switch {
Expand Down

0 comments on commit 214bd73

Please sign in to comment.