优化toAnySlice

This commit is contained in:
2024-01-23 10:42:03 +08:00
parent 448b472c53
commit 5a53679400
3 changed files with 39 additions and 12 deletions

View File

@ -990,13 +990,34 @@ func (str String) ToStringSlice(splitChar ...string) StringSliceResult {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:01 2023/5/5
func (str String) ToAnySlice() AnySliceResult {
func (str String) ToAnySlice(splitCharList ...string) AnySliceResult {
result := AnySliceResult{
Value: []interface{}{},
Err: nil,
}
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
if len(splitCharList) == 0 {
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
valArr := strings.Split(str.Value(), splitCharList[0])
valList := make([]interface{}, 0)
for _, item := range valArr {
v := String(item)
if res := v.ToInt64(); nil == res.Err {
valList = append(valList, res.Value)
} else if res := v.ToUint64(); nil == res.Err {
valList = append(valList, res.Value)
} else if res := v.ToFloat64(); nil == res.Err {
valList = append(valList, res.Value)
} else if res := v.ToBool(); nil == res.Err {
valList = append(valList, res.Value)
} else {
valList = append(valList, item)
}
}
result.Value = valList
return result
}