util/string_convert.go

654 lines
13 KiB
Go
Raw Permalink Normal View History

2023-05-04 18:40:25 +08:00
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-04 18:25
package util
import (
2023-05-04 19:06:10 +08:00
"strings"
2023-05-04 18:40:25 +08:00
"github.com/pkg/errors"
)
type stringConvert struct {
}
// ToFloat32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:26 2023/5/4
func (sc *stringConvert) ToFloat32(str string) (float32, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res float32
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
// ToFloat64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:26 2023/5/4
func (sc *stringConvert) ToFloat64(str string) (float64, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res float64
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
// ToDouble 转double
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:33 2023/5/4
func (sc *stringConvert) ToDouble(str string) (float64, error) {
return sc.ToFloat64(str)
}
// ToNumber 转数字, 使用最高精度的float64
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:33 2023/5/4
func (sc *stringConvert) ToNumber(str string) (float64, error) {
return sc.ToFloat64(str)
}
// ToInt8 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (sc *stringConvert) ToInt8(str string) (int8, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res int8
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
// ToInt16 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (sc *stringConvert) ToInt16(str string) (int16, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res int16
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
// ToInt32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (sc *stringConvert) ToInt32(str string) (int32, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res int32
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
// ToInt64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (sc *stringConvert) ToInt64(str string) (int64, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res int64
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
2023-05-05 10:48:55 +08:00
// ToInt ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:47 2023/5/5
2023-05-04 19:06:10 +08:00
func (sc *stringConvert) ToInt(str string) (int, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res int
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 19:06:10 +08:00
}
2023-05-04 18:40:25 +08:00
// ToUint8 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:30 2023/5/4
func (sc *stringConvert) ToUint8(str string) (uint8, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res uint8
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
// ToUint16 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (sc *stringConvert) ToUint16(str string) (uint16, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res uint16
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
// ToUint32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (sc *stringConvert) ToUint32(str string) (uint32, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res uint32
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
// ToUint64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (sc *stringConvert) ToUint64(str string) (uint64, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res uint64
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
// ToUint ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (sc *stringConvert) ToUint(str string) (uint, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res uint
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
// ToBool ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:34 2023/5/4
func (sc *stringConvert) ToBool(str string) (bool, error) {
2023-05-05 10:48:55 +08:00
var (
err error
res bool
)
err = ConvertAssign(&res, str)
return res, err
2023-05-04 18:40:25 +08:00
}
// ToObject ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:35 2023/5/4
func (sc *stringConvert) ToObject(str string) (map[string]interface{}, error) {
var (
err error
result map[string]interface{}
)
if err = JSON.UnmarshalWithNumber([]byte(str), &result); nil != err {
return nil, err
}
return result, nil
}
// ToStruct ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:38 2023/5/4
func (sc *stringConvert) ToStruct(str string, receiver interface{}) error {
if nil == receiver {
return errors.New("receiver is nil")
}
return JSON.UnmarshalWithNumber([]byte(str), receiver)
}
2023-05-04 19:06:10 +08:00
// ToInt8Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:55 2023/5/4
func (sc *stringConvert) ToInt8Slice(str string, splitChar ...string) ([]int8, error) {
var (
err error
res []int8
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int8, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToInt8(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToInt16Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:01 2023/5/4
func (sc *stringConvert) ToInt16Slice(str string, splitChar ...string) ([]int16, error) {
var (
err error
res []int16
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int16, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToInt16(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToInt32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:03 2023/5/4
func (sc *stringConvert) ToInt32Slice(str string, splitChar ...string) ([]int32, error) {
var (
err error
res []int32
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int32, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToInt32(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToInt64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:04 2023/5/4
func (sc *stringConvert) ToInt64Slice(str string, splitChar ...string) ([]int64, error) {
var (
err error
res []int64
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int64, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToInt64(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToIntSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:04 2023/5/4
func (sc *stringConvert) ToIntSlice(str string, splitChar ...string) ([]int, error) {
var (
err error
res []int
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToInt(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
2023-05-05 10:59:24 +08:00
// ToUint8Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:50 2023/5/5
func (sc *stringConvert) ToUint8Slice(str string, splitChar ...string) ([]uint8, error) {
var (
err error
res []uint8
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint8, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToUint8(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUint16Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:52 2023/5/5
func (sc *stringConvert) ToUint16Slice(str string, splitChar ...string) ([]uint16, error) {
var (
err error
res []uint16
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint16, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToUint16(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUint32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:53 2023/5/5
func (sc *stringConvert) ToUint32Slice(str string, splitChar ...string) ([]uint32, error) {
var (
err error
res []uint32
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint32, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToUint32(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUint64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/5
func (sc *stringConvert) ToUint64Slice(str string, splitChar ...string) ([]uint64, error) {
var (
err error
res []uint64
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint64, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToUint64(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUintSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/5
func (sc *stringConvert) ToUintSlice(str string, splitChar ...string) ([]uint, error) {
var (
err error
res []uint
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToUint(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToBoolSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:55 2023/5/5
func (sc *stringConvert) ToBoolSlice(str string, splitChar ...string) ([]bool, error) {
var (
err error
res []bool
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]bool, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToBool(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToFloat32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:56 2023/5/5
func (sc *stringConvert) ToFloat32Slice(str string, splitChar ...string) ([]float32, error) {
var (
err error
res []float32
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]float32, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToFloat32(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToFloat64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:56 2023/5/5
func (sc *stringConvert) ToFloat64Slice(str string, splitChar ...string) ([]float64, error) {
var (
err error
res []float64
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]float64, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToFloat64(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToDoubleSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/5
func (sc *stringConvert) ToDoubleSlice(str string, splitChar ...string) ([]float64, error) {
return sc.ToFloat64Slice(str, splitChar...)
}
// ToNumberSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/5
func (sc *stringConvert) ToNumberSlice(str string, splitChar ...string) ([]float64, error) {
return sc.ToFloat64Slice(str, splitChar...)
}