51 lines
866 B
Go
51 lines
866 B
Go
package utils
|
|
|
|
import (
|
|
"github.com/jinzhu/copier"
|
|
"github.com/pkg/errors"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
var 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 not matching")
|
|
}
|
|
|
|
return s.Format(time.RFC3339), nil
|
|
},
|
|
},
|
|
{
|
|
SrcType: copier.String,
|
|
DstType: copier.Int,
|
|
Fn: func(src interface{}) (interface{}, error) {
|
|
s, ok := src.(string)
|
|
|
|
if !ok {
|
|
return nil, errors.New("src type not matching")
|
|
}
|
|
|
|
return strconv.Atoi(s)
|
|
},
|
|
},
|
|
{
|
|
SrcType: copier.String,
|
|
DstType: copier.Bool,
|
|
Fn: func(src interface{}) (interface{}, error) {
|
|
s, ok := src.(string)
|
|
|
|
if !ok {
|
|
return nil, errors.New("src type not matching")
|
|
}
|
|
|
|
return strconv.ParseBool(s)
|
|
},
|
|
},
|
|
}
|