Skip to content

Panic on nil-value that implements driver.Valuer interface #255

@Diggs

Description

@Diggs

Describe the bug

If driver.Valuer is implemented as a value receiver on a struct, and that struct is referenced via a pointer, Goqu panics when converting to SQL.

To Reproduce

type Person struct {
   address *Address
}

type Address struct {
   // ...
}

func (m Address ) Value() (driver.Value, error) { // <- Address implements driver.Valuer with value receiver
	buf, err := json.Marshal(m)
	if err != nil {
		return nil, fmt.Errorf("error marshaling to JSON: %w", err)
	}
	return string(buf), nil
}

func main() {
    p := &Person{} // <- p.Address is nil
    builder := goqu.Insert("person").Rows(p)
    query, args, _ := builder.ToSQL() // <- Panics: panic: value method github.com.../models.Address.Value called using nil *Address pointer
}

Expected behavior

The Address property on Person should be converted to NULL.

Dialect:

  • [ *] postgres
  • [ *] mysql
  • [ *] sqlite3

Additional context
This same type of issue affected the Go std lib back in the day and was fixed: golang/go#8415

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions