Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/error code 迁移到各自的包下 #67

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ func main() {

## Sponsorship and support

`GoEasyUtils` Thank JetBrains for their support
`go-easy-utils` Thank JetBrains for their support

<a href="https://www.jetbrains.com"><img src="https://raw.githubusercontent.com/panjf2000/illustrations/master/jetbrains/jetbrains-variant-4.png" height="100" alt="JetBrains"/></a>
7 changes: 3 additions & 4 deletions anyUtil/any_to_float_x.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package anyUtil

import (
"github.com/jefferyjob/go-easy-utils/v2"
"math"
"reflect"
"strconv"
Expand All @@ -14,7 +13,7 @@ func AnyToFloat32(i any) (float32, error) {
return 0, err
}
if f64 < -math.MaxFloat32 || f64 > math.MaxFloat32 {
return 0, go_easy_utils.ErrValOut
return 0, ErrValOut
}
return float32(f64), nil
}
Expand All @@ -39,7 +38,7 @@ func AnyToFloat64(i any) (float64, error) {
case reflect.String:
floatValue, err := strconv.ParseFloat(v.String(), 64)
if err != nil {
return 0, go_easy_utils.ErrSyntax
return 0, ErrSyntax
}
return floatValue, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
Expand All @@ -55,6 +54,6 @@ func AnyToFloat64(i any) (float64, error) {
return 0, nil
}
default:
return 0, go_easy_utils.ErrType
return 0, ErrType
}
}
9 changes: 4 additions & 5 deletions anyUtil/any_to_float_x_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package anyUtil

import (
go_easy_utils "github.com/jefferyjob/go-easy-utils/v2"
"math"
"testing"
)
Expand Down Expand Up @@ -30,8 +29,8 @@ func TestAnyToFloat32(t *testing.T) {
{uint32(32), 32, nil},
{uint64(64), 64, nil},
{"3.14", 3.14, nil},
{math.MaxFloat64, 0, go_easy_utils.ErrValOut},
{make(chan int), 0, go_easy_utils.ErrType},
{math.MaxFloat64, 0, ErrValOut},
{make(chan int), 0, ErrType},
}

for _, test := range tests {
Expand Down Expand Up @@ -79,8 +78,8 @@ func TestAnyToFloat64(t *testing.T) {
{complex128(1 + 2i), 1, nil},
{&iPtr, 90, nil},
{(*int)(nil), 0, nil},
{"abc", 0, go_easy_utils.ErrSyntax},
{[]int{}, 0, go_easy_utils.ErrType},
{"abc", 0, ErrSyntax},
{[]int{}, 0, ErrType},
}

for _, test := range tests {
Expand Down
13 changes: 6 additions & 7 deletions anyUtil/any_to_int_x.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package anyUtil

import (
"github.com/jefferyjob/go-easy-utils/v2"
"math"
"reflect"
"strconv"
Expand All @@ -16,7 +15,7 @@ func AnyToInt(i any) (int, error) {

// int 兼容32位和64位系统
if int64(int(v)) != v {
return 0, go_easy_utils.ErrValOut
return 0, ErrValOut
}

return int(v), nil
Expand All @@ -29,7 +28,7 @@ func AnyToInt8(i any) (int8, error) {
return 0, err
}
if value < math.MinInt8 || value > math.MaxInt8 {
return 0, go_easy_utils.ErrValOut
return 0, ErrValOut
}
return int8(value), nil
}
Expand All @@ -41,7 +40,7 @@ func AnyToInt16(i any) (int16, error) {
return 0, err
}
if value < math.MinInt16 || value > math.MaxInt16 {
return 0, go_easy_utils.ErrValOut
return 0, ErrValOut
}
return int16(value), nil
}
Expand All @@ -53,7 +52,7 @@ func AnyToInt32(i any) (int32, error) {
return 0, err
}
if value < math.MinInt32 || value > math.MaxInt32 {
return 0, go_easy_utils.ErrValOut
return 0, ErrValOut
}
return int32(value), nil
}
Expand All @@ -78,7 +77,7 @@ func AnyToInt64(i any) (int64, error) {
case reflect.String:
intValue, err := strconv.ParseInt(v.String(), 10, 64)
if err != nil {
return 0, go_easy_utils.ErrSyntax
return 0, ErrSyntax
}
return intValue, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
Expand All @@ -96,6 +95,6 @@ func AnyToInt64(i any) (int64, error) {
return 0, nil
}
default:
return 0, go_easy_utils.ErrType
return 0, ErrType
}
}
23 changes: 11 additions & 12 deletions anyUtil/any_to_int_x_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package anyUtil

import (
"errors"
"github.com/jefferyjob/go-easy-utils/v2"
"math"
"testing"
)
Expand Down Expand Up @@ -32,7 +31,7 @@ func TestAnyToInt(t *testing.T) {
{input: float32(123.45), expected: 123, err: nil},
{input: float64(123.45), expected: 123, err: nil},
{input: "123", expected: 123, err: nil},
{input: "abc", expected: 0, err: go_easy_utils.ErrSyntax},
{input: "abc", expected: 0, err: ErrSyntax},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -83,13 +82,13 @@ func TestAnyToInt8(t *testing.T) {
name: "positive integer out of range",
input: 128,
expected: 0,
err: go_easy_utils.ErrValOut,
err: ErrValOut,
},
{
name: "negative integer out of range",
input: -129,
expected: 0,
err: go_easy_utils.ErrValOut,
err: ErrValOut,
},
{
name: "float",
Expand All @@ -107,13 +106,13 @@ func TestAnyToInt8(t *testing.T) {
name: "string out of range",
input: "128",
expected: 0,
err: go_easy_utils.ErrValOut,
err: ErrValOut,
},
{
name: "invalid string",
input: "invalid",
expected: 0,
err: go_easy_utils.ErrSyntax,
err: ErrSyntax,
},
}

Expand Down Expand Up @@ -159,8 +158,8 @@ func TestAnyToInt16(t *testing.T) {
{float32(123.45), 123, nil},
{float64(123.45), 123, nil},
{"12345", 12345, nil},
{math.MinInt16 - 1, 0, go_easy_utils.ErrValOut},
{"not a number", 0, go_easy_utils.ErrSyntax},
{math.MinInt16 - 1, 0, ErrValOut},
{"not a number", 0, ErrSyntax},
}

for _, tt := range tests {
Expand All @@ -184,13 +183,13 @@ func TestAnyToInt32(t *testing.T) {
}{
{int(123), 123, nil},
{int64(2147483647), 2147483647, nil},
{int64(2147483648), 0, go_easy_utils.ErrValOut},
{int64(2147483648), 0, ErrValOut},
{int64(-2147483648), -2147483648, nil},
{int64(-2147483649), 0, go_easy_utils.ErrValOut},
{int64(-2147483649), 0, ErrValOut},
{float64(123.45), 123, nil},
{"123", 123, nil},
{"-2147483649", 0, go_easy_utils.ErrValOut},
{struct{}{}, 0, go_easy_utils.ErrType},
{"-2147483649", 0, ErrValOut},
{struct{}{}, 0, ErrType},
}

for _, testCase := range testCases {
Expand Down
19 changes: 9 additions & 10 deletions anyUtil/any_to_uint_x.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package anyUtil

import (
"github.com/jefferyjob/go-easy-utils/v2"
"math"
"reflect"
"strconv"
Expand All @@ -16,7 +15,7 @@ func AnyToUint(i any) (uint, error) {

// uint 兼容32位和64位系统
if uint64(uint(v)) != v {
return 0, go_easy_utils.ErrValOut
return 0, ErrValOut
}

return uint(v), nil
Expand All @@ -29,7 +28,7 @@ func AnyToUint8(i any) (uint8, error) {
return 0, err
}
if value > math.MaxUint8 {
return 0, go_easy_utils.ErrValOut
return 0, ErrValOut
}
return uint8(value), nil
}
Expand All @@ -41,7 +40,7 @@ func AnyToUint16(i any) (uint16, error) {
return 0, err
}
if value > math.MaxUint16 {
return 0, go_easy_utils.ErrValOut
return 0, ErrValOut
}
return uint16(value), nil
}
Expand All @@ -53,7 +52,7 @@ func AnyToUint32(i any) (uint32, error) {
return 0, err
}
if value > math.MaxUint32 {
return 0, go_easy_utils.ErrValOut
return 0, ErrValOut
}
return uint32(value), nil
}
Expand All @@ -78,26 +77,26 @@ func AnyToUint64(i any) (uint64, error) {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intValue := v.Int()
if intValue < 0 {
return 0, go_easy_utils.ErrUnsignedInt
return 0, ErrUnsignedInt
}
return uint64(intValue), nil
case reflect.Float32, reflect.Float64:
floatValue := v.Float()
if floatValue < 0 {
return 0, go_easy_utils.ErrUnsignedInt
return 0, ErrUnsignedInt
}
return uint64(floatValue), nil
case reflect.Complex64, reflect.Complex128:
realValue := real(v.Complex())
if realValue < 0 {
return 0, go_easy_utils.ErrUnsignedInt
return 0, ErrUnsignedInt
}
return uint64(realValue), nil
case reflect.String:
strValue := v.String()
uintValue, err := strconv.ParseUint(strValue, 10, 64)
if err != nil {
return 0, go_easy_utils.ErrSyntax
return 0, ErrSyntax
}
return uintValue, nil
case reflect.Bool:
Expand All @@ -107,6 +106,6 @@ func AnyToUint64(i any) (uint64, error) {
return 0, nil
}
default:
return 0, go_easy_utils.ErrType
return 0, ErrType
}
}
Loading
Loading