mirror of https://github.com/VinGarcia/ksql.git
Add error check for nil pointers passed as arguments to Delete()
parent
dffba55a8b
commit
8b897d8ca9
7
ksql.go
7
ksql.go
|
@ -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 {
|
||||
|
|
12
ksql_test.go
12
ksql_test.go
|
@ -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)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue