From 808b8a4bbb2ea2128a11c402f2156dc0d1514e10 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 18 Dec 2024 00:33:32 +0100 Subject: [PATCH] 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. --- conversion.go | 23 +++++++++-------------- doc.go | 6 ++++++ errors.go | 34 +++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 25 deletions(-) create mode 100644 doc.go diff --git a/conversion.go b/conversion.go index 8039707..4c17095 100644 --- a/conversion.go +++ b/conversion.go @@ -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 ( @@ -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), } } @@ -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, @@ -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, @@ -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, @@ -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), } @@ -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), } @@ -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), } diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..67952ce --- /dev/null +++ b/doc.go @@ -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. diff --git a/errors.go b/errors.go index 605c42d..00e0f2c 100644 --- a/errors.go +++ b/errors.go @@ -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 { @@ -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 {