You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package wrapper
|
|
|
|
import (
|
|
"git.noahlan.cn/noahlan/ntool/nmap"
|
|
"git.noahlan.cn/noahlan/ntool/ntime"
|
|
"reflect"
|
|
"time"
|
|
)
|
|
|
|
// StrTimeWrapper Time->string | string->Time
|
|
type StrTimeWrapper struct {
|
|
BaseTypeWrapper
|
|
timeType reflect.Type
|
|
}
|
|
|
|
func NewStrTimeWrapper() TypeWrapper {
|
|
return &StrTimeWrapper{
|
|
timeType: reflect.TypeOf(time.Time{}),
|
|
}
|
|
}
|
|
|
|
func (*StrTimeWrapper) Supported(value reflect.Value) bool {
|
|
if _, ok := value.Interface().(time.Time); ok {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (*StrTimeWrapper) Wrap(valueElem reflect.Value, index int, tagMap nmap.SMap) reflect.Value {
|
|
layout := tagMap.Default("layout", time.DateTime)
|
|
t := valueElem.Interface().(time.Time)
|
|
return reflect.ValueOf(t.Format(layout))
|
|
}
|
|
|
|
func (t *StrTimeWrapper) Set(fieldElem reflect.Value, value any, tagMap nmap.SMap) {
|
|
layout := tagMap.Default("layout", time.DateTime)
|
|
var timeStr string
|
|
switch d := value.(type) {
|
|
case []byte:
|
|
timeStr = string(d)
|
|
case string:
|
|
timeStr = d
|
|
case int64:
|
|
fieldElem.Set(reflect.ValueOf(time.Unix(d, 0)))
|
|
case time.Time:
|
|
if fieldElem.Type() == t.timeType {
|
|
timeStr = ""
|
|
fieldElem.Set(reflect.ValueOf(value))
|
|
}
|
|
}
|
|
if timeStr != "" && fieldElem.Type() == t.timeType {
|
|
t, _ := ntime.ParseTime(timeStr, layout)
|
|
fieldElem.Set(reflect.ValueOf(t))
|
|
}
|
|
}
|