From f0e26b1c0d984e5380a394be6c0410b57d8463ee Mon Sep 17 00:00:00 2001 From: Hein Date: Tue, 11 Nov 2025 17:07:44 +0200 Subject: [PATCH] Fixed and refactored reflection.Len --- pkg/{common/reflection_utils.go => reflection/helpers.go} | 8 +++++++- pkg/restheadspec/handler.go | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) rename pkg/{common/reflection_utils.go => reflection/helpers.go} (69%) diff --git a/pkg/common/reflection_utils.go b/pkg/reflection/helpers.go similarity index 69% rename from pkg/common/reflection_utils.go rename to pkg/reflection/helpers.go index 712c046..fe9520c 100644 --- a/pkg/common/reflection_utils.go +++ b/pkg/reflection/helpers.go @@ -1,9 +1,15 @@ -package common +package reflection import "reflect" func Len(v any) int { val := reflect.ValueOf(v) + valKind := val.Kind() + + if valKind == reflect.Ptr { + val = val.Elem() + } + switch val.Kind() { case reflect.Slice, reflect.Array, reflect.Map, reflect.String, reflect.Chan: return val.Len() diff --git a/pkg/restheadspec/handler.go b/pkg/restheadspec/handler.go index 770fe06..b96e9ce 100644 --- a/pkg/restheadspec/handler.go +++ b/pkg/restheadspec/handler.go @@ -523,7 +523,7 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st metadata := &common.Metadata{ Total: int64(total), - Count: int64(common.Len(modelPtr)), + Count: int64(reflection.Len(modelPtr)), Filtered: int64(total), Limit: limit, Offset: offset,