扩展time.Time类型:JsonTime设置为指针类型查询报错 #1144

Open
opened 2018-11-13 07:20:28 +00:00 by zhangzxiang · 2 comments
zhangzxiang commented 2018-11-13 07:20:28 +00:00 (Migrated from github.com)

基于time.Time扩展了自定义类型JsonTime,同时数据库字段可能为nil值,所以使用指针方式
使用 *time.Time 正常
使用 JsonTime 正常
使用 *JsonTime 出错

image

image

基于time.Time扩展了自定义类型JsonTime,同时数据库字段可能为nil值,所以使用指针方式 使用 *time.Time 正常 使用 JsonTime 正常 使用 *JsonTime 出错 ![image](https://user-images.githubusercontent.com/8652298/48397008-563fe880-e757-11e8-8fd1-d0c7ff68d976.png) ![image](https://user-images.githubusercontent.com/8652298/48396676-3b20a900-e756-11e8-97c7-295d30298c4c.png)
Member

image
这个报错的根本原因是在说使用了unexported的字段导致反射获取失败,可否将上述代码贴出来方便我们进行排查呢

![image](https://user-images.githubusercontent.com/11432752/48753251-4761b480-ecc8-11e8-8806-cd2423e0cd8b.png) 这个报错的根本原因是在说使用了unexported的字段导致反射获取失败,可否将上述代码贴出来方便我们进行排查呢
zhangzxiang commented 2018-11-20 08:32:50 +00:00 (Migrated from github.com)

image
这个报错的根本原因是在说使用了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)

engine.Sync2(&TestUser{})

// 报错
birthday := Time(time.Now())
engine.InsertOne(&TestUser{Username: "zhangsan", Birthday: &birthday})
// 正常
// engine.InsertOne(&TestUser{Username: "zhangsan", Birthday: nil})

}
`

> ![image](https://user-images.githubusercontent.com/11432752/48753251-4761b480-ecc8-11e8-8806-cd2423e0cd8b.png) > 这个报错的根本原因是在说使用了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) engine.Sync2(&TestUser{}) // 报错 birthday := Time(time.Now()) engine.InsertOne(&TestUser{Username: "zhangsan", Birthday: &birthday}) // 正常 // engine.InsertOne(&TestUser{Username: "zhangsan", Birthday: nil}) } `
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1144
No description provided.