gobuffalo / buffalo

Rapid Web Development w/ Go
http://gobuffalo.io
MIT License
8.08k stars 579 forks source link

For nulls.Time, decode empty value as NULL #2359

Closed travisturner closed 1 year ago

travisturner commented 1 year ago

What is being done in this PR?

This PR adds a check during decoding which will return nulls.Time as a NULL value (i.e. its value for Valid will be false). Without this check, the value of nulls.Time{} will be:

{
  Time: 0001-01-01 00:00:00 +0000 UTC,
  Valid: true,
}

This ends up storing the value 0001-01-01 00:00:00 +0000 UTC in the database, as opposed to a NULL value.

What are the main choices made to get to this solution?

One option was to make the model Bindable and write a custom Bind() method which could handle empty values, but the default Bind handler does so much that bypassing it seemed extreme.

What was discovered while working on it? (Optional)

Type in here a description of the discoveries made while working on this PR.

List the manual test cases you've covered before sending this PR:

Fizz (using Postgres):

create_table("people") {
    t.Column("id", "uuid", {primary: true})
    t.Column("name", "string", {"size": 128})
    t.Column("birthday", "date", {"null": true})
    t.Timestamps()
}

Model:

type Person struct {
    ID uuid.UUID `json:"id" db:"id"`
    Name  string `json:"name" db:"name"`
    Birthday nulls.Time `json:"birthday" db:"birthday"`
    CreatedAt time.Time `json:"created_at" db:"created_at"`
    UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}

Plush form:

<%= f.InputTag("Birthday", {"type":"date"}) %>