Add error check for nil pointers passed as arguments to Delete()

pull/7/head
Vinícius Garcia 2021-09-18 21:00:22 -03:00
parent dffba55a8b
commit 8b897d8ca9
2 changed files with 19 additions and 0 deletions

View File

@ -583,6 +583,13 @@ func normalizeIDsAsMaps(idNames []string, ids []interface{}) ([]map[string]inter
for i := range ids {
t := reflect.TypeOf(ids[i])
switch t.Kind() {
case reflect.Ptr:
v := reflect.ValueOf(ids[i])
if v.IsNil() {
return nil, fmt.Errorf("ksql: expected a valid pointer to struct as argument but received a nil pointer: %v", ids[i])
}
fallthrough
case reflect.Struct:
m, err := kstructs.StructToMap(ids[i])
if err != nil {

View File

@ -1018,6 +1018,18 @@ func TestDelete(t *testing.T) {
assert.Equal(t, 1, len(results))
assert.Equal(t, "This won't be deleted", results[0].Name)
})
t.Run("should report error if it receives a nil pointer to a struct", func(t *testing.T) {
db, closer := connectDB(t, config)
defer closer.Close()
ctx := context.Background()
c := newTestDB(db, config.driver)
var user *User
err := c.Delete(ctx, UsersTable, user)
assert.NotEqual(t, nil, err)
})
})
}
}