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

pull/7/head
Vinícius Garcia 2021-09-18 18:29:00 -03:00
parent e7e404dc86
commit 1cf671cd33
2 changed files with 16 additions and 0 deletions

View File

@ -432,6 +432,10 @@ func (c DB) Insert(
)
}
if v.IsNil() {
return fmt.Errorf("ksql: expected a valid pointer to struct as argument but received a nil pointer: %v", record)
}
info := kstructs.GetTagInfo(t.Elem())
query, params, scanValues, err := buildInsertQuery(c.dialect, table.name, t, v, info, record, table.idColumns...)

View File

@ -791,6 +791,18 @@ func TestInsert(t *testing.T) {
assert.NotEqual(t, nil, err)
})
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.Insert(ctx, UsersTable, user)
assert.NotEqual(t, nil, err)
})
t.Run("should not panic if a column doesn't exist in the database", func(t *testing.T) {
db, closer := connectDB(t, config)
defer closer.Close()