chore: handle empty map or slices

This commit is contained in:
andig 2024-07-22 10:47:49 +02:00
parent 5083645b95
commit cbb1319ebc

View file

@ -198,8 +198,17 @@ func Json(key string, res any) error {
func DecodeOtherSliceOrMap(other, res any) error {
var len int
if typ := reflect.TypeOf(other); typ.Kind() == reflect.Slice || typ.Kind() == reflect.Map {
len = reflect.ValueOf(other).Len()
val := reflect.ValueOf(other)
typ := reflect.TypeOf(other)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = reflect.Indirect(val)
}
if typ.Kind() == reflect.Slice || typ.Kind() == reflect.Map {
len = val.Len()
} else {
return fmt.Errorf("cannot decode into slice or map: %v", other)
}