-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(time): added nullable Time with better json support (#44)
- Loading branch information
Showing
5 changed files
with
196 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package sqle | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"encoding/json" | ||
"time" | ||
) | ||
|
||
var nullTimeJsonBytes = []byte("null") | ||
|
||
const nullTimeJson = "null" | ||
|
||
// Time represents a nullable time value. | ||
type Time struct { | ||
sql.NullTime | ||
} | ||
|
||
// NewTime creates a new Time object with the given time and valid flag. | ||
func NewTime(t time.Time, valid bool) Time { | ||
return Time{NullTime: sql.NullTime{Time: t, Valid: valid}} | ||
} | ||
|
||
// Scan implements the [sql.Scanner] interface. | ||
func (t *Time) Scan(value any) error { // skipcq: GO-W1029 | ||
return t.NullTime.Scan(value) | ||
} | ||
|
||
// Value implements the [driver.Valuer] interface. | ||
func (t Time) Value() (driver.Value, error) { // skipcq: GO-W1029 | ||
return t.NullTime.Value() | ||
} | ||
|
||
// Time returns the underlying time.Time value of the Time struct. | ||
func (t *Time) Time() time.Time { // skipcq: GO-W1029 | ||
return t.NullTime.Time | ||
} | ||
|
||
// MarshalJSON implements the json.Marshaler interface | ||
func (t Time) MarshalJSON() ([]byte, error) { // skipcq: GO-W1029 | ||
if t.Valid { | ||
return json.Marshal(t.NullTime.Time) | ||
} | ||
return nullTimeJsonBytes, nil | ||
} | ||
|
||
// UnmarshalJSON implements the json.Unmarshaler interface | ||
func (t *Time) UnmarshalJSON(data []byte) error { // skipcq: GO-W1029 | ||
if len(data) == 0 || string(data) == nullTimeJson { | ||
t.NullTime.Time = time.Time{} | ||
t.NullTime.Valid = false | ||
return nil | ||
} | ||
|
||
var v time.Time | ||
err := json.Unmarshal(data, &v) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t.NullTime.Time = v | ||
t.NullTime.Valid = true | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package sqle | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTimeInSQL(t *testing.T) { | ||
|
||
now := time.Now() | ||
d, err := sql.Open("sqlite3", "file::memory:") | ||
require.NoError(t, err) | ||
|
||
_, err = d.Exec("CREATE TABLE `times` (`id` id NOT NULL,`created_at` datetime, PRIMARY KEY (`id`))") | ||
require.NoError(t, err) | ||
|
||
result, err := d.Exec("INSERT INTO `times`(`id`) VALUES(?)", 10) | ||
require.NoError(t, err) | ||
|
||
rows, err := result.RowsAffected() | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), rows) | ||
|
||
result, err = d.Exec("INSERT INTO `times`(`id`, `created_at`) VALUES(?, ?)", 20, now) | ||
require.NoError(t, err) | ||
|
||
rows, err = result.RowsAffected() | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), rows) | ||
|
||
var t10 Time | ||
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 10).Scan(&t10) | ||
require.NoError(t, err) | ||
|
||
require.EqualValues(t, false, t10.Valid) | ||
|
||
var t20 Time | ||
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 20).Scan(&t20) | ||
require.NoError(t, err) | ||
|
||
require.EqualValues(t, true, t20.Valid) | ||
require.EqualValues(t, now.UTC(), t20.Time().UTC()) | ||
|
||
result, err = d.Exec("INSERT INTO `times`(`id`,`created_at`) VALUES(?, ?)", 11, t10) | ||
require.NoError(t, err) | ||
|
||
rows, err = result.RowsAffected() | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), rows) | ||
|
||
result, err = d.Exec("INSERT INTO `times`(`id`, `created_at`) VALUES(?, ?)", 21, t20) | ||
require.NoError(t, err) | ||
|
||
rows, err = result.RowsAffected() | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), rows) | ||
|
||
var t11 Time | ||
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 11).Scan(&t11) | ||
require.NoError(t, err) | ||
|
||
require.EqualValues(t, false, t11.Valid) | ||
|
||
var t21 Time | ||
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 21).Scan(&t21) | ||
require.NoError(t, err) | ||
|
||
require.EqualValues(t, true, t21.Valid) | ||
require.EqualValues(t, now.UTC(), t21.Time().UTC()) | ||
|
||
} | ||
|
||
func TestTimeInJSON(t *testing.T) { | ||
|
||
sysTime := time.Now() | ||
|
||
bufSysTime, err := json.Marshal(sysTime) | ||
require.NoError(t, err) | ||
|
||
sqleTime := NewTime(sysTime, true) | ||
|
||
bufSqleTime, err := json.Marshal(sqleTime) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, bufSysTime, bufSqleTime) | ||
|
||
var jsSqleTime Time | ||
// Unmarshal sqle.Time from time.Time json bytes | ||
err = json.Unmarshal(bufSysTime, &jsSqleTime) | ||
require.NoError(t, err) | ||
|
||
require.True(t, sysTime.Equal(jsSqleTime.Time())) | ||
require.Equal(t, true, jsSqleTime.Valid) | ||
|
||
var jsSysTime time.Time | ||
// Unmarshal time.Time from sqle.Time json bytes | ||
err = json.Unmarshal(bufSqleTime, &jsSysTime) | ||
require.NoError(t, err) | ||
require.True(t, sysTime.Equal(jsSysTime)) | ||
|
||
var nullTime Time | ||
err = json.Unmarshal([]byte("null"), &nullTime) | ||
require.NoError(t, err) | ||
require.Equal(t, false, nullTime.Valid) | ||
|
||
bufNull, err := json.Marshal(nullTime) | ||
require.NoError(t, err) | ||
require.Equal(t, []byte("null"), bufNull) | ||
} |