濮阳杆衣贸易有限公司

主頁 > 知識庫 > golang xorm及time.Time自定義解決json日期格式的問題

golang xorm及time.Time自定義解決json日期格式的問題

熱門標簽:地圖標注免費定制店 宿遷便宜外呼系統(tǒng)代理商 不封卡外呼系統(tǒng) 仙桃400電話辦理 上海極信防封電銷卡價格 鄭州智能語音電銷機器人價格 湛江crm外呼系統(tǒng)排名 重慶慶云企業(yè)400電話到哪申請 寧波語音外呼系統(tǒng)公司

golang默認的time.Time類型在轉(zhuǎn)為json格式時不是常用的2019-05-08 10:00:01這種格式,解決辦法是自定義一個時間類型,例如

type myTime time.Time ,然后針對myTime實現(xiàn)Marshaler接口的MarshalJSON方法,例如:

package models 
import (
 "database/sql/driver"
 "time"
)
 
const localDateTimeFormat string = "2006-01-02 15:04:05" 
type LocalTime time.Time 
func (l LocalTime) MarshalJSON() ([]byte, error) {
 b := make([]byte, 0, len(localDateTimeFormat)+2)
 b = append(b, '"')
 b = time.Time(l).AppendFormat(b, localDateTimeFormat)
 b = append(b, '"')
 return b, nil
}
 
func (l *LocalTime) UnmarshalJSON(b []byte) error {
 now, err := time.ParseInLocation(`"`+localDateTimeFormat+`"`, string(b), time.Local)
 *l = LocalTime(now)
 return err
}

上面的代碼在網(wǎng)上隨手一搜就能找到,沒有什么困難的,接下來的才是本篇文章的重點,這玩意結(jié)合xorm使用時,特別是字段類型為*LocalTime的時候才需要折騰一番。

下面是我的對應(yīng)數(shù)據(jù)庫表結(jié)構(gòu)的struct 定義,

type ServerInfo struct {
 ServerInfoId       string   `xorm:"varchar(32) pk server_info_id"`
 CreatedAt        LocalTime `xorm:"timestamp created"`
 UpdatedAt        LocalTime `xorm:"timestamp updated"`
 DeletedAt        *LocalTime `xorm:"timestamp deleted index"`
 OrgId          string   `xorm:"varchar(100) org_id" json:"orgId"`                        
 ServerIp         string   `xorm:"varchar(128) server_ip" json:"serverIp"`                     
 ServerNameDesc      string   `xorm:"varchar(500) server_name_desc" json:"serverNameDesc"`               
 ServerTimeNow      LocalTime `xorm:"timestamp server_time" json:"serverTime"`                     
 DataReceiveTime     LocalTime `xorm:"timestamp data_receive_time" sql:"DEFAULT:current_timestamp" json:"dataRecvTime"` 
 LastUploadDataTime    *LocalTime `xorm:"timestamp last_upload_data_time" json:"lastUploadDataTime"`            
 LastCheckTime      *LocalTime `xorm:"timestamp last_check_time" json:"lastCheckTime"`                 
 LastErrorTime      *LocalTime `xorm:"timestamp last_error_time" json:"lastErrorTime"`                 
}

注意上面的字段類型,既有LocalTime類型的,又有*LocalTime類型的,*LocalTime是考慮到有時候數(shù)據(jù)值可能為NULL,即字段值可能為空的情況。

xorm不知道如何為LocalTime這個自定義類型進行賦值或者取值,因此需要實現(xiàn)xorm的core包中的Conversion接口,這個接口的定義如下:

注意,坑已經(jīng)隱藏在上面的接口定義中了,過一會說。

整個完整的自定義時間類型的代碼變成了下面的這樣:

package models 
import (
 "database/sql/driver"
 "time"
)
 
const localDateTimeFormat string = "2006-01-02 15:04:05" 
type LocalTime time.Time 
func (l LocalTime) MarshalJSON() ([]byte, error) {
 b := make([]byte, 0, len(localDateTimeFormat)+2)
 b = append(b, '"')
 b = time.Time(l).AppendFormat(b, localDateTimeFormat)
 b = append(b, '"')
 return b, nil
}
 
func (l *LocalTime) UnmarshalJSON(b []byte) error {
 now, err := time.ParseInLocation(`"`+localDateTimeFormat+`"`, string(b), time.Local)
 *l = LocalTime(now)
 return err
}
 
func (l LocalTime) String() string {
 return time.Time(l).Format(localDateTimeFormat)
}
 
func (l LocalTime)Now()(LocalTime){
 return LocalTime(time.Now())
}
 
func (l LocalTime)ParseTime(t time.Time)(LocalTime){
 return LocalTime(t)
}
 
func (j LocalTime) format() string {
 return time.Time(j).Format(localDateTimeFormat)
}
 
func (j LocalTime) MarshalText() ([]byte, error) {
 return []byte(j.format()), nil
}
 
func (l *LocalTime) FromDB(b []byte) error {
 if nil == b || len(b) == 0 {
 l = nil
 return nil
 }
 var now time.Time
 var err error
 now, err = time.ParseInLocation(localDateTimeFormat, string(b), time.Local)
 if nil == err {
 *l = LocalTime(now)
 return nil
 }
 now, err = time.ParseInLocation("2006-01-02T15:04:05Z", string(b), time.Local)
 if nil == err {
 *l = LocalTime(now)
 return nil
 }
 panic("自己定義個layout日期格式處理一下數(shù)據(jù)庫里面的日期型數(shù)據(jù)解析!")
 return err
}
 
//func (t *LocalTime) Scan(v interface{}) error {
// // Should be more strictly to check this type.
// vt, err := time.Parse("2006-01-02 15:04:05", string(v.([]byte)))
// if err != nil {
// return err
// }
// *t = LocalTime(vt)
// return nil
//}
 
func (l *LocalTime) ToDB() ([]byte, error) {
 if nil == l {
 return nil,nil
 }
 return []byte(time.Time(*l).Format(localDateTimeFormat)), nil
}
 
func (l *LocalTime) Value() (driver.Value, error) {
 if nil==l {
 return nil, nil
 }
 return time.Time(*l).Format(localDateTimeFormat), nil
}

此時,要是數(shù)據(jù)庫的字段內(nèi)容都有值的話插入和更新應(yīng)該是沒有什么問題,但是*LocalTime字段的值為nil的話問題就開始出現(xiàn)了,上面說了,ToDB()方法的返回值類型為[]byte,當(dāng)字段值為nil時,返回nil看上去一切正常,但是xorm打印出來的sql語句數(shù)據(jù)值是下面這個樣子的:

這個[]uint8(nil)就是*LocalTime值為nil時的情況,數(shù)據(jù)庫驅(qū)動是不認可[]uint8(nil)這種數(shù)據(jù)去寫給timestamp類型字段的,問題的根源就是ToDB方法的返回值類型為[]byte,既然是這樣,就需要我們?nèi)藶榈陌裑]uint8(nil)這種類型改為interface(nil)類型,數(shù)據(jù)庫驅(qū)動會識別interface(nil)為NULL值,修改代碼xorm\statement.go第322行,把原來的val=data改成下面的樣子:

就是把val=data改為 if nil==data { val=nil } else {val=data} ,看上去邏輯沒有什么變化,但是給val=nil賦值的時候,val的類型就從[]uint8(nil)變成了interface(nil)了,這樣數(shù)據(jù)庫驅(qū)動就可以正確處理空值了。

除了需要修改xorm\statement.go文件的內(nèi)容,還需要修改xorm\session_convert.go的第558行,增加以下代碼:

主要是增加下面的代碼

//fix when pointer type value is null,added by peihexian,2019-05-07
if nil==data {
  return nil,nil
}

之所以加這個代碼是因為xorm作者沒有考慮指針類型字段值為nil的情況,xorm對有轉(zhuǎn)換的字段要么當(dāng)成數(shù)字,要么當(dāng)成了字符串,這兩種對于NULL類型的值都不適用,所以需要增加if nil==data return nil,nil這樣的代碼,還是把數(shù)據(jù)值組織成interface(nil)去給數(shù)據(jù)庫驅(qū)動去處理。

另外還有一個地方,是session_convert.go 第556行,同樣需要增加

if nil==data { //edit by peihexian 2019.06.19
  return nil,nil
}

下面是加完以后的樣子

到這里,對xorm做了幾處小的修改,自定義日期的問題及json格式化問題完美解決。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • golang 定時任務(wù)方面time.Sleep和time.Tick的優(yōu)劣對比分析
  • 解決Golang time.Parse和time.Format的時區(qū)問題
  • 解決golang時間字符串轉(zhuǎn)time.Time的坑
  • golang的時區(qū)和神奇的time.Parse的使用方法
  • 對Golang中的runtime.Caller使用說明
  • Golang中的time.Duration類型用法說明
  • golang time包做時間轉(zhuǎn)換操作
  • golang time常用方法詳解

標簽:電子產(chǎn)品 西雙版納 物業(yè)服務(wù) 青海 遼寧 安康 儋州 海南

巨人網(wǎng)絡(luò)通訊聲明:本文標題《golang xorm及time.Time自定義解決json日期格式的問題》,本文關(guān)鍵詞  golang,xorm,及,time.Time,自定義,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《golang xorm及time.Time自定義解決json日期格式的問題》相關(guān)的同類信息!
  • 本頁收集關(guān)于golang xorm及time.Time自定義解決json日期格式的問題的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    吴川市| 太康县| 乌兰浩特市| 武清区| 航空| 晋江市| 望江县| 大洼县| 区。| 阳西县| 兴安盟| 始兴县| 罗平县| 连州市| 满洲里市| 巴塘县| 鄯善县| 珲春市| 遂平县| 永德县| 宣威市| 桃江县| 浏阳市| 兴化市| 松溪县| 龙海市| 武平县| 胶州市| 淳安县| 晋州市| 南木林县| 礼泉县| 桦川县| 宜兰县| 桂阳县| 晋城| 乾安县| 象山县| 上杭县| 清远市| 沅陵县|