mirror of https://github.com/VinGarcia/ksql.git
Add error check for nil pointers passed as arguments to Insert()
parent
e7e404dc86
commit
1cf671cd33
4
ksql.go
4
ksql.go
|
@ -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...)
|
||||
|
|
12
ksql_test.go
12
ksql_test.go
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue