数据操作升级为基于泛型实现 #10
@ -11,24 +11,30 @@ import "git.zhangdeman.cn/zhangdeman/op_type"
 | 
			
		||||
 | 
			
		||||
// BaseValueResult 基础类型转换结果
 | 
			
		||||
type BaseValueResult[BaseType op_type.BaseType] struct {
 | 
			
		||||
	Value BaseType `json:"result"` // 转换结果
 | 
			
		||||
	Err   error    `json:"err"`    // 错误信息
 | 
			
		||||
	Value BaseType `json:"value"` // 转换结果
 | 
			
		||||
	Err   error    `json:"err"`   // 错误信息
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BaseValuePtrResult 基础类型指针转换结果
 | 
			
		||||
type BaseValuePtrResult[BaseType op_type.BaseType] struct {
 | 
			
		||||
	Value *BaseType `json:"result"` // 转换结果
 | 
			
		||||
	Err   error     `json:"err"`    // 错误信息
 | 
			
		||||
	Value *BaseType `json:"value"` // 转换结果
 | 
			
		||||
	Err   error     `json:"err"`   // 错误信息
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MapValueResult map类型转换结果
 | 
			
		||||
type MapValueResult[Key comparable, Value any] struct {
 | 
			
		||||
	Value map[Key]Value `json:"result"` // 转换结果
 | 
			
		||||
	Err   error         `json:"err"`    // 错误信息
 | 
			
		||||
	Value map[Key]Value `json:"value"` // 转换结果
 | 
			
		||||
	Err   error         `json:"err"`   // 错误信息
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// StructValueResult struct类型转换结果
 | 
			
		||||
type StructValueResult[Value any] struct {
 | 
			
		||||
	Value Value `json:"result"` // 转换结果
 | 
			
		||||
	Err   error `json:"err"`    // 错误信息
 | 
			
		||||
	Value Value `json:"value"` // 转换结果
 | 
			
		||||
	Err   error `json:"err"`   // 错误信息
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BaseValueSliceResult 基础类型切片转换结果
 | 
			
		||||
type BaseValueSliceResult[BaseType op_type.BaseType] struct {
 | 
			
		||||
	Value []BaseType `json:"value"` // 转换结果
 | 
			
		||||
	Err   error      `json:"err"`   // 错误信息
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										43
									
								
								op_string/slice.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								op_string/slice.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,43 @@
 | 
			
		||||
// Package op_string ...
 | 
			
		||||
//
 | 
			
		||||
// Description : op_string ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 2025-10-13 14:21
 | 
			
		||||
package op_string
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"git.zhangdeman.cn/zhangdeman/op_type"
 | 
			
		||||
	"git.zhangdeman.cn/zhangdeman/serialize"
 | 
			
		||||
	"git.zhangdeman.cn/zhangdeman/wrapper/define"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// ToBaseTypeSlice 基础数据类型的列表
 | 
			
		||||
// splitChar 没有用字符串表示的原因: 存在场景, 使用空字符串风格字符串, 空字符串是有意义的
 | 
			
		||||
func ToBaseTypeSlice[BaseTpe op_type.BaseType](str string, splitChar ...string) define.BaseValueSliceResult[BaseTpe] {
 | 
			
		||||
	var (
 | 
			
		||||
		err        error
 | 
			
		||||
		sliceValue []BaseTpe
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	if len(splitChar) == 0 {
 | 
			
		||||
		// 序列化数组直接转换
 | 
			
		||||
		if err = serialize.JSON.UnmarshalWithNumberForString(str, &sliceValue); nil != err {
 | 
			
		||||
			return define.BaseValueSliceResult[BaseTpe]{Value: []BaseTpe{}, Err: err}
 | 
			
		||||
		}
 | 
			
		||||
		return define.BaseValueSliceResult[BaseTpe]{Value: sliceValue, Err: nil}
 | 
			
		||||
	}
 | 
			
		||||
	// 按照分隔符转换
 | 
			
		||||
	strArr := strings.Split(str, splitChar[0])
 | 
			
		||||
	for _, v := range strArr {
 | 
			
		||||
		itemConvertRes := ToBaseValue[BaseTpe](v)
 | 
			
		||||
		if nil != itemConvertRes.Err {
 | 
			
		||||
			return define.BaseValueSliceResult[BaseTpe]{Value: []BaseTpe{}, Err: err}
 | 
			
		||||
		}
 | 
			
		||||
		sliceValue = append(sliceValue, itemConvertRes.Value)
 | 
			
		||||
	}
 | 
			
		||||
	return define.BaseValueSliceResult[BaseTpe]{Value: sliceValue, Err: nil}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								op_string/slice_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								op_string/slice_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,45 @@
 | 
			
		||||
// Package op_string ...
 | 
			
		||||
//
 | 
			
		||||
// Description : op_string ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 2025-10-13 14:36
 | 
			
		||||
package op_string
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	. "github.com/smartystreets/goconvey/convey"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestToBaseTypeSlice(t *testing.T) {
 | 
			
		||||
	Convey("序列化数据转数组成功", t, func() {
 | 
			
		||||
		testData := `[1,2,3,4,5]`
 | 
			
		||||
		res := ToBaseTypeSlice[uint](testData)
 | 
			
		||||
		So(res.Value, ShouldNotBeNil)
 | 
			
		||||
		So(res.Err, ShouldBeNil)
 | 
			
		||||
		So(len(res.Value), ShouldEqual, 5)
 | 
			
		||||
	})
 | 
			
		||||
	Convey("序列化数据转数组失败", t, func() {
 | 
			
		||||
		testData := `[1,2,3,4,-5]`
 | 
			
		||||
		res := ToBaseTypeSlice[uint](testData)
 | 
			
		||||
		So(res.Value, ShouldNotBeNil)
 | 
			
		||||
		So(len(res.Value), ShouldEqual, 0)
 | 
			
		||||
		So(res.Err, ShouldNotBeNil)
 | 
			
		||||
	})
 | 
			
		||||
	Convey("字符串转数组成功", t, func() {
 | 
			
		||||
		testData := `1,2,3,4,5`
 | 
			
		||||
		res := ToBaseTypeSlice[uint](testData, ",")
 | 
			
		||||
		So(res.Value, ShouldNotBeNil)
 | 
			
		||||
		So(res.Err, ShouldBeNil)
 | 
			
		||||
		So(len(res.Value), ShouldEqual, 5)
 | 
			
		||||
	})
 | 
			
		||||
	Convey("字符串转数组失败", t, func() {
 | 
			
		||||
		testData := `1,2,3,4,-5`
 | 
			
		||||
		res := ToBaseTypeSlice[uint](testData)
 | 
			
		||||
		So(res.Value, ShouldNotBeNil)
 | 
			
		||||
		So(len(res.Value), ShouldEqual, 0)
 | 
			
		||||
		So(res.Err, ShouldNotBeNil)
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user