From 157c90b1a6ba3d2b1ba42b98ffd721131f92d18a Mon Sep 17 00:00:00 2001 From: Semyon Krestyaninov <ensiouel@gmail.com> Date: Wed, 21 Feb 2024 15:02:02 +0300 Subject: [PATCH] fix --- id/object_id_test.go | 13 ++++++++++++- id/registry.go | 7 ++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/id/object_id_test.go b/id/object_id_test.go index 572f0da9..4f8d0181 100644 --- a/id/object_id_test.go +++ b/id/object_id_test.go @@ -3,6 +3,7 @@ package id import ( "testing" + "git.perx.ru/perxis/perxis-go/pkg/items" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -10,7 +11,7 @@ import ( func Test_ParseID(t *testing.T) { tests := []struct { name string - id string + id any result *ObjectId wantError bool }{ @@ -104,6 +105,16 @@ func Test_ParseID(t *testing.T) { result: nil, wantError: true, }, + { + name: "With error #6: nil value", + id: nil, + wantError: true, + }, + { + name: "With error #7: nil object value", + id: (*items.Item)(nil), + wantError: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/id/registry.go b/id/registry.go index e457b9ba..0ffec024 100644 --- a/id/registry.go +++ b/id/registry.go @@ -61,7 +61,12 @@ func (r *Registry) FromMap(m map[string]interface{}) (*ObjectId, error) { } func (r *Registry) FromObject(v interface{}) (*ObjectId, error) { - t := reflect.TypeOf(v) + value := reflect.ValueOf(v) + if v == nil || (value.Kind() == reflect.Ptr && value.IsNil()) { + return nil, fmt.Errorf("object value is nil") + } + + t := value.Type() if handler, ok := r.handlers[t]; ok { i := handler(v) if i == nil { -- GitLab