Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: time cannot be bind to structure in Cursor #246

Merged
merged 3 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 117 additions & 1 deletion database/gorm/cursor.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package gorm

import "github.com/mitchellh/mapstructure"
import (
"reflect"
"strings"
"time"

"github.com/mitchellh/mapstructure"
"gorm.io/gorm"

"github.com/goravel/framework/support/carbon"
"github.com/goravel/framework/support/str"
)

type CursorImpl struct {
row map[string]any
}

func (c *CursorImpl) Scan(value any) error {
msConfig := &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
ToTimeHookFunc(), ToCarbonHookFunc(), ToDeletedAtHookFunc(),
),
Squash: true,
Result: value,
MatchName: func(mapKey, fieldName string) bool {
return str.Case2Camel(mapKey) == fieldName || strings.EqualFold(mapKey, fieldName)
},
}

decoder, err := mapstructure.NewDecoder(msConfig)
Expand All @@ -19,3 +35,103 @@ func (c *CursorImpl) Scan(value any) error {

return decoder.Decode(c.row)
}

func ToTimeHookFunc() mapstructure.DecodeHookFunc {
return func(f reflect.Type, t reflect.Type, data any) (any, error) {
if t != reflect.TypeOf(time.Time{}) {
return data, nil
}

switch f.Kind() {
case reflect.String:
return time.Parse(time.RFC3339, data.(string))
case reflect.Float64:
return time.Unix(0, int64(data.(float64))*int64(time.Millisecond)), nil
case reflect.Int64:
return time.Unix(0, data.(int64)*int64(time.Millisecond)), nil
default:
return data, nil
}
}
}

func ToCarbonHookFunc() mapstructure.DecodeHookFunc {
return func(f reflect.Type, t reflect.Type, data any) (any, error) {
if f == reflect.TypeOf(time.Time{}) {
switch t {
case reflect.TypeOf(carbon.DateTime{}):
return carbon.NewDateTime(carbon.FromStdTime(data.(time.Time))), nil
case reflect.TypeOf(carbon.DateTimeMilli{}):
return carbon.NewDateTimeMilli(carbon.FromStdTime(data.(time.Time))), nil
case reflect.TypeOf(carbon.DateTimeMicro{}):
return carbon.NewDateTimeMicro(carbon.FromStdTime(data.(time.Time))), nil
case reflect.TypeOf(carbon.DateTimeNano{}):
return carbon.NewDateTimeNano(carbon.FromStdTime(data.(time.Time))), nil
case reflect.TypeOf(carbon.Date{}):
return carbon.NewDate(carbon.FromStdTime(data.(time.Time))), nil
case reflect.TypeOf(carbon.DateMilli{}):
return carbon.NewDateMilli(carbon.FromStdTime(data.(time.Time))), nil
case reflect.TypeOf(carbon.DateMicro{}):
return carbon.NewDateMicro(carbon.FromStdTime(data.(time.Time))), nil
case reflect.TypeOf(carbon.DateNano{}):
return carbon.NewDateNano(carbon.FromStdTime(data.(time.Time))), nil
case reflect.TypeOf(carbon.Timestamp{}):
return carbon.NewTimestamp(carbon.FromStdTime(data.(time.Time))), nil
case reflect.TypeOf(carbon.TimestampMilli{}):
return carbon.NewTimestampMilli(carbon.FromStdTime(data.(time.Time))), nil
case reflect.TypeOf(carbon.TimestampMicro{}):
return carbon.NewTimestampMicro(carbon.FromStdTime(data.(time.Time))), nil
case reflect.TypeOf(carbon.TimestampNano{}):
return carbon.NewTimestampNano(carbon.FromStdTime(data.(time.Time))), nil
}
}
if f.Kind() == reflect.String {
switch t {
case reflect.TypeOf(carbon.DateTime{}):
return carbon.NewDateTime(carbon.Parse(data.(string))), nil
case reflect.TypeOf(carbon.DateTimeMilli{}):
return carbon.NewDateTimeMilli(carbon.Parse(data.(string))), nil
case reflect.TypeOf(carbon.DateTimeMicro{}):
return carbon.NewDateTimeMicro(carbon.Parse(data.(string))), nil
case reflect.TypeOf(carbon.DateTimeNano{}):
return carbon.NewDateTimeNano(carbon.Parse(data.(string))), nil
case reflect.TypeOf(carbon.Date{}):
return carbon.NewDate(carbon.Parse(data.(string))), nil
case reflect.TypeOf(carbon.DateMilli{}):
return carbon.NewDateMilli(carbon.Parse(data.(string))), nil
case reflect.TypeOf(carbon.DateMicro{}):
return carbon.NewDateMicro(carbon.Parse(data.(string))), nil
case reflect.TypeOf(carbon.DateNano{}):
return carbon.NewDateNano(carbon.Parse(data.(string))), nil
case reflect.TypeOf(carbon.Timestamp{}):
return carbon.NewTimestamp(carbon.Parse(data.(string))), nil
case reflect.TypeOf(carbon.TimestampMilli{}):
return carbon.NewTimestampMilli(carbon.Parse(data.(string))), nil
case reflect.TypeOf(carbon.TimestampMicro{}):
return carbon.NewTimestampMicro(carbon.Parse(data.(string))), nil
case reflect.TypeOf(carbon.TimestampNano{}):
return carbon.NewTimestampNano(carbon.Parse(data.(string))), nil
}
}

return data, nil
}
}

func ToDeletedAtHookFunc() mapstructure.DecodeHookFunc {
return func(f reflect.Type, t reflect.Type, data any) (any, error) {
if t != reflect.TypeOf(gorm.DeletedAt{}) {
return data, nil
}

if f == reflect.TypeOf(time.Time{}) {
return gorm.DeletedAt{Time: data.(time.Time), Valid: true}, nil
}

if f.Kind() == reflect.String {
return gorm.DeletedAt{Time: carbon.Parse(data.(string)).ToStdTime(), Valid: true}, nil
}

return data, nil
}
}
62 changes: 36 additions & 26 deletions database/gorm/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,42 @@ func (s *QueryTestSuite) TestCreate() {
}
}

func (s *QueryTestSuite) TestCursor() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
user := User{Name: "cursor_user", Avatar: "cursor_avatar"}
s.Nil(query.Create(&user))
s.True(user.ID > 0)

user1 := User{Name: "cursor_user", Avatar: "cursor_avatar1"}
s.Nil(query.Create(&user1))
s.True(user1.ID > 0)

user2 := User{Name: "cursor_user", Avatar: "cursor_avatar2"}
s.Nil(query.Create(&user2))
s.True(user2.ID > 0)
res, err := query.Delete(&user2)
s.Nil(err)
s.Equal(int64(1), res.RowsAffected)

users, err := query.Model(&User{}).Where("name = ?", "cursor_user").WithTrashed().Cursor()
s.Nil(err)
var size int
for row := range users {
var tempUser User
s.Nil(row.Scan(&tempUser))
s.True(tempUser.ID > 0)
s.True(len(tempUser.Name) > 0)
s.NotEmpty(tempUser.CreatedAt.String())
s.NotEmpty(tempUser.UpdatedAt.String())
s.Equal(tempUser.DeletedAt.Valid, tempUser.ID == user2.ID)
size++
}
s.Equal(3, size)
})
}
}

func (s *QueryTestSuite) TestDelete() {
for _, query := range s.queries {
tests := []struct {
Expand Down Expand Up @@ -2458,32 +2494,6 @@ func (s *QueryTestSuite) TestSum() {
}
}

func (s *QueryTestSuite) TestCursor() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
user := User{Name: "cursor_user", Avatar: "cursor_avatar"}
s.Nil(query.Create(&user))
s.True(user.ID > 0)

user1 := User{Name: "cursor_user", Avatar: "cursor_avatar1"}
s.Nil(query.Create(&user1))
s.True(user1.ID > 0)

users, err := query.Model(&User{}).Where("name = ?", "cursor_user").Cursor()
s.Nil(err)
var size int
for row := range users {
var user User
s.Nil(row.Scan(&user))
s.True(user.ID > 0)
s.True(len(user.Name) > 0)
size++
}
s.Equal(2, size)
})
}
}

func (s *QueryTestSuite) TestTransactionSuccess() {
for driver, query := range s.queries {
s.Run(driver.String(), func() {
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
Expand Down