mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395)
This pull request standardizes the use of the Go 1.18+ `any` type alias
instead of `interface{}` throughout the codebase. The change improves
code readability and aligns with modern Go best practices. The update
touches many files, including core data structures, code generation
templates, logging utilities, and test data, ensuring consistency across
all usages.
**Type alias migration to `any`:**
* Replaced all instances of `interface{}` with `any` in core data
structures such as `garray` and in generated model structs (e.g.,
`TableUser`, `User1`, `User2`) to modernize type usage.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
* Updated function signatures, method parameters, and return types from
`interface{}` to `any` in various parts of the codebase, including code
generation, service logic, and logging utilities (e.g., `mlog`).
[[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74)
[[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
[[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107)
[[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121)
[[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112)
[[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36)
[[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74)
[[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96)
**Generated code and templates:**
* Adjusted generated files and code generation templates to output `any`
instead of `interface{}` for relevant struct fields and function
signatures, ensuring that new code generation aligns with the updated
convention.
[[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
[[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
**Container and utility updates:**
* Refactored the `garray` container implementation and related
constructors/methods to use `[]any` instead of `[]interface{}`, along
with corresponding function signatures.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52)
[[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62)
[[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86)
[[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97)
[[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114)
[[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124)
[[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143)
[[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167)
These changes collectively modernize the codebase and prepare it for
future Go developments by using the idiomatic `any` type.
This commit is contained in:
@ -16,13 +16,13 @@ import (
|
||||
|
||||
// Interface for delegating copy process to type
|
||||
type Interface interface {
|
||||
DeepCopy() interface{}
|
||||
DeepCopy() any
|
||||
}
|
||||
|
||||
// Copy creates a deep copy of whatever is passed to it and returns the copy
|
||||
// in an interface{}. The returned value will need to be asserted to the
|
||||
// in an any. The returned value will need to be asserted to the
|
||||
// correct type.
|
||||
func Copy(src interface{}) interface{} {
|
||||
func Copy(src any) any {
|
||||
if src == nil {
|
||||
return nil
|
||||
}
|
||||
@ -66,7 +66,7 @@ func copyRecursive(original, cpy reflect.Value) {
|
||||
|
||||
// handle according to original's Kind
|
||||
switch original.Kind() {
|
||||
case reflect.Ptr:
|
||||
case reflect.Pointer:
|
||||
// Get the actual value being pointed to.
|
||||
originalValue := original.Elem()
|
||||
|
||||
|
||||
@ -112,19 +112,19 @@ CopyFloat32s:
|
||||
}
|
||||
|
||||
CopyInterfaces:
|
||||
Interfaces := []interface{}{"a", 42, true, 4.32}
|
||||
cpyIf := Copy(Interfaces).([]interface{})
|
||||
Interfaces := []any{"a", 42, true, 4.32}
|
||||
cpyIf := Copy(Interfaces).([]any)
|
||||
if (*reflect.SliceHeader)(unsafe.Pointer(&Strings)).Data == (*reflect.SliceHeader)(unsafe.Pointer(&cpyIf)).Data {
|
||||
t.Error("[]interfaces: expected SliceHeader data pointers to point to different locations, they didn't")
|
||||
return
|
||||
}
|
||||
if len(cpyIf) != len(Interfaces) {
|
||||
t.Errorf("[]interface{}: len was %d; want %d", len(cpyIf), len(Interfaces))
|
||||
t.Errorf("[]any: len was %d; want %d", len(cpyIf), len(Interfaces))
|
||||
return
|
||||
}
|
||||
for i, v := range Interfaces {
|
||||
if v != cpyIf[i] {
|
||||
t.Errorf("[]interface{}: got %v at index %d of the copy; want %v", cpyIf[i], i, v)
|
||||
t.Errorf("[]any: got %v at index %d of the copy; want %v", cpyIf[i], i, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -165,8 +165,8 @@ type Basics struct {
|
||||
Complex64s []complex64
|
||||
Complex128 complex128
|
||||
Complex128s []complex128
|
||||
Interface interface{}
|
||||
Interfaces []interface{}
|
||||
Interface any
|
||||
Interfaces []any
|
||||
}
|
||||
|
||||
// These tests test that all supported basic types are copied correctly. This
|
||||
@ -208,7 +208,7 @@ func TestMostTypes(t *testing.T) {
|
||||
Complex64s: []complex64{complex64(-65 + 11i), complex64(66 + 10i)},
|
||||
Complex128: complex128(-128 + 12i),
|
||||
Complex128s: []complex128{complex128(-128 + 11i), complex128(129 + 10i)},
|
||||
Interfaces: []interface{}{42, true, "pan-galactic"},
|
||||
Interfaces: []any{42, true, "pan-galactic"},
|
||||
}
|
||||
|
||||
cpy := Copy(test).(Basics)
|
||||
@ -1087,7 +1087,7 @@ type I struct {
|
||||
A string
|
||||
}
|
||||
|
||||
func (i *I) DeepCopy() interface{} {
|
||||
func (i *I) DeepCopy() any {
|
||||
return &I{A: "custom copy"}
|
||||
}
|
||||
|
||||
|
||||
@ -21,12 +21,12 @@ type iString interface {
|
||||
|
||||
// iInterfaces is used for type assert api for Interfaces.
|
||||
type iInterfaces interface {
|
||||
Interfaces() []interface{}
|
||||
Interfaces() []any
|
||||
}
|
||||
|
||||
// iMapStrAny is the interface support for converting struct parameter to map.
|
||||
type iMapStrAny interface {
|
||||
MapStrAny() map[string]interface{}
|
||||
MapStrAny() map[string]any
|
||||
}
|
||||
|
||||
type iTime interface {
|
||||
@ -41,7 +41,7 @@ type iTime interface {
|
||||
// The parameter `traceSource` is used for tracing to the source variable if given `value` is type of pointer
|
||||
// that also points to a pointer. It returns true if the source is empty when `traceSource` is true.
|
||||
// Note that it might use reflect feature which affects performance a little.
|
||||
func IsEmpty(value interface{}, traceSource ...bool) bool {
|
||||
func IsEmpty(value any, traceSource ...bool) bool {
|
||||
if value == nil {
|
||||
return true
|
||||
}
|
||||
@ -88,7 +88,7 @@ func IsEmpty(value interface{}, traceSource ...bool) bool {
|
||||
return len(result) == 0
|
||||
case []float64:
|
||||
return len(result) == 0
|
||||
case map[string]interface{}:
|
||||
case map[string]any:
|
||||
return len(result) == 0
|
||||
|
||||
default:
|
||||
@ -161,7 +161,7 @@ func IsEmpty(value interface{}, traceSource ...bool) bool {
|
||||
return rv.Len() == 0
|
||||
|
||||
case reflect.Struct:
|
||||
var fieldValueInterface interface{}
|
||||
var fieldValueInterface any
|
||||
for i := 0; i < rv.NumField(); i++ {
|
||||
fieldValueInterface, _ = reflection.ValueToInterface(rv.Field(i))
|
||||
if !IsEmpty(fieldValueInterface) {
|
||||
@ -177,7 +177,7 @@ func IsEmpty(value interface{}, traceSource ...bool) bool {
|
||||
reflect.Array:
|
||||
return rv.Len() == 0
|
||||
|
||||
case reflect.Ptr:
|
||||
case reflect.Pointer:
|
||||
if len(traceSource) > 0 && traceSource[0] {
|
||||
return IsEmpty(rv.Elem())
|
||||
}
|
||||
@ -198,11 +198,11 @@ func IsEmpty(value interface{}, traceSource ...bool) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// IsNil checks whether given `value` is nil, especially for interface{} type value.
|
||||
// IsNil checks whether given `value` is nil, especially for any type value.
|
||||
// Parameter `traceSource` is used for tracing to the source variable if given `value` is type of pointer
|
||||
// that also points to a pointer. It returns nil if the source is nil when `traceSource` is true.
|
||||
// Note that it might use reflect feature which affects performance a little.
|
||||
func IsNil(value interface{}, traceSource ...bool) bool {
|
||||
func IsNil(value any, traceSource ...bool) bool {
|
||||
if value == nil {
|
||||
return true
|
||||
}
|
||||
@ -221,15 +221,15 @@ func IsNil(value interface{}, traceSource ...bool) bool {
|
||||
reflect.UnsafePointer:
|
||||
return !rv.IsValid() || rv.IsNil()
|
||||
|
||||
case reflect.Ptr:
|
||||
case reflect.Pointer:
|
||||
if len(traceSource) > 0 && traceSource[0] {
|
||||
for rv.Kind() == reflect.Ptr {
|
||||
for rv.Kind() == reflect.Pointer {
|
||||
rv = rv.Elem()
|
||||
}
|
||||
if !rv.IsValid() {
|
||||
return true
|
||||
}
|
||||
if rv.Kind() == reflect.Ptr {
|
||||
if rv.Kind() == reflect.Pointer {
|
||||
return rv.IsNil()
|
||||
}
|
||||
} else {
|
||||
|
||||
@ -48,6 +48,6 @@ func (i *Info) ModTime() time.Time {
|
||||
return i.modTime
|
||||
}
|
||||
|
||||
func (i *Info) Sys() interface{} {
|
||||
func (i *Info) Sys() any {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -25,12 +25,12 @@ const (
|
||||
// string/[]byte/map/struct/*struct.
|
||||
//
|
||||
// The optional parameter `noUrlEncode` specifies whether ignore the url encoding for the data.
|
||||
func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) {
|
||||
func BuildParams(params any, noUrlEncode ...bool) (encodedParamStr string) {
|
||||
// If given string/[]byte, converts and returns it directly as string.
|
||||
switch v := params.(type) {
|
||||
case string, []byte:
|
||||
return gconv.String(params)
|
||||
case []interface{}:
|
||||
case []any:
|
||||
if len(v) > 0 {
|
||||
params = v[0]
|
||||
} else {
|
||||
@ -79,8 +79,8 @@ func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr strin
|
||||
}
|
||||
|
||||
// HeaderToMap coverts request headers to map.
|
||||
func HeaderToMap(header http.Header) map[string]interface{} {
|
||||
m := make(map[string]interface{})
|
||||
func HeaderToMap(header http.Header) map[string]any {
|
||||
m := make(map[string]any)
|
||||
for k, v := range header {
|
||||
if len(v) > 1 {
|
||||
m[k] = v
|
||||
|
||||
@ -33,25 +33,25 @@ func getGroup(key string) *gmap.StrAnyMap {
|
||||
}
|
||||
|
||||
// Get returns the instance by given name.
|
||||
func Get(name string) interface{} {
|
||||
func Get(name string) any {
|
||||
return getGroup(name).Get(name)
|
||||
}
|
||||
|
||||
// Set sets an instance to the instance manager with given name.
|
||||
func Set(name string, instance interface{}) {
|
||||
func Set(name string, instance any) {
|
||||
getGroup(name).Set(name, instance)
|
||||
}
|
||||
|
||||
// GetOrSet returns the instance by name,
|
||||
// or set instance to the instance manager if it does not exist and returns this instance.
|
||||
func GetOrSet(name string, instance interface{}) interface{} {
|
||||
func GetOrSet(name string, instance any) any {
|
||||
return getGroup(name).GetOrSet(name, instance)
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the instance by name,
|
||||
// or sets instance with returned value of callback function `f` if it does not exist
|
||||
// and then returns this instance.
|
||||
func GetOrSetFunc(name string, f func() interface{}) interface{} {
|
||||
func GetOrSetFunc(name string, f func() any) any {
|
||||
return getGroup(name).GetOrSetFunc(name, f)
|
||||
}
|
||||
|
||||
@ -61,13 +61,13 @@ func GetOrSetFunc(name string, f func() interface{}) interface{} {
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func GetOrSetFuncLock(name string, f func() interface{}) interface{} {
|
||||
func GetOrSetFuncLock(name string, f func() any) any {
|
||||
return getGroup(name).GetOrSetFuncLock(name, f)
|
||||
}
|
||||
|
||||
// SetIfNotExist sets `instance` to the map if the `name` does not exist, then returns true.
|
||||
// It returns false if `name` exists, and `instance` would be ignored.
|
||||
func SetIfNotExist(name string, instance interface{}) bool {
|
||||
func SetIfNotExist(name string, instance any) bool {
|
||||
return getGroup(name).SetIfNotExist(name, instance)
|
||||
}
|
||||
|
||||
|
||||
@ -24,13 +24,13 @@ func Test_SetGet(t *testing.T) {
|
||||
t.Assert(instance.Get("test-1"), 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(instance.GetOrSetFunc("test-2", func() interface{} {
|
||||
t.Assert(instance.GetOrSetFunc("test-2", func() any {
|
||||
return 2
|
||||
}), 2)
|
||||
t.Assert(instance.Get("test-2"), 2)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(instance.GetOrSetFuncLock("test-3", func() interface{} {
|
||||
t.Assert(instance.GetOrSetFuncLock("test-3", func() any {
|
||||
return 3
|
||||
}), 3)
|
||||
t.Assert(instance.Get("test-3"), 3)
|
||||
|
||||
@ -26,7 +26,7 @@ const (
|
||||
|
||||
// Print prints `v` with newline using fmt.Println.
|
||||
// The parameter `v` can be multiple variables.
|
||||
func Print(ctx context.Context, v ...interface{}) {
|
||||
func Print(ctx context.Context, v ...any) {
|
||||
if !utils.IsDebugEnabled() {
|
||||
return
|
||||
}
|
||||
@ -35,7 +35,7 @@ func Print(ctx context.Context, v ...interface{}) {
|
||||
|
||||
// Printf prints `v` with format `format` using fmt.Printf.
|
||||
// The parameter `v` can be multiple variables.
|
||||
func Printf(ctx context.Context, format string, v ...interface{}) {
|
||||
func Printf(ctx context.Context, format string, v ...any) {
|
||||
if !utils.IsDebugEnabled() {
|
||||
return
|
||||
}
|
||||
@ -44,7 +44,7 @@ func Printf(ctx context.Context, format string, v ...interface{}) {
|
||||
|
||||
// Error prints `v` with newline using fmt.Println.
|
||||
// The parameter `v` can be multiple variables.
|
||||
func Error(ctx context.Context, v ...interface{}) {
|
||||
func Error(ctx context.Context, v ...any) {
|
||||
if !utils.IsDebugEnabled() {
|
||||
return
|
||||
}
|
||||
@ -52,7 +52,7 @@ func Error(ctx context.Context, v ...interface{}) {
|
||||
}
|
||||
|
||||
// Errorf prints `v` with format `format` using fmt.Printf.
|
||||
func Errorf(ctx context.Context, format string, v ...interface{}) {
|
||||
func Errorf(ctx context.Context, format string, v ...any) {
|
||||
if !utils.IsDebugEnabled() {
|
||||
return
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ type RawMessage = json.RawMessage
|
||||
//
|
||||
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
|
||||
// Refer to https://godoc.org/encoding/json#Marshal for more information.
|
||||
func Marshal(v interface{}) (marshaledBytes []byte, err error) {
|
||||
func Marshal(v any) (marshaledBytes []byte, err error) {
|
||||
marshaledBytes, err = json.Marshal(v)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, `json.Marshal failed`)
|
||||
@ -33,7 +33,7 @@ func Marshal(v interface{}) (marshaledBytes []byte, err error) {
|
||||
}
|
||||
|
||||
// MarshalIndent same as json.MarshalIndent.
|
||||
func MarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error) {
|
||||
func MarshalIndent(v any, prefix, indent string) (marshaledBytes []byte, err error) {
|
||||
marshaledBytes, err = json.MarshalIndent(v, prefix, indent)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, `json.MarshalIndent failed`)
|
||||
@ -45,7 +45,7 @@ func MarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte,
|
||||
//
|
||||
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
|
||||
// Refer to https://godoc.org/encoding/json#Unmarshal for more information.
|
||||
func Unmarshal(data []byte, v interface{}) (err error) {
|
||||
func Unmarshal(data []byte, v any) (err error) {
|
||||
err = json.Unmarshal(data, v)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, `json.Unmarshal failed`)
|
||||
@ -54,7 +54,7 @@ func Unmarshal(data []byte, v interface{}) (err error) {
|
||||
}
|
||||
|
||||
// UnmarshalUseNumber decodes the json data bytes to target interface using number option.
|
||||
func UnmarshalUseNumber(data []byte, v interface{}) (err error) {
|
||||
func UnmarshalUseNumber(data []byte, v any) (err error) {
|
||||
decoder := NewDecoder(bytes.NewReader(data))
|
||||
decoder.UseNumber()
|
||||
err = decoder.Decode(v)
|
||||
|
||||
@ -19,7 +19,7 @@ type OriginValueAndKindOutput struct {
|
||||
}
|
||||
|
||||
// OriginValueAndKind retrieves and returns the original reflect value and kind.
|
||||
func OriginValueAndKind(value interface{}) (out OriginValueAndKindOutput) {
|
||||
func OriginValueAndKind(value any) (out OriginValueAndKindOutput) {
|
||||
if v, ok := value.(reflect.Value); ok {
|
||||
out.InputValue = v
|
||||
} else {
|
||||
@ -28,7 +28,7 @@ func OriginValueAndKind(value interface{}) (out OriginValueAndKindOutput) {
|
||||
out.InputKind = out.InputValue.Kind()
|
||||
out.OriginValue = out.InputValue
|
||||
out.OriginKind = out.InputKind
|
||||
for out.OriginKind == reflect.Ptr {
|
||||
for out.OriginKind == reflect.Pointer {
|
||||
out.OriginValue = out.OriginValue.Elem()
|
||||
out.OriginKind = out.OriginValue.Kind()
|
||||
}
|
||||
@ -43,7 +43,7 @@ type OriginTypeAndKindOutput struct {
|
||||
}
|
||||
|
||||
// OriginTypeAndKind retrieves and returns the original reflect type and kind.
|
||||
func OriginTypeAndKind(value interface{}) (out OriginTypeAndKindOutput) {
|
||||
func OriginTypeAndKind(value any) (out OriginTypeAndKindOutput) {
|
||||
if value == nil {
|
||||
return
|
||||
}
|
||||
@ -59,7 +59,7 @@ func OriginTypeAndKind(value interface{}) (out OriginTypeAndKindOutput) {
|
||||
out.InputKind = out.InputType.Kind()
|
||||
out.OriginType = out.InputType
|
||||
out.OriginKind = out.InputKind
|
||||
for out.OriginKind == reflect.Ptr {
|
||||
for out.OriginKind == reflect.Pointer {
|
||||
out.OriginType = out.OriginType.Elem()
|
||||
out.OriginKind = out.OriginType.Kind()
|
||||
}
|
||||
@ -67,7 +67,7 @@ func OriginTypeAndKind(value interface{}) (out OriginTypeAndKindOutput) {
|
||||
}
|
||||
|
||||
// ValueToInterface converts reflect value to its interface type.
|
||||
func ValueToInterface(v reflect.Value) (value interface{}, ok bool) {
|
||||
func ValueToInterface(v reflect.Value) (value any, ok bool) {
|
||||
if v.IsValid() && v.CanInterface() {
|
||||
return v.Interface(), true
|
||||
}
|
||||
@ -84,7 +84,7 @@ func ValueToInterface(v reflect.Value) (value interface{}, ok bool) {
|
||||
return v.Complex(), true
|
||||
case reflect.String:
|
||||
return v.String(), true
|
||||
case reflect.Ptr:
|
||||
case reflect.Pointer:
|
||||
return ValueToInterface(v.Elem())
|
||||
case reflect.Interface:
|
||||
return ValueToInterface(v.Elem())
|
||||
|
||||
@ -24,7 +24,7 @@ func Test_OriginValueAndKind(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var s = "s"
|
||||
out := reflection.OriginValueAndKind(&s)
|
||||
t.Assert(out.InputKind, reflect.Ptr)
|
||||
t.Assert(out.InputKind, reflect.Pointer)
|
||||
t.Assert(out.OriginKind, reflect.String)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
@ -36,7 +36,7 @@ func Test_OriginValueAndKind(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var s []int
|
||||
out := reflection.OriginValueAndKind(&s)
|
||||
t.Assert(out.InputKind, reflect.Ptr)
|
||||
t.Assert(out.InputKind, reflect.Pointer)
|
||||
t.Assert(out.OriginKind, reflect.Slice)
|
||||
})
|
||||
}
|
||||
@ -51,7 +51,7 @@ func Test_OriginTypeAndKind(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var s = "s"
|
||||
out := reflection.OriginTypeAndKind(&s)
|
||||
t.Assert(out.InputKind, reflect.Ptr)
|
||||
t.Assert(out.InputKind, reflect.Pointer)
|
||||
t.Assert(out.OriginKind, reflect.String)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
@ -63,7 +63,7 @@ func Test_OriginTypeAndKind(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var s []int
|
||||
out := reflection.OriginTypeAndKind(&s)
|
||||
t.Assert(out.InputKind, reflect.Ptr)
|
||||
t.Assert(out.InputKind, reflect.Pointer)
|
||||
t.Assert(out.OriginKind, reflect.Slice)
|
||||
})
|
||||
}
|
||||
|
||||
@ -10,10 +10,10 @@ import "reflect"
|
||||
|
||||
// IsArray checks whether given value is array/slice.
|
||||
// Note that it uses reflect internally implementing this feature.
|
||||
func IsArray(value interface{}) bool {
|
||||
func IsArray(value any) bool {
|
||||
rv := reflect.ValueOf(value)
|
||||
kind := rv.Kind()
|
||||
if kind == reflect.Ptr {
|
||||
if kind == reflect.Pointer {
|
||||
rv = rv.Elem()
|
||||
kind = rv.Kind()
|
||||
}
|
||||
|
||||
@ -12,18 +12,18 @@ import (
|
||||
"github.com/gogf/gf/v2/internal/empty"
|
||||
)
|
||||
|
||||
// IsNil checks whether `value` is nil, especially for interface{} type value.
|
||||
func IsNil(value interface{}) bool {
|
||||
// IsNil checks whether `value` is nil, especially for any type value.
|
||||
func IsNil(value any) bool {
|
||||
return empty.IsNil(value)
|
||||
}
|
||||
|
||||
// IsEmpty checks whether `value` is empty.
|
||||
func IsEmpty(value interface{}) bool {
|
||||
func IsEmpty(value any) bool {
|
||||
return empty.IsEmpty(value)
|
||||
}
|
||||
|
||||
// IsInt checks whether `value` is type of int.
|
||||
func IsInt(value interface{}) bool {
|
||||
func IsInt(value any) bool {
|
||||
switch value.(type) {
|
||||
case int, *int, int8, *int8, int16, *int16, int32, *int32, int64, *int64:
|
||||
return true
|
||||
@ -32,7 +32,7 @@ func IsInt(value interface{}) bool {
|
||||
}
|
||||
|
||||
// IsUint checks whether `value` is type of uint.
|
||||
func IsUint(value interface{}) bool {
|
||||
func IsUint(value any) bool {
|
||||
switch value.(type) {
|
||||
case uint, *uint, uint8, *uint8, uint16, *uint16, uint32, *uint32, uint64, *uint64:
|
||||
return true
|
||||
@ -41,7 +41,7 @@ func IsUint(value interface{}) bool {
|
||||
}
|
||||
|
||||
// IsFloat checks whether `value` is type of float.
|
||||
func IsFloat(value interface{}) bool {
|
||||
func IsFloat(value any) bool {
|
||||
switch value.(type) {
|
||||
case float32, *float32, float64, *float64:
|
||||
return true
|
||||
@ -50,12 +50,12 @@ func IsFloat(value interface{}) bool {
|
||||
}
|
||||
|
||||
// IsSlice checks whether `value` is type of slice.
|
||||
func IsSlice(value interface{}) bool {
|
||||
func IsSlice(value any) bool {
|
||||
var (
|
||||
reflectValue = reflect.ValueOf(value)
|
||||
reflectKind = reflectValue.Kind()
|
||||
)
|
||||
for reflectKind == reflect.Ptr {
|
||||
for reflectKind == reflect.Pointer {
|
||||
reflectValue = reflectValue.Elem()
|
||||
reflectKind = reflectValue.Kind()
|
||||
}
|
||||
@ -67,12 +67,12 @@ func IsSlice(value interface{}) bool {
|
||||
}
|
||||
|
||||
// IsMap checks whether `value` is type of map.
|
||||
func IsMap(value interface{}) bool {
|
||||
func IsMap(value any) bool {
|
||||
var (
|
||||
reflectValue = reflect.ValueOf(value)
|
||||
reflectKind = reflectValue.Kind()
|
||||
)
|
||||
for reflectKind == reflect.Ptr {
|
||||
for reflectKind == reflect.Pointer {
|
||||
reflectValue = reflectValue.Elem()
|
||||
reflectKind = reflectValue.Kind()
|
||||
}
|
||||
@ -84,13 +84,13 @@ func IsMap(value interface{}) bool {
|
||||
}
|
||||
|
||||
// IsStruct checks whether `value` is type of struct.
|
||||
func IsStruct(value interface{}) bool {
|
||||
func IsStruct(value any) bool {
|
||||
reflectType := reflect.TypeOf(value)
|
||||
if reflectType == nil {
|
||||
return false
|
||||
}
|
||||
reflectKind := reflectType.Kind()
|
||||
for reflectKind == reflect.Ptr {
|
||||
for reflectKind == reflect.Pointer {
|
||||
reflectType = reflectType.Elem()
|
||||
reflectKind = reflectType.Kind()
|
||||
}
|
||||
|
||||
@ -8,13 +8,13 @@ package utils
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ListToMapByKey converts `list` to a map[string]interface{} of which key is specified by `key`.
|
||||
// ListToMapByKey converts `list` to a map[string]any of which key is specified by `key`.
|
||||
// Note that the item value may be type of slice.
|
||||
func ListToMapByKey(list []map[string]interface{}, key string) map[string]interface{} {
|
||||
func ListToMapByKey(list []map[string]any, key string) map[string]any {
|
||||
var (
|
||||
s = ""
|
||||
m = make(map[string]interface{})
|
||||
tempMap = make(map[string][]interface{})
|
||||
m = make(map[string]any)
|
||||
tempMap = make(map[string][]any)
|
||||
hasMultiValues bool
|
||||
)
|
||||
for _, item := range list {
|
||||
|
||||
@ -9,7 +9,7 @@ package utils
|
||||
// MapPossibleItemByKey tries to find the possible key-value pair for given key ignoring cases and symbols.
|
||||
//
|
||||
// Note that this function might be of low performance.
|
||||
func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey string, foundValue interface{}) {
|
||||
func MapPossibleItemByKey(data map[string]any, key string) (foundKey string, foundValue any) {
|
||||
if len(data) == 0 {
|
||||
return
|
||||
}
|
||||
@ -29,7 +29,7 @@ func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey str
|
||||
// It checks the key ignoring cases and symbols.
|
||||
//
|
||||
// Note that this function might be of low performance.
|
||||
func MapContainsPossibleKey(data map[string]interface{}, key string) bool {
|
||||
func MapContainsPossibleKey(data map[string]any, key string) bool {
|
||||
if k, _ := MapPossibleItemByKey(data, key); k != "" {
|
||||
return true
|
||||
}
|
||||
|
||||
@ -12,13 +12,13 @@ import (
|
||||
|
||||
// CanCallIsNil Can reflect.Value call reflect.Value.IsNil.
|
||||
// It can avoid reflect.Value.IsNil panics.
|
||||
func CanCallIsNil(v interface{}) bool {
|
||||
func CanCallIsNil(v any) bool {
|
||||
rv, ok := v.(reflect.Value)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
switch rv.Kind() {
|
||||
case reflect.Interface, reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
|
||||
case reflect.Interface, reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.Slice, reflect.UnsafePointer:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user