Cron_Admin/app/utils/mapstructure/my_decode.go

27 lines
687 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
}