From 2100caa5e48984638784c8018413f518d60ac779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 13 Oct 2025 12:52:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0map=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/result.go | 6 ++++++ go.mod | 4 ++-- go.sum | 4 ++++ op_string/map.go | 31 +++++++++++++++++++++++++++++++ op_string/map_test.go | 30 ++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 op_string/map.go create mode 100644 op_string/map_test.go diff --git a/define/result.go b/define/result.go index 0b38608..b1d176c 100644 --- a/define/result.go +++ b/define/result.go @@ -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"` // 错误信息 +} diff --git a/go.mod b/go.mod index de8c903..2f5b853 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index e4de9ae..dfea902 100644 --- a/go.sum +++ b/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= diff --git a/op_string/map.go b/op_string/map.go new file mode 100644 index 0000000..0c00720 --- /dev/null +++ b/op_string/map.go @@ -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, + } +} diff --git a/op_string/map_test.go b/op_string/map_test.go new file mode 100644 index 0000000..9dcef82 --- /dev/null +++ b/op_string/map_test.go @@ -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")) + }) +}