优化toAnySlice
This commit is contained in:
parent
448b472c53
commit
5a53679400
20
define.go
20
define.go
@ -472,16 +472,6 @@ type StringSliceResult struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
// AnySliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:28 2023/5/8
|
||||
type AnySliceResult struct {
|
||||
Value []interface{}
|
||||
Err error
|
||||
}
|
||||
|
||||
// MapResult 转map的结果
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
@ -491,3 +481,13 @@ type MapResult struct {
|
||||
Value Map
|
||||
Err error
|
||||
}
|
||||
|
||||
// AnySliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:28 2023/5/8
|
||||
type AnySliceResult struct {
|
||||
Value []interface{}
|
||||
Err error
|
||||
}
|
||||
|
25
string.go
25
string.go
@ -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
|
||||
}
|
||||
|
||||
|
@ -23,3 +23,9 @@ func TestString_ToFloat32(t *testing.T) {
|
||||
fmt.Println(str.ToInt())
|
||||
fmt.Println(str.ToUint())
|
||||
}
|
||||
|
||||
func TestString_ToAnySlice(t *testing.T) {
|
||||
str := "1,2,3"
|
||||
r := String(str).ToAnySlice(",")
|
||||
fmt.Println(r)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user