From a2fcd25223291a9f82af5892a2581c7a8f1ea3a0 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 18 Sep 2024 17:40:05 +0200 Subject: [PATCH] chore: refactor code - remove assertNotNegative - use checkLowerBoundary --- asserters.go | 11 ----------- conversion.go | 10 +++++----- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/asserters.go b/asserters.go index 60aa9dd..57993b5 100644 --- a/asserters.go +++ b/asserters.go @@ -1,16 +1,5 @@ package safecast -func assertNotNegative[T Type, T2 Type](i T, zero T2) error { - if i < 0 { - return Error{ - err: ErrExceedMinimumValue, - value: i, - boundary: zero, - } - } - return nil -} - func checkUpperBoundary[T Type, T2 Type](value T, boundary T2) error { if value <= 0 { return nil diff --git a/conversion.go b/conversion.go index 51c9d70..4b16570 100644 --- a/conversion.go +++ b/conversion.go @@ -26,7 +26,7 @@ func ToInt[T Type](i T) (int, error) { // If the conversion results in a value outside the range of an uint, // an [ErrConversionIssue] error is returned. func ToUint[T Type](i T) (uint, error) { - if err := assertNotNegative(i, uint(0)); err != nil { + if err := checkLowerBoundary(i, uint(0)); err != nil { return 0, err } @@ -56,7 +56,7 @@ func ToInt8[T Type](i T) (int8, error) { // If the conversion results in a value outside the range of an uint8, // an [ErrConversionIssue] error is returned. func ToUint8[T Type](i T) (uint8, error) { - if err := assertNotNegative(i, uint8(0)); err != nil { + if err := checkLowerBoundary(i, uint8(0)); err != nil { return 0, err } @@ -86,7 +86,7 @@ func ToInt16[T Type](i T) (int16, error) { // If the conversion results in a value outside the range of an uint16, // an [ErrConversionIssue] error is returned. func ToUint16[T Type](i T) (uint16, error) { - if err := assertNotNegative(i, uint16(0)); err != nil { + if err := checkLowerBoundary(i, uint16(0)); err != nil { return 0, err } @@ -116,7 +116,7 @@ func ToInt32[T Type](i T) (int32, error) { // If the conversion results in a value outside the range of an uint32, // an [ErrConversionIssue] error is returned. func ToUint32[T Type](i T) (uint32, error) { - if err := assertNotNegative(i, uint32(0)); err != nil { + if err := checkLowerBoundary(i, uint32(0)); err != nil { return 0, err } @@ -142,7 +142,7 @@ func ToInt64[T Type](i T) (int64, error) { // If the conversion results in a value outside the range of an uint64, // an [ErrConversionIssue] error is returned. func ToUint64[T Type](i T) (uint64, error) { - if err := assertNotNegative(i, uint64(0)); err != nil { + if err := checkLowerBoundary(i, uint64(0)); err != nil { return 0, err }