pcm-coordinator/common/tool/convert.go

54 lines
1.1 KiB
Go

package tool
import (
"encoding/json"
"github.com/robfig/cron/v3"
"io"
"mime/multipart"
"sigs.k8s.io/yaml"
"strconv"
"strings"
)
// Convert 通过JSON赋值
func Convert(source interface{}, target interface{}) {
jsonByte, _ := json.Marshal(source)
json.Unmarshal(jsonByte, &target)
}
// Int64ToString int64转string
func Int64ToString(value int64) string {
return strconv.FormatInt(value, 10)
}
// EntryIdToString EntryID转string
func EntryIdToString(id cron.EntryID) string {
return strconv.Itoa(int(id))
}
func StringToInt(value string) int {
intValue, _ := strconv.Atoi(value)
return intValue
}
func RunTimeToSeconds(runTime string) int {
time := strings.Split(runTime, ":")
day, _ := strconv.Atoi(time[0])
hour, _ := strconv.Atoi(time[1])
seconds, _ := strconv.Atoi(time[2])
return day*3600 + hour*60 + seconds
}
func Yaml2struct(fileHeader *multipart.FileHeader, req interface{}) error {
file, err := fileHeader.Open()
if err != nil {
return err
}
fileByte, err := io.ReadAll(file)
if err != nil {
return err
}
yaml.Unmarshal(fileByte, &req)
return nil
}