feat: 增加map类型转换
This commit is contained in:
@ -20,3 +20,9 @@ type BaseValuePtrResult[BaseType op_type.BaseType] struct {
|
||||
Value *BaseType `json:"result"` // 转换结果
|
||||
Err error `json:"err"` // 错误信息
|
||||
}
|
||||
|
||||
// MapValueResult map类型转换结果
|
||||
type MapValueResult[Key comparable, Value comparable] struct {
|
||||
Value map[Key]Value `json:"result"` // 转换结果
|
||||
Err error `json:"err"` // 错误信息
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@ -4,8 +4,8 @@ go 1.24.0
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20251013024601-da007da2fb42
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
|
||||
github.com/smartystreets/goconvey v1.8.1
|
||||
|
4
go.sum
4
go.sum
@ -2,8 +2,12 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772 h1:Yo1ur3
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y=
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20251013024601-da007da2fb42 h1:VjYrb4adud7FHeiYS9XA0B/tOaJjfRejzQAlwimrrDc=
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20251013024601-da007da2fb42/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9 h1:/GLQaFoLb+ciHOtAS2BIyPNnf4O5ME3AC5PUaJY9kfs=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9/go.mod h1:ABJ655C5QenQNOzf7LjCe4sSB52CXvaWLX2Zg4uwDJY=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd h1:kTZOpR8iHx27sUufMWVYhDZx9Q4h80j7RWlaR8GIBiU=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd/go.mod h1:pLrQ63JICi81/3w2BrD26QZiu+IpddvEVfMJ6No3Xb4=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
||||
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
||||
|
31
op_string/map.go
Normal file
31
op_string/map.go
Normal file
@ -0,0 +1,31 @@
|
||||
// Package op_string ...
|
||||
//
|
||||
// Description : op_string ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-10-13 12:18
|
||||
package op_string
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper/define"
|
||||
)
|
||||
|
||||
// ToMap 转换为map, 数据类型可比较, 即可作为 map key, 内置 any 类型无法作为key
|
||||
func ToMap[Key comparable, Value comparable](str string) define.MapValueResult[Key, Value] {
|
||||
var (
|
||||
err error
|
||||
res map[Key]Value
|
||||
)
|
||||
if err = serialize.JSON.UnmarshalWithNumberForString(str, &res); err != nil {
|
||||
return define.MapValueResult[Key, Value]{
|
||||
Value: nil,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
return define.MapValueResult[Key, Value]{
|
||||
Value: res,
|
||||
Err: nil,
|
||||
}
|
||||
}
|
30
op_string/map_test.go
Normal file
30
op_string/map_test.go
Normal file
@ -0,0 +1,30 @@
|
||||
// Package op_string ...
|
||||
//
|
||||
// Description : op_string ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-10-13 12:21
|
||||
package op_string
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"encoding/json"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestToMap(t *testing.T) {
|
||||
Convey("map[string]any转换成", t, func() {
|
||||
testData := `{
|
||||
"name": "baicha",
|
||||
"age": 18
|
||||
}`
|
||||
res := ToMap[string, any](testData)
|
||||
So(res.Err, ShouldBeNil)
|
||||
So(res.Value, ShouldNotBeNil)
|
||||
So(res.Value["name"], ShouldEqual, "baicha")
|
||||
So(res.Value["age"], ShouldEqual, json.Number("18"))
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user