扩展time.Time类型:JsonTime设置为指针类型查询报错 #1144
Labels
No Label
backport/done
backport/v1
blocked
db
oracle
db
sqlserver
duplicate
feature
cache
frontport/done
frontport/main
invalid
kind
breaking
kind
bug
kind
build
kind
dependencies
kind
docs
kind
driver
kind
enhancement
kind
feature
kind
performance
kind
proposal
kind
question
kind
refactor
kind
testing
need
feedback
need
test
proposal:accepted
RaspBerry Pi
regression
skip-changelog
upstream
wip
wontfix
No Milestone
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: xorm/xorm#1144
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
基于time.Time扩展了自定义类型JsonTime,同时数据库字段可能为nil值,所以使用指针方式
使用 *time.Time 正常
使用 JsonTime 正常
使用 *JsonTime 出错
这个报错的根本原因是在说使用了unexported的字段导致反射获取失败,可否将上述代码贴出来方便我们进行排查呢
感谢回复,以下是测试代码
`
package main
import (
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
)
// ==========================================================================================
type Time time.Time
const timeFormate = "2006-01-02 15:04:05"
func (t *Time) UnmarshalJSON(data []byte) (err error) {
now, err := time.ParseInLocation(
"
+timeFormate+"
, string(data), time.Local)*t = Time(now)
return
}
func (t Time) MarshalJSON() ([]byte, error) {
data := make([]byte, 0)
data = append(data, '"')
data = time.Time(t).AppendFormat(data, timeFormate)
data = append(data, '"')
return data, nil
}
func (t Time) String() string {
return time.Time(t).Format(timeFormate)
}
// ==========================================================================================
type TestUser struct {
Id int64
json:"id" xorm:"pk autoincr"
Username string
json:"username" xorm:"notnull"
Birthday *Time
json:"birthday" xorm:"null"
CreateTime Time
json:"createTime" xorm:"created notnull"
ModifyTime Time
json:"modifyTime" xorm:"updated"
}
func main() {
engine, err := xorm.NewEngine("mysql", "root:123456@tcp(192.168.5.101:3306)/husky?charset=utf8")
if err != nil {
panic(err)
} else if err := engine.Ping(); err != nil {
panic(err)
}
engine.TZLocation, _ = time.LoadLocation("Asia/Shanghai")
engine.ShowSQL(true)
engine.ShowExecTime(true)
}
`