diff --git a/define/result.go b/define/result.go index 174a6de..5e2c15f 100644 --- a/define/result.go +++ b/define/result.go @@ -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"` // 错误信息 +} diff --git a/op_string/base.go b/op_string/base.go index 1eb8d27..3d56c5d 100644 --- a/op_string/base.go +++ b/op_string/base.go @@ -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, } } diff --git a/op_string/map_test.go b/op_string/map_test.go index d2b8441..5def276 100644 --- a/op_string/map_test.go +++ b/op_string/map_test.go @@ -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) + }) } diff --git a/op_string/slice.go b/op_string/slice.go index 153f8d1..19a0a5e 100644 --- a/op_string/slice.go +++ b/op_string/slice.go @@ -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 } diff --git a/op_string/slice_test.go b/op_string/slice_test.go index 0617e35..6ddd007 100644 --- a/op_string/slice_test.go +++ b/op_string/slice_test.go @@ -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) }) }