From b92b8dbf2585528a4ae071d658445a83d768e704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 10 Aug 2023 15:19:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Map=E7=9A=84=E5=8C=85?= =?UTF-8?q?=E8=A3=85=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 3 ++- go.sum | 5 +++++ map.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 map.go diff --git a/go.mod b/go.mod index 63572eb..4ca0202 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,10 @@ module git.zhangdeman.cn/zhangdeman/wrapper go 1.20 -require git.zhangdeman.cn/zhangdeman/util v0.0.0-20230801092344-773ac512f305 +require git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810063945-842592611562 require ( + git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10 // indirect github.com/BurntSushi/toml v1.3.2 // indirect github.com/Jeffail/gabs v1.4.0 // indirect github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect diff --git a/go.sum b/go.sum index afd96d0..de1d612 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ +git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10 h1:+Lg4vXFEiWVKjhUJdXuoP0AgjGT49oqJ3301STnZErk= +git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10/go.mod h1:+Lc0zYF8sylRi75A7NGmObrLxugwAZa8WVpWh2eh5X0= git.zhangdeman.cn/zhangdeman/util v0.0.0-20230801092344-773ac512f305 h1:6Bs/cQP+eKABHB/01uHQI15PwKbo7n8HNx7nIFUGBp0= git.zhangdeman.cn/zhangdeman/util v0.0.0-20230801092344-773ac512f305/go.mod h1:trYFOShINaQBvinQrH4A0G2kfL22Y2lygEcAiGDt/sc= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810063945-842592611562 h1:wo0r4mexqkPzQ1SZOw5z8A7FJ3ne1G6A/qWR3iaqlhw= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810063945-842592611562/go.mod h1:trYFOShINaQBvinQrH4A0G2kfL22Y2lygEcAiGDt/sc= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo= @@ -7,6 +11,7 @@ github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/ github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU= github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4= github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= diff --git a/map.go b/map.go new file mode 100644 index 0000000..f2695c8 --- /dev/null +++ b/map.go @@ -0,0 +1,51 @@ +// Package wrapper ... +// +// Description : wrapper ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-08-10 15:01 +package wrapper + +import ( + "errors" + "git.zhangdeman.cn/zhangdeman/easymap" + "reflect" +) + +// EasyMap ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:02 2023/8/10 +func EasyMap(mapData interface{}) Map { + m, _ := EasyMapWithError(mapData) + return m +} + +// EasyMapWithError 转换map,并带上转换的异常 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:06 2023/8/10 +func EasyMapWithError(mapData interface{}) (Map, error) { + m := easymap.NewNormal(true) + reflectType := reflect.TypeOf(mapData) + if reflectType.Kind() != reflect.Map { + return m, errors.New("input data type is " + reflectType.String() + ", not map") + } + + reflectValue := reflect.ValueOf(mapData).MapRange() + for reflectValue.Next() { + // 循环提取相关值 + m.Set(reflectValue.Key().Interface(), reflectValue.Value().Interface()) + } + return m, nil +} + +// Map ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:14 2023/8/10 +type Map easymap.EasyMap -- 2.36.6