micro-document/pkg/utils/copierOptions.go
2025-09-24 10:24:52 +08:00

41 lines
838 B
Go

package util
import (
"errors"
"time"
"micro-document/pkg/utils/stime"
"github.com/jinzhu/copier"
)
var CopierProtoOptions = copier.Option{
IgnoreEmpty: true,
DeepCopy: true,
Converters: []copier.TypeConverter{
{
SrcType: time.Time{},
DstType: copier.String,
Fn: func(src interface{}) (interface{}, error) {
s, ok := src.(time.Time)
if !ok {
return nil, errors.New("src type :time.Time not matching")
}
return s.Format(stime.Format_Normal_YMDhms), nil
},
},
{
SrcType: copier.String,
DstType: time.Time{},
Fn: func(src interface{}) (interface{}, error) {
s, ok := src.(string)
if !ok {
return nil, errors.New("src type :time.Time not matching")
}
tt, err := stime.StringToTimeWithFormat(s, stime.Format_Normal_YMDhms)
return *tt, err
},
},
},
}