675 lines
13 KiB
Go
675 lines
13 KiB
Go
// Package wrapper ...
|
|
//
|
|
// Description : wrapper ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2023-05-05 11:59
|
|
package wrapper
|
|
|
|
import (
|
|
"errors"
|
|
"git.zhangdeman.cn/zhangdeman/util"
|
|
"strings"
|
|
)
|
|
|
|
// String 字符串类型包装
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 12:07 2023/5/5
|
|
type String string
|
|
|
|
// ToFloat32 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:26 2023/5/4
|
|
func (str String) ToFloat32() (float32, error) {
|
|
var (
|
|
err error
|
|
res float32
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToFloat64 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:26 2023/5/4
|
|
func (str String) ToFloat64() (float64, error) {
|
|
var (
|
|
err error
|
|
res float64
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToDouble 转double
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:33 2023/5/4
|
|
func (str String) ToDouble() (float64, error) {
|
|
return str.ToFloat64()
|
|
}
|
|
|
|
// ToNumber 转数字, 使用最高精度的float64
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:33 2023/5/4
|
|
func (str String) ToNumber() (float64, error) {
|
|
return str.ToFloat64()
|
|
}
|
|
|
|
// ToInt8 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:29 2023/5/4
|
|
func (str String) ToInt8() (int8, error) {
|
|
var (
|
|
err error
|
|
res int8
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToInt16 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:29 2023/5/4
|
|
func (str String) ToInt16() (int16, error) {
|
|
var (
|
|
err error
|
|
res int16
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToInt32 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:29 2023/5/4
|
|
func (str String) ToInt32() (int32, error) {
|
|
var (
|
|
err error
|
|
res int32
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToInt64 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:29 2023/5/4
|
|
func (str String) ToInt64() (int64, error) {
|
|
var (
|
|
err error
|
|
res int64
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToInt ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 10:47 2023/5/5
|
|
func (str String) ToInt() (int, error) {
|
|
var (
|
|
err error
|
|
res int
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToUint8 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:30 2023/5/4
|
|
func (str String) ToUint8() (uint8, error) {
|
|
var (
|
|
err error
|
|
res uint8
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToUint16 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:29 2023/5/4
|
|
func (str String) ToUint16() (uint16, error) {
|
|
var (
|
|
err error
|
|
res uint16
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToUint32 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:29 2023/5/4
|
|
func (str String) ToUint32() (uint32, error) {
|
|
var (
|
|
err error
|
|
res uint32
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToUint64 ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:29 2023/5/4
|
|
func (str String) ToUint64() (uint64, error) {
|
|
var (
|
|
err error
|
|
res uint64
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToUint ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:29 2023/5/4
|
|
func (str String) ToUint() (uint, error) {
|
|
var (
|
|
err error
|
|
res uint
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToBool ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:34 2023/5/4
|
|
func (str String) ToBool() (bool, error) {
|
|
var (
|
|
err error
|
|
res bool
|
|
)
|
|
err = util.ConvertAssign(&res, str)
|
|
return res, err
|
|
}
|
|
|
|
// ToObject ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:35 2023/5/4
|
|
func (str String) ToObject() (map[string]interface{}, error) {
|
|
var (
|
|
err error
|
|
result map[string]interface{}
|
|
)
|
|
if err = util.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 (str String) ToStruct(receiver interface{}) error {
|
|
if nil == receiver {
|
|
return errors.New("receiver is nil")
|
|
}
|
|
return util.JSON.UnmarshalWithNumber([]byte(str), receiver)
|
|
}
|
|
|
|
// ToInt8Slice ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:55 2023/5/4
|
|
func (str String) ToInt8Slice(splitChar ...string) ([]int8, error) {
|
|
var (
|
|
err error
|
|
res []int8
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]int8, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToInt8(); 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 (str String) ToInt16Slice(splitChar ...string) ([]int16, error) {
|
|
var (
|
|
err error
|
|
res []int16
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]int16, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToInt16(); 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 (str String) ToInt32Slice(splitChar ...string) ([]int32, error) {
|
|
var (
|
|
err error
|
|
res []int32
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]int32, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToInt32(); 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 (str String) ToInt64Slice(splitChar ...string) ([]int64, error) {
|
|
var (
|
|
err error
|
|
res []int64
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]int64, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToInt64(); 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 (str String) ToIntSlice(splitChar ...string) ([]int, error) {
|
|
var (
|
|
err error
|
|
res []int
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]int, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToInt(); nil != err {
|
|
return nil, err
|
|
} else {
|
|
res = append(res, itemVal)
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// ToUint8Slice ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 10:50 2023/5/5
|
|
func (str String) ToUint8Slice(splitChar ...string) ([]uint8, error) {
|
|
var (
|
|
err error
|
|
res []uint8
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]uint8, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToUint8(); 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 (str String) ToUint16Slice(splitChar ...string) ([]uint16, error) {
|
|
var (
|
|
err error
|
|
res []uint16
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]uint16, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToUint16(); 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 (str String) ToUint32Slice(splitChar ...string) ([]uint32, error) {
|
|
var (
|
|
err error
|
|
res []uint32
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]uint32, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToUint32(); 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 (str String) ToUint64Slice(splitChar ...string) ([]uint64, error) {
|
|
var (
|
|
err error
|
|
res []uint64
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]uint64, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToUint64(); 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 (str String) ToUintSlice(splitChar ...string) ([]uint, error) {
|
|
var (
|
|
err error
|
|
res []uint
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]uint, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToUint(); 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 (str String) ToBoolSlice(splitChar ...string) ([]bool, error) {
|
|
var (
|
|
err error
|
|
res []bool
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]bool, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToBool(); 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 (str String) ToFloat32Slice(splitChar ...string) ([]float32, error) {
|
|
var (
|
|
err error
|
|
res []float32
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]float32, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToFloat32(); 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 (str String) ToFloat64Slice(splitChar ...string) ([]float64, error) {
|
|
var (
|
|
err error
|
|
res []float64
|
|
)
|
|
|
|
isSplit := len(splitChar) > 0
|
|
if !isSplit {
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
res = make([]float64, 0)
|
|
arr := strings.Split(string(str), splitChar[0])
|
|
for _, item := range arr {
|
|
if itemVal, err := String(item).ToFloat64(); 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 (str String) ToDoubleSlice(splitChar ...string) ([]float64, error) {
|
|
return str.ToFloat64Slice(splitChar...)
|
|
}
|
|
|
|
// ToNumberSlice ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 10:57 2023/5/5
|
|
func (str String) ToNumberSlice(splitChar ...string) ([]float64, error) {
|
|
return str.ToFloat64Slice(splitChar...)
|
|
}
|
|
|
|
// ToAnySlice ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 15:01 2023/5/5
|
|
func (str String) ToAnySlice() ([]interface{}, error) {
|
|
var (
|
|
err error
|
|
res []interface{}
|
|
)
|
|
|
|
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|