2 Star 0 Fork 70

golang-package/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
type_layout.go 2.03 KB
一键复制 编辑 原始数据 按行查看 历史
package carbon
import (
"bytes"
"database/sql/driver"
)
// LayoutType defines a LayoutType generic struct
type LayoutType[T LayoutTyper] struct {
*Carbon
}
// NewLayoutType returns a new LayoutType generic instance.
func NewLayoutType[T LayoutTyper](c *Carbon) *LayoutType[T] {
return &LayoutType[T]{
Carbon: c,
}
}
// Scan implements "driver.Scanner" interface for LayoutType generic struct.
func (t *LayoutType[T]) Scan(src any) error {
var c *Carbon
switch v := src.(type) {
case nil:
return nil
case []byte:
c = Parse(string(v))
case string:
c = Parse(v)
case StdTime:
c = CreateFromStdTime(v)
case *StdTime:
c = CreateFromStdTime(*v)
default:
return ErrFailedScan(v)
}
*t = *NewLayoutType[T](c)
return t.Error
}
// Value implements "driver.Valuer" interface for LayoutType generic struct.
func (t LayoutType[T]) Value() (driver.Value, error) {
if t.IsNil() || t.IsZero() || t.IsEmpty() {
return nil, nil
}
if t.HasError() {
return nil, t.Error
}
return t.StdTime(), nil
}
// MarshalJSON implements "json.Marshaler" interface for LayoutType generic struct.
func (t LayoutType[T]) MarshalJSON() ([]byte, error) {
if t.IsNil() || t.IsZero() || t.IsEmpty() {
return []byte(`null`), nil
}
if t.HasError() {
return []byte(`null`), t.Error
}
v := t.Layout(t.getLayout())
b := make([]byte, 0, len(v)+2)
b = append(b, '"')
b = append(b, v...)
b = append(b, '"')
return b, nil
}
// UnmarshalJSON implements "json.Unmarshaler" interface for LayoutType generic struct.
func (t *LayoutType[T]) UnmarshalJSON(src []byte) error {
v := string(bytes.Trim(src, `"`))
if v == "" || v == "null" {
return nil
}
*t = *NewLayoutType[T](ParseByLayout(v, t.getLayout()))
return t.Error
}
// String implements "Stringer" interface for LayoutType generic struct.
func (t *LayoutType[T]) String() string {
if t == nil || t.IsInvalid() {
return ""
}
return t.Layout(t.getLayout())
}
// getLayout returns the layout of LayoutType generic struct.
func (t *LayoutType[T]) getLayout() string {
var typer T
return typer.Layout()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/golang-package/carbon.git
git@gitee.com:golang-package/carbon.git
golang-package
carbon
carbon
master

搜索帮助