feat: 字符串转struct slice
This commit is contained in:
@ -38,3 +38,15 @@ type BaseValueSliceResult[BaseType op_type.BaseType] struct {
|
|||||||
Value []BaseType `json:"value"` // 转换结果
|
Value []BaseType `json:"value"` // 转换结果
|
||||||
Err error `json:"err"` // 错误信息
|
Err error `json:"err"` // 错误信息
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MapValueSliceResult map类型切片转换结果
|
||||||
|
type MapValueSliceResult[Key comparable, Value any] struct {
|
||||||
|
Value []map[Key]Value `json:"value"` // 转换结果
|
||||||
|
Err error `json:"err"` // 错误信息
|
||||||
|
}
|
||||||
|
|
||||||
|
// StructValueSliceResult struct类型切片转换结果
|
||||||
|
type StructValueSliceResult[Value any] struct {
|
||||||
|
Value []Value `json:"value"` // 转换结果
|
||||||
|
Err error `json:"err"` // 错误信息
|
||||||
|
}
|
||||||
|
@ -27,7 +27,7 @@ func ToBaseValue[BaseType op_type.BaseType](str string) define.BaseValueResult[B
|
|||||||
}
|
}
|
||||||
return define.BaseValueResult[BaseType]{
|
return define.BaseValueResult[BaseType]{
|
||||||
Value: target,
|
Value: target,
|
||||||
Err: err,
|
Err: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestToMap(t *testing.T) {
|
func TestToMap(t *testing.T) {
|
||||||
Convey("map[string]any转换成", t, func() {
|
Convey("map[string]any转换成功", t, func() {
|
||||||
testData := `{
|
testData := `{
|
||||||
"name": "baicha",
|
"name": "baicha",
|
||||||
"age": 18
|
"age": 18
|
||||||
@ -27,10 +27,19 @@ func TestToMap(t *testing.T) {
|
|||||||
So(res.Value["name"], ShouldEqual, "baicha")
|
So(res.Value["name"], ShouldEqual, "baicha")
|
||||||
So(res.Value["age"], ShouldEqual, json.Number("18"))
|
So(res.Value["age"], ShouldEqual, json.Number("18"))
|
||||||
})
|
})
|
||||||
|
Convey("map[string]any转换失败", t, func() {
|
||||||
|
testData := `
|
||||||
|
"name": "baicha",
|
||||||
|
"age": 18
|
||||||
|
}`
|
||||||
|
res := ToMap[string, any](testData)
|
||||||
|
So(res.Err, ShouldNotBeNil)
|
||||||
|
So(res.Value, ShouldBeNil)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToStruct(t *testing.T) {
|
func TestToStruct(t *testing.T) {
|
||||||
Convey("struct转换成", t, func() {
|
Convey("struct转换成功", t, func() {
|
||||||
testData := `{
|
testData := `{
|
||||||
"name": "baicha",
|
"name": "baicha",
|
||||||
"age": 18
|
"age": 18
|
||||||
@ -47,4 +56,21 @@ func TestToStruct(t *testing.T) {
|
|||||||
So(res.Value.Name, ShouldEqual, "baicha")
|
So(res.Value.Name, ShouldEqual, "baicha")
|
||||||
So(res.Value.Age, ShouldEqual, 18)
|
So(res.Value.Age, ShouldEqual, 18)
|
||||||
})
|
})
|
||||||
|
Convey("struct转换失败", t, func() {
|
||||||
|
testData := `
|
||||||
|
"name": "baicha",
|
||||||
|
"age": 18
|
||||||
|
}`
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Age int `json:"age"`
|
||||||
|
}
|
||||||
|
var u User
|
||||||
|
res := ToStruct(testData, &u)
|
||||||
|
So(res.Err, ShouldNotBeNil)
|
||||||
|
So(res.Value, ShouldNotBeNil)
|
||||||
|
So(res.Value.Name, ShouldEqual, "")
|
||||||
|
So(res.Value.Age, ShouldEqual, 0)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -17,27 +17,41 @@ import (
|
|||||||
|
|
||||||
// ToBaseTypeSlice 基础数据类型的列表
|
// ToBaseTypeSlice 基础数据类型的列表
|
||||||
// splitChar 没有用字符串表示的原因: 存在场景, 使用空字符串风格字符串, 空字符串是有意义的
|
// splitChar 没有用字符串表示的原因: 存在场景, 使用空字符串风格字符串, 空字符串是有意义的
|
||||||
func ToBaseTypeSlice[BaseTpe op_type.BaseType](str string, splitChar ...string) define.BaseValueSliceResult[BaseTpe] {
|
func ToBaseTypeSlice[BaseType op_type.BaseType](str string, splitChar ...string) define.BaseValueSliceResult[BaseType] {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
sliceValue []BaseTpe
|
sliceValue []BaseType
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(splitChar) == 0 {
|
if len(splitChar) == 0 {
|
||||||
// 序列化数组直接转换
|
// 序列化数组直接转换
|
||||||
if err = serialize.JSON.UnmarshalWithNumberForString(str, &sliceValue); nil != err {
|
if err = serialize.JSON.UnmarshalWithNumberForString(str, &sliceValue); nil != err {
|
||||||
return define.BaseValueSliceResult[BaseTpe]{Value: []BaseTpe{}, Err: err}
|
return define.BaseValueSliceResult[BaseType]{Value: []BaseType{}, Err: err}
|
||||||
}
|
}
|
||||||
return define.BaseValueSliceResult[BaseTpe]{Value: sliceValue, Err: nil}
|
return define.BaseValueSliceResult[BaseType]{Value: sliceValue, Err: nil}
|
||||||
}
|
}
|
||||||
// 按照分隔符转换
|
// 按照分隔符转换
|
||||||
strArr := strings.Split(str, splitChar[0])
|
strArr := strings.Split(str, splitChar[0])
|
||||||
for _, v := range strArr {
|
for _, v := range strArr {
|
||||||
itemConvertRes := ToBaseValue[BaseTpe](v)
|
itemConvertRes := ToBaseValue[BaseType](v)
|
||||||
if nil != itemConvertRes.Err {
|
if nil != itemConvertRes.Err {
|
||||||
return define.BaseValueSliceResult[BaseTpe]{Value: []BaseTpe{}, Err: err}
|
return define.BaseValueSliceResult[BaseType]{Value: []BaseType{}, Err: itemConvertRes.Err}
|
||||||
}
|
}
|
||||||
sliceValue = append(sliceValue, itemConvertRes.Value)
|
sliceValue = append(sliceValue, itemConvertRes.Value)
|
||||||
}
|
}
|
||||||
return define.BaseValueSliceResult[BaseTpe]{Value: sliceValue, Err: nil}
|
return define.BaseValueSliceResult[BaseType]{Value: sliceValue, Err: nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToMapSlice map类型的列表
|
||||||
|
func ToMapSlice[Key comparable, Value any](str string) define.MapValueSliceResult[Key, Value] {
|
||||||
|
res := define.MapValueSliceResult[Key, Value]{Value: []map[Key]Value{}, Err: nil}
|
||||||
|
res.Err = serialize.JSON.UnmarshalWithNumberForString(str, &res.Value)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToStructSlice 结构体类型的列表
|
||||||
|
func ToStructSlice[Value any](str string) define.StructValueSliceResult[Value] {
|
||||||
|
res := define.StructValueSliceResult[Value]{Value: []Value{}, Err: nil}
|
||||||
|
res.Err = serialize.JSON.UnmarshalWithNumberForString(str, &res.Value)
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,10 @@
|
|||||||
package op_string
|
package op_string
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -37,9 +39,38 @@ func TestToBaseTypeSlice(t *testing.T) {
|
|||||||
})
|
})
|
||||||
Convey("字符串转数组失败", t, func() {
|
Convey("字符串转数组失败", t, func() {
|
||||||
testData := `1,2,3,4,-5`
|
testData := `1,2,3,4,-5`
|
||||||
res := ToBaseTypeSlice[uint](testData)
|
res := ToBaseTypeSlice[uint](testData, ",")
|
||||||
So(res.Value, ShouldNotBeNil)
|
So(res.Value, ShouldNotBeNil)
|
||||||
So(len(res.Value), ShouldEqual, 0)
|
So(len(res.Value), ShouldEqual, 0)
|
||||||
So(res.Err, ShouldNotBeNil)
|
So(res.Err, ShouldNotBeNil)
|
||||||
|
serialize.JSON.ConsoleOutput(res)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToMapSlice(t *testing.T) {
|
||||||
|
Convey("序列化数据转map数组成功", t, func() {
|
||||||
|
testData := `[{"name":"baicha", "age": 18}]`
|
||||||
|
res := ToMapSlice[string, any](testData)
|
||||||
|
So(res.Value, ShouldNotBeNil)
|
||||||
|
So(res.Err, ShouldBeNil)
|
||||||
|
So(len(res.Value), ShouldEqual, 1)
|
||||||
|
So(res.Value[0]["name"], ShouldEqual, "baicha")
|
||||||
|
So(res.Value[0]["age"], ShouldEqual, json.Number("18"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToStructSlice(t *testing.T) {
|
||||||
|
Convey("序列化数据转map数组成功", t, func() {
|
||||||
|
testData := `[{"name":"baicha", "age": 18}]`
|
||||||
|
type User struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Age int `json:"age"`
|
||||||
|
}
|
||||||
|
res := ToStructSlice[User](testData)
|
||||||
|
So(res.Value, ShouldNotBeNil)
|
||||||
|
So(res.Err, ShouldBeNil)
|
||||||
|
So(len(res.Value), ShouldEqual, 1)
|
||||||
|
So(res.Value[0].Name, ShouldEqual, "baicha")
|
||||||
|
So(res.Value[0].Age, ShouldEqual, 18)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user