Skip to content
Snippets Groups Projects
Commit 189cc197 authored by Pavel Antonov's avatar Pavel Antonov :asterisk:
Browse files

Merge branch 'fix/PRXS-951-FixNilInterface' into 'feature/PRXS-951-Log'

Добавлена проверка на пустой интерфейс при парсинге ObjectId

See merge request perxis/perxis-go!170
parents 4f1deb3c 157c90b1
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ package id ...@@ -3,6 +3,7 @@ package id
import ( import (
"testing" "testing"
"git.perx.ru/perxis/perxis-go/pkg/items"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
...@@ -10,7 +11,7 @@ import ( ...@@ -10,7 +11,7 @@ import (
func Test_ParseID(t *testing.T) { func Test_ParseID(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
id string id any
result *ObjectId result *ObjectId
wantError bool wantError bool
}{ }{
...@@ -104,6 +105,16 @@ func Test_ParseID(t *testing.T) { ...@@ -104,6 +105,16 @@ func Test_ParseID(t *testing.T) {
result: nil, result: nil,
wantError: true, 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 { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
......
...@@ -61,7 +61,12 @@ func (r *Registry) FromMap(m map[string]interface{}) (*ObjectId, error) { ...@@ -61,7 +61,12 @@ func (r *Registry) FromMap(m map[string]interface{}) (*ObjectId, error) {
} }
func (r *Registry) FromObject(v 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 { if handler, ok := r.handlers[t]; ok {
i := handler(v) i := handler(v)
if i == nil { if i == nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment