diff --git a/id/object_id_test.go b/id/object_id_test.go index 572f0da96c190a2138875c6b7dcb24c237fbe6fa..4f8d018139ce0b125e09124e5113e94c8031f5b3 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 e457b9baf639cafe88b1d72974a1e955fd711fab..0ffec0240952b742c33a4763c6ee49fa210ce4f0 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 {