Fix use uint to for autoCreateTime, autoUpdateTime

This commit is contained in:
Jinzhu 2020-09-11 17:33:31 +08:00
parent 02fb382ec0
commit ed1b134e1c
2 changed files with 19 additions and 11 deletions

View File

@ -624,6 +624,14 @@ func (field *Field) setupValuerAndSetter() {
field.ReflectValueOf(value).SetUint(uint64(data))
case []byte:
return field.Set(value, string(data))
case time.Time:
if field.AutoCreateTime == UnixNanosecond || field.AutoUpdateTime == UnixNanosecond {
field.ReflectValueOf(value).SetUint(uint64(data.UnixNano()))
} else if field.AutoCreateTime == UnixMillisecond || field.AutoUpdateTime == UnixMillisecond {
field.ReflectValueOf(value).SetUint(uint64(data.UnixNano() / 1e6))
} else {
field.ReflectValueOf(value).SetUint(uint64(data.Unix()))
}
case string:
if i, err := strconv.ParseUint(data, 0, 64); err == nil {
field.ReflectValueOf(value).SetUint(i)

View File

@ -69,12 +69,12 @@ func TestCustomizeField(t *testing.T) {
FieldAllowSave3 string `gorm:"->:false;<-:create"`
FieldReadonly string `gorm:"->"`
FieldIgnore string `gorm:"-"`
AutoUnixCreateTime int64 `gorm:"autocreatetime"`
AutoUnixMilliCreateTime int64 `gorm:"autocreatetime:milli"`
AutoUnixCreateTime int32 `gorm:"autocreatetime"`
AutoUnixMilliCreateTime int `gorm:"autocreatetime:milli"`
AutoUnixNanoCreateTime int64 `gorm:"autocreatetime:nano"`
AutoUnixUpdateTime int64 `gorm:"autoupdatetime"`
AutoUnixMilliUpdateTime int64 `gorm:"autoupdatetime:milli"`
AutoUnixNanoUpdateTime int64 `gorm:"autoupdatetime:nano"`
AutoUnixUpdateTime uint32 `gorm:"autoupdatetime"`
AutoUnixMilliUpdateTime int `gorm:"autoupdatetime:milli"`
AutoUnixNanoUpdateTime uint64 `gorm:"autoupdatetime:nano"`
}
DB.Migrator().DropTable(&CustomizeFieldStruct{})
@ -116,15 +116,15 @@ func TestCustomizeField(t *testing.T) {
t.Fatalf("invalid result: %#v", result)
}
if result.AutoUnixCreateTime != result.AutoUnixUpdateTime || result.AutoUnixCreateTime == 0 {
if int(result.AutoUnixCreateTime) != int(result.AutoUnixUpdateTime) || result.AutoUnixCreateTime == 0 {
t.Fatalf("invalid create/update unix time: %#v", result)
}
if result.AutoUnixMilliCreateTime != result.AutoUnixMilliUpdateTime || result.AutoUnixMilliCreateTime == 0 || result.AutoUnixMilliCreateTime/result.AutoUnixCreateTime < 1e3 {
if int(result.AutoUnixMilliCreateTime) != int(result.AutoUnixMilliUpdateTime) || result.AutoUnixMilliCreateTime == 0 || int(result.AutoUnixMilliCreateTime)/int(result.AutoUnixCreateTime) < 1e3 {
t.Fatalf("invalid create/update unix milli time: %#v", result)
}
if result.AutoUnixNanoCreateTime != result.AutoUnixNanoUpdateTime || result.AutoUnixNanoCreateTime == 0 || result.AutoUnixNanoCreateTime/result.AutoUnixCreateTime < 1e6 {
if int(result.AutoUnixNanoCreateTime) != int(result.AutoUnixNanoUpdateTime) || result.AutoUnixNanoCreateTime == 0 || int(result.AutoUnixNanoCreateTime)/int(result.AutoUnixCreateTime) < 1e6 {
t.Fatalf("invalid create/update unix nano time: %#v", result)
}
@ -178,15 +178,15 @@ func TestCustomizeField(t *testing.T) {
var createWithDefaultTimeResult CustomizeFieldStruct
DB.Find(&createWithDefaultTimeResult, "name = ?", createWithDefaultTime.Name)
if createWithDefaultTimeResult.AutoUnixCreateTime != createWithDefaultTimeResult.AutoUnixUpdateTime || createWithDefaultTimeResult.AutoUnixCreateTime != 100 {
if int(createWithDefaultTimeResult.AutoUnixCreateTime) != int(createWithDefaultTimeResult.AutoUnixUpdateTime) || createWithDefaultTimeResult.AutoUnixCreateTime != 100 {
t.Fatalf("invalid create/update unix time: %#v", createWithDefaultTimeResult)
}
if createWithDefaultTimeResult.AutoUnixMilliCreateTime != createWithDefaultTimeResult.AutoUnixMilliUpdateTime || createWithDefaultTimeResult.AutoUnixMilliCreateTime != 100 {
if int(createWithDefaultTimeResult.AutoUnixMilliCreateTime) != int(createWithDefaultTimeResult.AutoUnixMilliUpdateTime) || createWithDefaultTimeResult.AutoUnixMilliCreateTime != 100 {
t.Fatalf("invalid create/update unix milli time: %#v", createWithDefaultTimeResult)
}
if createWithDefaultTimeResult.AutoUnixNanoCreateTime != createWithDefaultTimeResult.AutoUnixNanoUpdateTime || createWithDefaultTimeResult.AutoUnixNanoCreateTime != 100 {
if int(createWithDefaultTimeResult.AutoUnixNanoCreateTime) != int(createWithDefaultTimeResult.AutoUnixNanoUpdateTime) || createWithDefaultTimeResult.AutoUnixNanoCreateTime != 100 {
t.Fatalf("invalid create/update unix nano time: %#v", createWithDefaultTimeResult)
}
}