溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

完善優(yōu)化對ORM多場景支持的方法

發(fā)布時間:2020-10-14 15:08:33 來源:億速云 閱讀:134 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關完善優(yōu)化對ORM多場景支持的方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

                                                       

carbon 是一個輕量級、語義化、對開發(fā)者友好的Golang時間處理庫,支持鏈式調用和gorm、xrom等主流orm

如果您覺得不錯,請給個star吧

github:github.com/golang-module/carbon

gitee:gitee.com/go-package/carbon

安裝使用
// 使用github庫
go get -u github.com/golang-module/carbon

import (
 "github.com/golang-module/carbon")

// 使用gitee庫
go get -u gitee.com/go-package/carbon

import (
 "gitee.com/go-package/carbon")
數(shù)據(jù)庫支持

假設數(shù)據(jù)表為users,字段有id(int)、name(varchar)、age(int)、graduated_at(date)、birthday(date)、created_at(datetime)、updated_at(datetime)、deleted_at(datetime)

type UserModel struct {
 ID  int64  `json:"id"` Name string `json:"name"` Age int `json:"age"` Birthday carbon.Carbon `json:"birthday"` CreatedAt carbon.ToDateTimeString `json:"created_at"` DeletedAt carbon.ToTimestamp `json:"deleted_at"` GraduatedAt carbon.ToDateString `json:"graduated_at"` UpdatedAt carbon.ToTimeString `json:"updated_at"`}
user := UserModel {
 Name: "勾國印", Age: 18, Birthday: carbon.Now().SubYears(18), CreatedAt: carbon.ToDateTimeString{carbon.Now()}, DeletedAt: carbon.ToTimestamp{carbon.Parse("2020-08-05 13:14:15")}, GraduatedAt: carbon.ToDateString{carbon.Parse("2012-09-09")}, UpdatedAt: carbon.ToTimeString{carbon.Now()},}
user.ID // 18
user.Name // 勾國印
user.Birthday.ToDateString() // 2012-08-05
user.CreatedAt.ToTimestamp() // 2012-08-05 13:14:15
user.DeletedAt.ToDateTimeString() // 1596604455
user.GraduatedAt.AddDay().ToDateString() // 2012-09-10
user.UpdatedAt.ToDateString() // 2012-08-05
data, _ := json.Marshal(&user)
fmt.Print(string(data))
// 輸出
{
 "id": 42, "name": "勾國印", "age": 18, "birthday": "2012-08-05 00:00:00", "created_at": "2020-08-05 13:14:15", "deleted_at": 1596604455 "graduated_at": "2012-09-09", "updated_at": "13:14:15",}
更新日志
  • 完善單元測試,代碼覆蓋率100%
  • 完善優(yōu)化對ORM的多場景支持
  • 新增英文版README.MD說明文檔
  • 統(tǒng)一異常處理
  • 統(tǒng)一輸出函數(shù)命名規(guī)則,將Format() 改為 ToFormatString()
  • 新增CreateFromGoTime(t time.Time)方法從原生time.Time創(chuàng)建Carbon實例
  • 優(yōu)化代碼組織結構,將不可繼承的最終方法統(tǒng)一放到final.go文件里
  • 廢棄New()初始化函數(shù),無需初始化即可直接使用
  • 新增多種時間格式輸出,如Cookie、W3C、RSS、RFC7231
  • 新增ParseByDuration()方法解析持續(xù)時間字符串(相對于今天),支持正負整數(shù)/浮點數(shù)和符號ns(納秒)、us(微妙)、ms(毫秒)、s(秒)、m(分鐘)、h(小時)的組合
  • 新增NextYears()、NextYear()、PreYears()、PreYear()方法防止出現(xiàn)添加/減少指定年時出現(xiàn)跨月的現(xiàn)象
  • 新增NextMonths()、NextMonth()、PreMonths()、PreMonth()方法防止出現(xiàn)添加/減少指定月后出現(xiàn)跨月的現(xiàn)象
  • 新增DaysInYear()方法獲取本年的總天數(shù)
  • 新增DaysInMonth()方法獲取本月的總天數(shù)
  • 新增MonthOfYear()方法獲取本年的第幾月
  • 新增DayOfYear()方法獲取本年的第幾天
  • 新增DayOfMonth()方法獲取本月的第幾天
  • 新增DayOfWeek()方法獲取本周的第幾天
  • 新增WeekOfYear()方法獲取本年的第幾周
  • 新增WeekOfMonth()方法獲取本月的第幾周
  • 新增IsZero()方法判斷是否是零值時間
  • 新增IsFuture()方法判斷是否是未來時間
  • 新增IsPast()方法判斷是否是過去時間
  • 新增IsNow()方法判斷是否是當前時間

感謝各位的閱讀!關于完善優(yōu)化對ORM多場景支持的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI