feat: 字符串转struct slice

This commit is contained in:
2025-10-13 15:21:33 +08:00
parent 033e013510
commit 80b5e4e7cc
5 changed files with 94 additions and 11 deletions

View File

@ -38,3 +38,15 @@ type BaseValueSliceResult[BaseType op_type.BaseType] struct {
Value []BaseType `json:"value"` // 转换结果
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"` // 错误信息
}

View File

@ -27,7 +27,7 @@ func ToBaseValue[BaseType op_type.BaseType](str string) define.BaseValueResult[B
}
return define.BaseValueResult[BaseType]{
Value: target,
Err: err,
Err: nil,
}
}

View File

@ -16,7 +16,7 @@ import (
)
func TestToMap(t *testing.T) {
Convey("map[string]any转换成", t, func() {
Convey("map[string]any转换成", t, func() {
testData := `{
"name": "baicha",
"age": 18
@ -27,10 +27,19 @@ func TestToMap(t *testing.T) {
So(res.Value["name"], ShouldEqual, "baicha")
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) {
Convey("struct转换成", t, func() {
Convey("struct转换成", t, func() {
testData := `{
"name": "baicha",
"age": 18
@ -47,4 +56,21 @@ func TestToStruct(t *testing.T) {
So(res.Value.Name, ShouldEqual, "baicha")
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)
})
}

View File

@ -17,27 +17,41 @@ import (
// ToBaseTypeSlice 基础数据类型的列表
// 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 (
err error
sliceValue []BaseTpe
sliceValue []BaseType
)
if len(splitChar) == 0 {
// 序列化数组直接转换
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])
for _, v := range strArr {
itemConvertRes := ToBaseValue[BaseTpe](v)
itemConvertRes := ToBaseValue[BaseType](v)
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)
}
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
}

View File

@ -8,8 +8,10 @@
package op_string
import (
"encoding/json"
"testing"
"git.zhangdeman.cn/zhangdeman/serialize"
. "github.com/smartystreets/goconvey/convey"
)
@ -37,9 +39,38 @@ func TestToBaseTypeSlice(t *testing.T) {
})
Convey("字符串转数组失败", t, func() {
testData := `1,2,3,4,-5`
res := ToBaseTypeSlice[uint](testData)
res := ToBaseTypeSlice[uint](testData, ",")
So(res.Value, ShouldNotBeNil)
So(len(res.Value), ShouldEqual, 0)
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)
})
}