27 lines
687 B
Go
27 lines
687 B
Go
package mapstructure
|
||
|
||
import "time"
|
||
|
||
// DecodeWithTime 支持时间转字符串
|
||
// 支持
|
||
// 1. *Time.time 转 string/*string
|
||
// 2. *Time.time 转 uint/uint32/uint64/int/int32/int64,支持带指针
|
||
// 不能用 Time.time 转,它会在上层认为是一个结构体数据而直接转成map,再到hook方法
|
||
func DecodeWithTime(input, output interface{}, layout string) error {
|
||
if layout == "" {
|
||
layout = time.DateTime
|
||
}
|
||
config := &DecoderConfig{
|
||
Metadata: nil,
|
||
Result: output,
|
||
DecodeHook: ComposeDecodeHookFunc(TimeToStringHook(layout), TimeToUnixIntHook()),
|
||
}
|
||
|
||
decoder, err := NewDecoder(config)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
return decoder.Decode(input)
|
||
}
|