Skip to content

Commit

Permalink
fix: improve bun.NullTime to accept string
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 15, 2021
1 parent 4a95333 commit 73ad6f5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions internal/dbtest/pg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"reflect"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -315,4 +316,19 @@ func TestPGDate(t *testing.T) {
err := db.NewSelect().ColumnExpr("'2021-09-15'::DATE").Scan(ctx, &str)
require.NoError(t, err)
require.Equal(t, "2021-09-15", str)

str = ""
err = db.NewSelect().ColumnExpr("CURRENT_TIMESTAMP::date").Scan(ctx, &str)
require.NoError(t, err)
require.NotZero(t, str)

var tm time.Time
err = db.NewSelect().ColumnExpr("CURRENT_TIMESTAMP::date").Scan(ctx, &tm)
require.NoError(t, err)
require.NotZero(t, tm)

var nullTime bun.NullTime
err = db.NewSelect().ColumnExpr("CURRENT_TIMESTAMP::date").Scan(ctx, &nullTime)
require.NoError(t, err)
require.False(t, nullTime.IsZero())
}
14 changes: 10 additions & 4 deletions schema/sqltype.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,23 @@ func (tm *NullTime) Scan(src interface{}) error {
}

switch src := src.(type) {
case time.Time:
tm.Time = src
return nil
case string:
newtm, err := internal.ParseTime(src)
if err != nil {
return err
}
tm.Time = newtm
return nil
case []byte:
newtm, err := internal.ParseTime(internal.String(src))
if err != nil {
return err
}

tm.Time = newtm
return nil
case time.Time:
tm.Time = src
return nil
default:
return fmt.Errorf("bun: can't scan %#v into NullTime", src)
}
Expand Down

0 comments on commit 73ad6f5

Please sign in to comment.