build(deps): bump github.com/gookit/config/v2 from 2.2.6 to 2.2.7
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.2.6 to 2.2.7. - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.6...v2.2.7) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-version: 2.2.7 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
+24
-5
@@ -5,6 +5,24 @@ import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// IsTimeType check is or alias of time.Time type
|
||||
func IsTimeType(t reflect.Type) bool {
|
||||
if t == nil || t.Kind() != reflect.Struct {
|
||||
return false
|
||||
}
|
||||
// t == timeType - 无法判断自定义类型
|
||||
return t == timeType || t.ConvertibleTo(timeType)
|
||||
}
|
||||
|
||||
// IsDurationType check is or alias of time.Duration type
|
||||
func IsDurationType(t reflect.Type) bool {
|
||||
if t == nil || t.Kind() != reflect.Int64 {
|
||||
return false
|
||||
}
|
||||
// t == durationType - 无法判断自定义类型
|
||||
return t == durationType || t.ConvertibleTo(durationType)
|
||||
}
|
||||
|
||||
// HasChild type check. eg: array, slice, map, struct
|
||||
func HasChild(v reflect.Value) bool {
|
||||
switch v.Kind() {
|
||||
@@ -15,7 +33,7 @@ func HasChild(v reflect.Value) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// IsArrayOrSlice check. eg: array, slice
|
||||
// IsArrayOrSlice type check. eg: array, slice
|
||||
func IsArrayOrSlice(k reflect.Kind) bool {
|
||||
return k == reflect.Slice || k == reflect.Array
|
||||
}
|
||||
@@ -70,8 +88,9 @@ func CanBeNil(typ reflect.Type) bool {
|
||||
return true
|
||||
case reflect.Struct:
|
||||
return typ == reflectValueType
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsFunc value
|
||||
@@ -84,7 +103,7 @@ func IsFunc(val any) bool {
|
||||
|
||||
// IsEqual determines if two objects are considered equal.
|
||||
//
|
||||
// TIP: cannot compare function type
|
||||
// TIP: cannot compare a function type
|
||||
func IsEqual(src, dst any) bool {
|
||||
if src == nil || dst == nil {
|
||||
return src == dst
|
||||
@@ -109,7 +128,7 @@ func IsEqual(src, dst any) bool {
|
||||
// IsZero reflect value check, alias of the IsEmpty()
|
||||
var IsZero = IsEmpty
|
||||
|
||||
// IsEmpty reflect value check
|
||||
// IsEmpty reflect value check. if is ptr, check if is nil
|
||||
func IsEmpty(v reflect.Value) bool {
|
||||
switch v.Kind() {
|
||||
case reflect.Invalid:
|
||||
@@ -140,7 +159,7 @@ var IsEmptyValue = IsEmptyReal
|
||||
//
|
||||
// Note:
|
||||
//
|
||||
// Difference the IsEmpty(), if value is ptr or interface, will check real elem.
|
||||
// Difference the IsEmpty(), if value is ptr or interface, will check real elem.
|
||||
//
|
||||
// From src/pkg/encoding/json/encode.go.
|
||||
func IsEmptyReal(v reflect.Value) bool {
|
||||
|
||||
+52
-15
@@ -50,44 +50,47 @@ func ConvToType(val any, typ reflect.Type) (rv reflect.Value, err error) {
|
||||
|
||||
// ValueByType create reflect.Value by give reflect.Type
|
||||
func ValueByType(val any, typ reflect.Type) (rv reflect.Value, err error) {
|
||||
// handle kind: string, bool, intX, uintX, floatX
|
||||
if typ.Kind() == reflect.String || typ.Kind() <= reflect.Float64 {
|
||||
return ConvToKind(val, typ.Kind())
|
||||
}
|
||||
|
||||
var ok bool
|
||||
var newRv reflect.Value
|
||||
if newRv, ok = val.(reflect.Value); !ok {
|
||||
newRv = reflect.ValueOf(val)
|
||||
}
|
||||
|
||||
// try auto convert slice type
|
||||
if IsArrayOrSlice(newRv.Kind()) && IsArrayOrSlice(typ.Kind()) {
|
||||
return ConvSlice(newRv, typ.Elem())
|
||||
// fix: check newRv is valid
|
||||
if !newRv.IsValid() {
|
||||
return rv, comdef.ErrConvType
|
||||
}
|
||||
|
||||
// check type. like map
|
||||
// check the same type. like map
|
||||
if newRv.Type() == typ {
|
||||
return newRv, nil
|
||||
}
|
||||
|
||||
// handle kind: string, bool, intX, uintX, floatX
|
||||
if typ.Kind() == reflect.String || typ.Kind() <= reflect.Float64 {
|
||||
return ConvToKind(val, typ.Kind())
|
||||
}
|
||||
|
||||
// try the auto convert slice type
|
||||
if IsArrayOrSlice(newRv.Kind()) && IsArrayOrSlice(typ.Kind()) {
|
||||
return ConvSlice(newRv, typ.Elem())
|
||||
}
|
||||
|
||||
err = comdef.ErrConvType
|
||||
return
|
||||
}
|
||||
|
||||
// ValueByKind convert and create reflect.Value by give reflect.Kind
|
||||
func ValueByKind(val any, kind reflect.Kind) (rv reflect.Value, err error) {
|
||||
return ConvToKind(val, kind)
|
||||
}
|
||||
func ValueByKind(val any, kind reflect.Kind) (reflect.Value, error) { return ConvToKind(val, kind) }
|
||||
|
||||
// ConvToKind convert and create reflect.Value by give reflect.Kind
|
||||
//
|
||||
// TIPs:
|
||||
//
|
||||
// Only support kind: string, bool, intX, uintX, floatX
|
||||
func ConvToKind(val any, kind reflect.Kind) (rv reflect.Value, err error) {
|
||||
if rv, ok := val.(reflect.Value); ok {
|
||||
val = rv.Interface()
|
||||
func ConvToKind(val any, kind reflect.Kind, fallback ...ConvFunc) (rv reflect.Value, err error) {
|
||||
if rv1, ok := val.(reflect.Value); ok {
|
||||
val = rv1.Interface()
|
||||
}
|
||||
|
||||
switch kind {
|
||||
@@ -185,6 +188,12 @@ func ConvToKind(val any, kind reflect.Kind) (rv reflect.Value, err error) {
|
||||
err = err1
|
||||
}
|
||||
default:
|
||||
// call fallback func
|
||||
if len(fallback) > 0 && fallback[0] != nil {
|
||||
rv, err = fallback[0](val, kind)
|
||||
} else {
|
||||
err = comdef.ErrConvType
|
||||
}
|
||||
err = comdef.ErrConvType
|
||||
}
|
||||
return
|
||||
@@ -254,3 +263,31 @@ func ValToString(rv reflect.Value, defaultAsErr bool) (str string, err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ToTimeOrDuration convert string to time.Time or time.Duration type
|
||||
//
|
||||
// If the target type is not match, return the input string.
|
||||
func ToTimeOrDuration(str string, typ reflect.Type) (any, error) {
|
||||
// datetime, time, duration string should not greater than 64
|
||||
if len(str) > 64 {
|
||||
return str, nil
|
||||
}
|
||||
var anyVal any = str
|
||||
|
||||
// time.Time date string
|
||||
if len(str) > 5 && IsTimeType(typ) {
|
||||
ttVal, err := strutil.ToTime(str)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
anyVal = ttVal
|
||||
} else if IsDurationType(typ) {
|
||||
dVal, err := strutil.ToDuration(str)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
anyVal = dVal
|
||||
}
|
||||
|
||||
return anyVal, nil
|
||||
}
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/gookit/goutil/basefn"
|
||||
"github.com/gookit/goutil/x/basefn"
|
||||
)
|
||||
|
||||
// FuncX wrap a go func. represent a function
|
||||
@@ -86,7 +86,7 @@ func (f *FuncX) Call(args ...any) ([]any, error) {
|
||||
// The function must return 1 result, or 2 results, the second of which is an error.
|
||||
//
|
||||
// - Only support func with 1 or 2 return values: (val) OR (val, err)
|
||||
// - Will check args and try convert input args to func args type.
|
||||
// - Will check args and try to convert input args to func args type.
|
||||
func (f *FuncX) Call2(args ...any) (any, error) {
|
||||
// convert args to []reflect.Value
|
||||
argRvs := make([]reflect.Value, len(args))
|
||||
|
||||
+16
-4
@@ -1,27 +1,39 @@
|
||||
package reflects
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// TryAnyMap convert map[TYPE1]TYPE2 to map[string]any
|
||||
func TryAnyMap(mp reflect.Value) (map[string]any, error) {
|
||||
saMap := make(map[string]any)
|
||||
err := EachStrAnyMap(mp, func(key string, val any) {
|
||||
saMap[key] = val
|
||||
})
|
||||
|
||||
return saMap, err
|
||||
}
|
||||
|
||||
// EachMap process any map data
|
||||
func EachMap(mp reflect.Value, fn func(key, val reflect.Value)) {
|
||||
func EachMap(mp reflect.Value, fn func(key, val reflect.Value)) (err error) {
|
||||
if fn == nil {
|
||||
return
|
||||
}
|
||||
if mp.Kind() != reflect.Map {
|
||||
panic("only allow map value data")
|
||||
return errors.New("EachMap: only allow map value data")
|
||||
}
|
||||
|
||||
for _, key := range mp.MapKeys() {
|
||||
fn(key, mp.MapIndex(key))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// EachStrAnyMap process any map data as string key and any value
|
||||
func EachStrAnyMap(mp reflect.Value, fn func(key string, val any)) {
|
||||
EachMap(mp, func(key, val reflect.Value) {
|
||||
func EachStrAnyMap(mp reflect.Value, fn func(key string, val any)) error {
|
||||
return EachMap(mp, func(key, val reflect.Value) {
|
||||
fn(String(key), val.Interface())
|
||||
})
|
||||
}
|
||||
|
||||
+9
@@ -3,6 +3,7 @@ package reflects
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
var emptyValue = reflect.Value{}
|
||||
@@ -11,6 +12,14 @@ var (
|
||||
anyType = reflect.TypeOf((*any)(nil)).Elem()
|
||||
errorType = reflect.TypeOf((*error)(nil)).Elem()
|
||||
|
||||
// time.Time type
|
||||
timeType = reflect.TypeOf(time.Time{})
|
||||
// time.Duration type
|
||||
durationType = reflect.TypeOf(time.Duration(0))
|
||||
|
||||
// fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
|
||||
reflectValueType = reflect.TypeOf((*reflect.Value)(nil)).Elem()
|
||||
)
|
||||
|
||||
// ConvFunc custom func convert input value to kind reflect.Value
|
||||
type ConvFunc func(val any, kind reflect.Kind) (reflect.Value, error)
|
||||
|
||||
+4
-5
@@ -98,10 +98,9 @@ func Len(v reflect.Value) int {
|
||||
return len(strconv.FormatInt(v.Int(), 10))
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return len(fmt.Sprint(v.Interface()))
|
||||
default:
|
||||
return -1 // cannot get length
|
||||
}
|
||||
|
||||
// cannot get length
|
||||
return -1
|
||||
}
|
||||
|
||||
// SliceSubKind get sub-elem kind of the array, slice, variadic-var. alias SliceElemKind()
|
||||
@@ -152,7 +151,7 @@ func SetUnexportedValue(rv reflect.Value, value any) {
|
||||
|
||||
// SetValue to a `reflect.Value`. will auto convert type if needed.
|
||||
func SetValue(rv reflect.Value, val any) error {
|
||||
// get real type of the ptr value
|
||||
// get a real type of the ptr value
|
||||
if rv.Kind() == reflect.Ptr {
|
||||
if rv.IsNil() {
|
||||
elemTyp := rv.Type().Elem()
|
||||
@@ -170,7 +169,7 @@ func SetValue(rv reflect.Value, val any) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// SetRValue to a `reflect.Value`. will direct set value without convert type.
|
||||
// SetRValue to a `reflect.Value`. will direct set value without a type convert.
|
||||
func SetRValue(rv, val reflect.Value) {
|
||||
if rv.Kind() == reflect.Ptr {
|
||||
if rv.IsNil() {
|
||||
|
||||
Reference in New Issue
Block a user