Skip to content

Commit

Permalink
Merge pull request #78 from ulule/fix/do-not-panic-when-formatting-a-…
Browse files Browse the repository at this point in the history
…nil-valuer

fix: don't panic when formatting a nil valuer
  • Loading branch information
thoas authored May 23, 2019
2 parents 71cec05 + 8933d8e commit be8dac3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
21 changes: 21 additions & 0 deletions builder/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package builder_test

import (
"database/sql"
"database/sql/driver"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -495,9 +496,29 @@ func TestInsert_Valuer(t *testing.T) {
NamedQuery: "INSERT INTO table (email, login) VALUES (:arg_1, :arg_2)",
Args: []interface{}{"[email protected]", sql.NullInt64{}},
},
{
Name: "nil valuer",
Builder: loukoum.
Insert("table").
Columns("email", "login").
Values("[email protected]", (*valuer)(nil)),
String: "INSERT INTO table (email, login) VALUES ('[email protected]', NULL)",
Query: "INSERT INTO table (email, login) VALUES ($1, $2)",
NamedQuery: "INSERT INTO table (email, login) VALUES (:arg_1, :arg_2)",
Args: []interface{}{"[email protected]", (*valuer)(nil)},
},
})
}

type valuer struct{}

func (valuer) Value() (driver.Value, error) {
return nil, nil
}
func (*valuer) Scan(src interface{}) error {
return nil
}

func TestInsert_Set(t *testing.T) {
RunBuilderTests(t, []BuilderTest{
{
Expand Down
7 changes: 7 additions & 0 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
"encoding/hex"
"fmt"
"reflect"
"strconv"
"time"
)
Expand All @@ -23,6 +24,12 @@ func Value(arg interface{}) string { // nolint: gocyclo
case time.Time:
return Time(value)
case driver.Valuer:
reflectvalue := reflect.ValueOf(value)
if reflectvalue.Kind() == reflect.Ptr &&
reflectvalue.IsNil() {
return "NULL"
}

v, err := value.Value()
if err != nil {
panic("loukoum: was not able to retrieve valuer value")
Expand Down

0 comments on commit be8dac3

Please sign in to comment.