对string类型的包装

This commit is contained in:
白茶清欢 2023-05-08 18:29:48 +08:00
parent a1748cc324
commit 763009ad36
2 changed files with 393 additions and 268 deletions

140
define.go
View File

@ -203,3 +203,143 @@ type StringResult struct {
Value string Value string
Err error Err error
} }
// Int8SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:49 2023/5/8
type Int8SliceResult struct {
Value []int8
Err error
}
// Int16SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:49 2023/5/8
type Int16SliceResult struct {
Value []int16
Err error
}
// Int32SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:50 2023/5/8
type Int32SliceResult struct {
Value []int32
Err error
}
// Int64SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:50 2023/5/8
type Int64SliceResult struct {
Value []int64
Err error
}
// IntSliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:50 2023/5/8
type IntSliceResult struct {
Value []int
Err error
}
// Uint8SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint8SliceResult struct {
Value []uint8
Err error
}
// Uint16SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint16SliceResult struct {
Value []uint16
Err error
}
// Uint32SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint32SliceResult struct {
Value []uint32
Err error
}
// Uint64SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint64SliceResult struct {
Value []uint64
Err error
}
// UintSliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:56 2023/5/8
type UintSliceResult struct {
Value []uint
Err error
}
// BoolSliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:22 2023/5/8
type BoolSliceResult struct {
Value []bool
Err error
}
// Float32SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:24 2023/5/8
type Float32SliceResult struct {
Value []float32
Err error
}
// Float64SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:24 2023/5/8
type Float64SliceResult struct {
Value []float64
Err error
}
// AnySliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:28 2023/5/8
type AnySliceResult struct {
Value []interface{}
Err error
}

519
string.go
View File

@ -25,13 +25,12 @@ type String string
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:26 2023/5/4 // Date : 18:26 2023/5/4
func (str String) ToFloat32() (float32, error) { func (str String) ToFloat32() Float32Result {
var ( var (
err error res Float32Result
res float32
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToFloat64 ... // ToFloat64 ...
@ -39,13 +38,12 @@ func (str String) ToFloat32() (float32, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:26 2023/5/4 // Date : 18:26 2023/5/4
func (str String) ToFloat64() (float64, error) { func (str String) ToFloat64() Float64Result {
var ( var (
err error res Float64Result
res float64
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToDouble 转double // ToDouble 转double
@ -53,7 +51,7 @@ func (str String) ToFloat64() (float64, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:33 2023/5/4 // Date : 18:33 2023/5/4
func (str String) ToDouble() (float64, error) { func (str String) ToDouble() Float64Result {
return str.ToFloat64() return str.ToFloat64()
} }
@ -62,7 +60,7 @@ func (str String) ToDouble() (float64, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:33 2023/5/4 // Date : 18:33 2023/5/4
func (str String) ToNumber() (float64, error) { func (str String) ToNumber() Float64Result {
return str.ToFloat64() return str.ToFloat64()
} }
@ -71,13 +69,12 @@ func (str String) ToNumber() (float64, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:29 2023/5/4 // Date : 18:29 2023/5/4
func (str String) ToInt8() (int8, error) { func (str String) ToInt8() Int8Result {
var ( var (
err error res Int8Result
res int8
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToInt16 ... // ToInt16 ...
@ -85,13 +82,12 @@ func (str String) ToInt8() (int8, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:29 2023/5/4 // Date : 18:29 2023/5/4
func (str String) ToInt16() (int16, error) { func (str String) ToInt16() Int16Result {
var ( var (
err error res Int16Result
res int16
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToInt32 ... // ToInt32 ...
@ -99,13 +95,12 @@ func (str String) ToInt16() (int16, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:29 2023/5/4 // Date : 18:29 2023/5/4
func (str String) ToInt32() (int32, error) { func (str String) ToInt32() Int32Result {
var ( var (
err error res Int32Result
res int32
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToInt64 ... // ToInt64 ...
@ -113,13 +108,12 @@ func (str String) ToInt32() (int32, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:29 2023/5/4 // Date : 18:29 2023/5/4
func (str String) ToInt64() (int64, error) { func (str String) ToInt64() Int64Result {
var ( var (
err error res Int64Result
res int64
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToInt ... // ToInt ...
@ -127,13 +121,12 @@ func (str String) ToInt64() (int64, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:47 2023/5/5 // Date : 10:47 2023/5/5
func (str String) ToInt() (int, error) { func (str String) ToInt() IntResult {
var ( var (
err error res IntResult
res int
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToUint8 ... // ToUint8 ...
@ -141,13 +134,12 @@ func (str String) ToInt() (int, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:30 2023/5/4 // Date : 18:30 2023/5/4
func (str String) ToUint8() (uint8, error) { func (str String) ToUint8() Uint8Result {
var ( var (
err error res Uint8Result
res uint8
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToUint16 ... // ToUint16 ...
@ -155,13 +147,12 @@ func (str String) ToUint8() (uint8, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:29 2023/5/4 // Date : 18:29 2023/5/4
func (str String) ToUint16() (uint16, error) { func (str String) ToUint16() Uint16Result {
var ( var (
err error res Uint16Result
res uint16
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToUint32 ... // ToUint32 ...
@ -169,13 +160,12 @@ func (str String) ToUint16() (uint16, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:29 2023/5/4 // Date : 18:29 2023/5/4
func (str String) ToUint32() (uint32, error) { func (str String) ToUint32() Uint32Result {
var ( var (
err error res Uint32Result
res uint32
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToUint64 ... // ToUint64 ...
@ -183,13 +173,12 @@ func (str String) ToUint32() (uint32, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:29 2023/5/4 // Date : 18:29 2023/5/4
func (str String) ToUint64() (uint64, error) { func (str String) ToUint64() Uint64Result {
var ( var (
err error res Uint64Result
res uint64
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToUint ... // ToUint ...
@ -197,13 +186,12 @@ func (str String) ToUint64() (uint64, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:29 2023/5/4 // Date : 18:29 2023/5/4
func (str String) ToUint() (uint, error) { func (str String) ToUint() UintResult {
var ( var (
err error res UintResult
res uint
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToBool ... // ToBool ...
@ -211,13 +199,12 @@ func (str String) ToUint() (uint, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:34 2023/5/4 // Date : 18:34 2023/5/4
func (str String) ToBool() (bool, error) { func (str String) ToBool() BoolResult {
var ( var (
err error res BoolResult
res bool
) )
err = util.ConvertAssign(&res, str) res.Err = util.ConvertAssign(&res.Value, str)
return res, err return res
} }
// ToObject ... // ToObject ...
@ -225,15 +212,15 @@ func (str String) ToBool() (bool, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:35 2023/5/4 // Date : 18:35 2023/5/4
func (str String) ToObject() (map[string]interface{}, error) { func (str String) ToObject() ObjectResult {
var ( var (
err error res = ObjectResult{
result map[string]interface{} Value: map[string]interface{}{},
) Err: nil,
if err = util.JSON.UnmarshalWithNumber([]byte(str), &result); nil != err {
return nil, err
} }
return result, nil )
res.Err = util.JSON.UnmarshalWithNumber([]byte(str), &res.Value)
return res
} }
// ToStruct ... // ToStruct ...
@ -253,29 +240,31 @@ func (str String) ToStruct(receiver interface{}) error {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:55 2023/5/4 // Date : 18:55 2023/5/4
func (str String) ToInt8Slice(splitChar ...string) ([]int8, error) { func (str String) ToInt8Slice(splitChar ...string) Int8SliceResult {
var ( result := Int8SliceResult{
err error Value: []int8{},
res []int8 Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
}
return res, nil
} }
var (
res = make([]int8, 0) res = make([]int8, 0)
)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToInt8(); nil != err { if itemVal := String(item).ToInt8(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil result.Value = res
return result
} }
// ToInt16Slice ... // ToInt16Slice ...
@ -283,29 +272,30 @@ func (str String) ToInt8Slice(splitChar ...string) ([]int8, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 19:01 2023/5/4 // Date : 19:01 2023/5/4
func (str String) ToInt16Slice(splitChar ...string) ([]int16, error) { func (str String) ToInt16Slice(splitChar ...string) Int16SliceResult {
var (
err error result := Int16SliceResult{
res []int16 Value: []int16{},
) Err: nil,
}
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]int16, 0)
}
res = make([]int16, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToInt16(); nil != err { if itemVal := String(item).ToInt16(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil result.Value = res
return result
} }
// ToInt32Slice ... // ToInt32Slice ...
@ -313,29 +303,29 @@ func (str String) ToInt16Slice(splitChar ...string) ([]int16, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 19:03 2023/5/4 // Date : 19:03 2023/5/4
func (str String) ToInt32Slice(splitChar ...string) ([]int32, error) { func (str String) ToInt32Slice(splitChar ...string) Int32SliceResult {
var ( result := Int32SliceResult{
err error Value: []int32{},
res []int32 Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]int32, 0)
}
res = make([]int32, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToInt32(); nil != err { if itemVal := String(item).ToInt32(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil result.Value = res
return result
} }
// ToInt64Slice ... // ToInt64Slice ...
@ -343,29 +333,28 @@ func (str String) ToInt32Slice(splitChar ...string) ([]int32, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 19:04 2023/5/4 // Date : 19:04 2023/5/4
func (str String) ToInt64Slice(splitChar ...string) ([]int64, error) { func (str String) ToInt64Slice(splitChar ...string) Int64SliceResult {
var ( result := Int64SliceResult{
err error Value: []int64{},
res []int64 Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]int64, 0)
}
res = make([]int64, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToInt64(); nil != err { if itemVal := String(item).ToInt64(); nil != itemVal.Err {
return nil, err return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil result.Value = res
return result
} }
// ToIntSlice ... // ToIntSlice ...
@ -373,29 +362,29 @@ func (str String) ToInt64Slice(splitChar ...string) ([]int64, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 19:04 2023/5/4 // Date : 19:04 2023/5/4
func (str String) ToIntSlice(splitChar ...string) ([]int, error) { func (str String) ToIntSlice(splitChar ...string) IntSliceResult {
var ( result := IntSliceResult{
err error Value: []int{},
res []int Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]int, 0)
}
res = make([]int, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToInt(); nil != err { if itemVal := String(item).ToInt(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil result.Value = res
return result
} }
// ToUint8Slice ... // ToUint8Slice ...
@ -403,29 +392,29 @@ func (str String) ToIntSlice(splitChar ...string) ([]int, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:50 2023/5/5 // Date : 10:50 2023/5/5
func (str String) ToUint8Slice(splitChar ...string) ([]uint8, error) { func (str String) ToUint8Slice(splitChar ...string) Uint8SliceResult {
var ( result := Uint8SliceResult{
err error Value: []uint8{},
res []uint8 Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]uint8, 0)
}
res = make([]uint8, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToUint8(); nil != err { if itemVal := String(item).ToUint8(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil result.Value = res
return result
} }
// ToUint16Slice ... // ToUint16Slice ...
@ -433,29 +422,29 @@ func (str String) ToUint8Slice(splitChar ...string) ([]uint8, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:52 2023/5/5 // Date : 10:52 2023/5/5
func (str String) ToUint16Slice(splitChar ...string) ([]uint16, error) { func (str String) ToUint16Slice(splitChar ...string) Uint16SliceResult {
var ( result := Uint16SliceResult{
err error Value: []uint16{},
res []uint16 Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]uint16, 0)
}
res = make([]uint16, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToUint16(); nil != err { if itemVal := String(item).ToUint16(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil result.Value = res
return result
} }
// ToUint32Slice ... // ToUint32Slice ...
@ -463,29 +452,29 @@ func (str String) ToUint16Slice(splitChar ...string) ([]uint16, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:53 2023/5/5 // Date : 10:53 2023/5/5
func (str String) ToUint32Slice(splitChar ...string) ([]uint32, error) { func (str String) ToUint32Slice(splitChar ...string) Uint32SliceResult {
var ( result := Uint32SliceResult{
err error Value: []uint32{},
res []uint32 Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]uint32, 0)
}
res = make([]uint32, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToUint32(); nil != err { if itemVal := String(item).ToUint32(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil result.Value = res
return result
} }
// ToUint64Slice ... // ToUint64Slice ...
@ -493,29 +482,28 @@ func (str String) ToUint32Slice(splitChar ...string) ([]uint32, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:54 2023/5/5 // Date : 10:54 2023/5/5
func (str String) ToUint64Slice(splitChar ...string) ([]uint64, error) { func (str String) ToUint64Slice(splitChar ...string) Uint64SliceResult {
var ( result := Uint64SliceResult{
err error Value: []uint64{},
res []uint64 Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]uint64, 0)
}
res = make([]uint64, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToUint64(); nil != err { if itemVal := String(item).ToUint64(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil return result
} }
// ToUintSlice ... // ToUintSlice ...
@ -523,29 +511,29 @@ func (str String) ToUint64Slice(splitChar ...string) ([]uint64, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:54 2023/5/5 // Date : 10:54 2023/5/5
func (str String) ToUintSlice(splitChar ...string) ([]uint, error) { func (str String) ToUintSlice(splitChar ...string) UintSliceResult {
var ( result := UintSliceResult{
err error Value: []uint{},
res []uint Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]uint, 0)
}
res = make([]uint, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToUint(); nil != err { if itemVal := String(item).ToUint(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil result.Value = res
return result
} }
// ToBoolSlice ... // ToBoolSlice ...
@ -553,29 +541,29 @@ func (str String) ToUintSlice(splitChar ...string) ([]uint, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:55 2023/5/5 // Date : 10:55 2023/5/5
func (str String) ToBoolSlice(splitChar ...string) ([]bool, error) { func (str String) ToBoolSlice(splitChar ...string) BoolSliceResult {
var ( result := BoolSliceResult{
err error Value: []bool{},
res []bool Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]bool, 0)
}
res = make([]bool, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToBool(); nil != err { if itemVal := String(item).ToBool(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil result.Value = res
return result
} }
// ToFloat32Slice ... // ToFloat32Slice ...
@ -583,29 +571,28 @@ func (str String) ToBoolSlice(splitChar ...string) ([]bool, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:56 2023/5/5 // Date : 10:56 2023/5/5
func (str String) ToFloat32Slice(splitChar ...string) ([]float32, error) { func (str String) ToFloat32Slice(splitChar ...string) Float32SliceResult {
var ( result := Float32SliceResult{
err error Value: []float32{},
res []float32 Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]float32, 0)
}
res = make([]float32, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToFloat32(); nil != err { if itemVal := String(item).ToFloat32(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil return result
} }
// ToFloat64Slice ... // ToFloat64Slice ...
@ -613,29 +600,29 @@ func (str String) ToFloat32Slice(splitChar ...string) ([]float32, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:56 2023/5/5 // Date : 10:56 2023/5/5
func (str String) ToFloat64Slice(splitChar ...string) ([]float64, error) { func (str String) ToFloat64Slice(splitChar ...string) Float64SliceResult {
var ( result := Float64SliceResult{
err error Value: []float64{},
res []float64 Err: nil,
) }
isSplit := len(splitChar) > 0 isSplit := len(splitChar) > 0
if !isSplit { if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
} }
return res, nil res := make([]float64, 0)
}
res = make([]float64, 0)
arr := strings.Split(string(str), splitChar[0]) arr := strings.Split(string(str), splitChar[0])
for _, item := range arr { for _, item := range arr {
if itemVal, err := String(item).ToFloat64(); nil != err { if itemVal := String(item).ToFloat64(); nil != itemVal.Err {
return nil, err result.Err = itemVal.Err
return result
} else { } else {
res = append(res, itemVal) res = append(res, itemVal.Value)
} }
} }
return res, nil result.Value = res
return result
} }
// ToDoubleSlice ... // ToDoubleSlice ...
@ -643,7 +630,7 @@ func (str String) ToFloat64Slice(splitChar ...string) ([]float64, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:57 2023/5/5 // Date : 10:57 2023/5/5
func (str String) ToDoubleSlice(splitChar ...string) ([]float64, error) { func (str String) ToDoubleSlice(splitChar ...string) Float64SliceResult {
return str.ToFloat64Slice(splitChar...) return str.ToFloat64Slice(splitChar...)
} }
@ -652,7 +639,7 @@ func (str String) ToDoubleSlice(splitChar ...string) ([]float64, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:57 2023/5/5 // Date : 10:57 2023/5/5
func (str String) ToNumberSlice(splitChar ...string) ([]float64, error) { func (str String) ToNumberSlice(splitChar ...string) Float64SliceResult {
return str.ToFloat64Slice(splitChar...) return str.ToFloat64Slice(splitChar...)
} }
@ -661,14 +648,12 @@ func (str String) ToNumberSlice(splitChar ...string) ([]float64, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:01 2023/5/5 // Date : 15:01 2023/5/5
func (str String) ToAnySlice() ([]interface{}, error) { func (str String) ToAnySlice() AnySliceResult {
var ( result := AnySliceResult{
err error Value: []interface{}{},
res []interface{} Err: nil,
) }
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { result.Err = util.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return nil, err return result
}
return res, nil
} }