pcm-coordinator/pkg/utils/copier.go

65 lines
1.3 KiB
Go

/*
Copyright (c) [2023] [pcm]
[pcm-coordinator] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
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)
},
},
}