2022-08-15 21:57:30 +00:00
|
|
|
package rfutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-08-20 16:39:24 +00:00
|
|
|
"math/bits"
|
2022-08-15 21:57:30 +00:00
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
func StructValue(v interface{}) (reflect.Value, reflect.Type, error) {
|
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
return StructRValue(rv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func StructRValue(rv reflect.Value) (reflect.Value, reflect.Type, error) {
|
|
|
|
rt := rv.Type()
|
|
|
|
if rt.Kind() != reflect.Ptr {
|
|
|
|
return reflect.Value{}, nil, errors.New("v is not a pointer")
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = rv.Elem()
|
|
|
|
rt = rt.Elem()
|
|
|
|
if rt.Kind() != reflect.Struct {
|
|
|
|
return reflect.Value{}, nil, errors.New("v is not a pointer to a struct")
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv, rt, nil
|
|
|
|
}
|
2022-08-20 16:39:24 +00:00
|
|
|
|
|
|
|
// KindBits works on int*, uint* and float* only.
|
|
|
|
func KindBits(k reflect.Kind) int {
|
|
|
|
switch k {
|
|
|
|
case reflect.Int, reflect.Uint:
|
|
|
|
return bits.UintSize
|
|
|
|
case reflect.Int8, reflect.Uint8:
|
|
|
|
return 8
|
|
|
|
case reflect.Int16, reflect.Uint16:
|
|
|
|
return 16
|
|
|
|
case reflect.Int32, reflect.Uint32, reflect.Float32:
|
|
|
|
return 32
|
|
|
|
case reflect.Int64, reflect.Uint64, reflect.Float64:
|
|
|
|
return 64
|
|
|
|
default:
|
|
|
|
panic("unknown kind " + k.String())
|
|
|
|
}
|
|
|
|
}
|