gf/internal/utils/utils_reflect.go

62 lines
1.7 KiB
Go
Raw Normal View History

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package utils
import "reflect"
type OriginValueAndKindOutput struct {
InputValue reflect.Value
InputKind reflect.Kind
OriginValue reflect.Value
OriginKind reflect.Kind
}
// OriginValueAndKind retrieves and returns the original reflect value and kind.
func OriginValueAndKind(value interface{}) (out OriginValueAndKindOutput) {
if v, ok := value.(reflect.Value); ok {
out.InputValue = v
} else {
out.InputValue = reflect.ValueOf(value)
}
out.InputKind = out.InputValue.Kind()
out.OriginValue = out.InputValue
out.OriginKind = out.InputKind
for out.OriginKind == reflect.Ptr {
out.OriginValue = out.OriginValue.Elem()
out.OriginKind = out.OriginValue.Kind()
}
return
}
type OriginTypeAndKindOutput struct {
InputType reflect.Type
InputKind reflect.Kind
OriginType reflect.Type
OriginKind reflect.Kind
}
// OriginTypeAndKind retrieves and returns the original reflect type and kind.
func OriginTypeAndKind(value interface{}) (out OriginTypeAndKindOutput) {
if reflectType, ok := value.(reflect.Type); ok {
out.InputType = reflectType
} else {
if reflectValue, ok := value.(reflect.Value); ok {
out.InputType = reflectValue.Type()
} else {
out.InputType = reflect.TypeOf(value)
}
}
out.InputKind = out.InputType.Kind()
out.OriginType = out.InputType
out.OriginKind = out.InputKind
for out.OriginKind == reflect.Ptr {
out.OriginType = out.OriginType.Elem()
out.OriginKind = out.OriginType.Kind()
}
return
}