feat: str转struct

This commit is contained in:
2025-10-13 14:20:40 +08:00
parent 2100caa5e4
commit efab8cb6d2
3 changed files with 45 additions and 2 deletions

View File

@ -13,7 +13,7 @@ import (
)
// ToMap 转换为map, 数据类型可比较, 即可作为 map key, 内置 any 类型无法作为key
func ToMap[Key comparable, Value comparable](str string) define.MapValueResult[Key, Value] {
func ToMap[Key comparable, Value any](str string) define.MapValueResult[Key, Value] {
var (
err error
res map[Key]Value
@ -29,3 +29,20 @@ func ToMap[Key comparable, Value comparable](str string) define.MapValueResult[K
Err: nil,
}
}
// ToStruct 转换为结构体
func ToStruct[Value any](str string, receiver Value) define.StructValueResult[Value] {
var (
err error
)
if err = serialize.JSON.UnmarshalWithNumberForString(str, receiver); err != nil {
return define.StructValueResult[Value]{
Value: receiver,
Err: err,
}
}
return define.StructValueResult[Value]{
Value: receiver,
Err: nil,
}
}