Skip to content

Commit

Permalink
👔 up: reflects,internal - update some internal and reflects util func
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 4, 2024
1 parent d397d97 commit 154fd5b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 17 deletions.
16 changes: 13 additions & 3 deletions errorx/panics/assert.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package panics

import (
"github.com/gookit/goutil/internal/checkfn"
"reflect"

"github.com/gookit/goutil/internal/comfunc"
"github.com/gookit/goutil/reflects"
)

// IsTrue assert result is true, otherwise will panic
Expand Down Expand Up @@ -35,14 +37,14 @@ func NotNil(result any, fmtAndArgs ...any) {

// IsEmpty assert result is empty, otherwise will panic
func IsEmpty(result any, fmtAndArgs ...any) {
if !checkfn.IsEmpty(result) {
if !isEmpty(result) {
panicWithMsg("result should be empty", fmtAndArgs)
}
}

// NotEmpty assert result is empty, otherwise will panic
func NotEmpty(result any, fmtAndArgs ...any) {
if checkfn.IsEmpty(result) {
if isEmpty(result) {
panicWithMsg("result should not be empty", fmtAndArgs)
}
}
Expand All @@ -53,3 +55,11 @@ func panicWithMsg(errMsg string, fmtAndArgs []any) {
}
panic(errMsg)
}

// IsEmpty value check
func isEmpty(v any) bool {
if v == nil {
return true
}
return reflects.IsEmpty(reflect.ValueOf(v))
}
48 changes: 39 additions & 9 deletions internal/checkfn/check.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,57 @@
package checkfn

import (
"bytes"
"fmt"
"reflect"
"strings"

"github.com/gookit/goutil/reflects"
)

// IsNil value check
func IsNil(v any) bool {
if v == nil {
return true
}
return reflects.IsNil(reflect.ValueOf(v))

rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return rv.IsNil()
default:
return false
}
}

// IsEmpty value check
func IsEmpty(v any) bool {
if v == nil {
// IsSimpleKind kind in: string, bool, intX, uintX, floatX
func IsSimpleKind(k reflect.Kind) bool {
if reflect.String == k {
return true
}
return reflects.IsEmpty(reflect.ValueOf(v))
return k > reflect.Invalid && k <= reflect.Float64
}

// IsEqual determines if two objects are considered equal.
//
// TIP: cannot compare function type
func IsEqual(src, dst any) bool {
if src == nil || dst == nil {
return src == dst
}

bs1, ok := src.([]byte)
if !ok {
return reflect.DeepEqual(src, dst)
}

bs2, ok := dst.([]byte)
if !ok {
return false
}

if bs1 == nil || bs2 == nil {
return bs1 == nil && bs2 == nil
}
return bytes.Equal(bs1, bs2)
}

// Contains try loop over the data check if the data includes the element.
Expand Down Expand Up @@ -57,7 +87,7 @@ func Contains(data, elem any) (valid, found bool) {
if dataKind == reflect.Map {
mapKeys := dataRv.MapKeys()
for i := 0; i < len(mapKeys); i++ {
if reflects.IsEqual(mapKeys[i].Interface(), elem) {
if IsEqual(mapKeys[i].Interface(), elem) {
return true, true
}
}
Expand All @@ -70,7 +100,7 @@ func Contains(data, elem any) (valid, found bool) {
}

for i := 0; i < dataRv.Len(); i++ {
if reflects.IsEqual(dataRv.Index(i).Interface(), elem) {
if IsEqual(dataRv.Index(i).Interface(), elem) {
return true, true
}
}
Expand Down
16 changes: 16 additions & 0 deletions internal/comfunc/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package comfunc

import (
"fmt"
"reflect"
"strconv"
"strings"
"time"

"github.com/gookit/goutil/comdef"
"github.com/gookit/goutil/internal/checkfn"
)

// Bool try to convert type to bool
Expand Down Expand Up @@ -68,6 +70,9 @@ type ConvOption struct {
// if ture: value is nil, will return convert error;
// if false(default): value is nil, will convert to zero value
NilAsFail bool
// EnablePtr auto convert ptr type(int,float,string) value. eg: *int to int
// - if true: will use real type try convert. default is false
EnablePtr bool
// set custom fallback convert func for not supported type.
UserConvFn comdef.ToStringFunc
}
Expand Down Expand Up @@ -139,6 +144,8 @@ func ToStringWith(in any, optFns ...ConvOptionFn) (str string, err error) {
str = strconv.FormatBool(value)
case string:
str = value
case *string:
str = *value
case []byte:
str = string(value)
case time.Duration:
Expand All @@ -148,6 +155,15 @@ func ToStringWith(in any, optFns ...ConvOptionFn) (str string, err error) {
case error:
str = value.Error()
default:
if opt.EnablePtr {
if rv := reflect.ValueOf(in); rv.Kind() == reflect.Pointer {
rv = rv.Elem()
if checkfn.IsSimpleKind(rv.Kind()) {
return ToStringWith(rv.Interface(), optFns...)
}
}
}

if opt.UserConvFn != nil {
str, err = opt.UserConvFn(in)
} else {
Expand Down
11 changes: 6 additions & 5 deletions reflects/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ func HasChild(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.Struct:
return true
default:
return false
}
return false
}

// IsArrayOrSlice check. eg: array, slice
Expand Down Expand Up @@ -122,9 +123,9 @@ func IsEmpty(v reflect.Value) bool {
return v.Float() == 0
case reflect.Interface, reflect.Ptr, reflect.Func:
return v.IsNil()
default:
return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}

return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}

// IsEmptyValue reflect value check, alias of the IsEmptyReal()
Expand Down Expand Up @@ -158,7 +159,7 @@ func IsEmptyReal(v reflect.Value) bool {
return v.IsNil()
case reflect.Invalid:
return true
default:
return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}

return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}

0 comments on commit 154fd5b

Please sign in to comment.