1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-09 16:35:12 +00:00
arikawa/internal/rfutil/rfutil.go
diamondburned 7961d18278
discord: ContainerComponents.Unmarshal for number types
This commit adds int*, uint* and float* parsing support into
ContainerComponents.Unmarshal.
2022-08-20 09:39:24 -07:00

46 lines
981 B
Go

package rfutil
import (
"errors"
"math/bits"
"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
}
// 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())
}
}