From 9fcae03b020b248bf44088e1775c764226c652c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 16 Nov 2021 17:08:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=89=E5=85=A8=E7=9A=84?= =?UTF-8?q?=E4=BB=8Emap=E4=B8=AD=E8=AF=BB=E5=8F=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=9A=84=E5=90=84=E7=A7=8D=E6=96=B9=E6=B3=95=EF=BC=8C=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/map.go | 209 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 util/map.go diff --git a/util/map.go b/util/map.go new file mode 100644 index 0000000..88cd1fc --- /dev/null +++ b/util/map.go @@ -0,0 +1,209 @@ +// Package util ... +// +// Description : 从map中读取数据 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-11-16 4:28 下午 +package util + +import ( + "git.zhangdeman.cn/zhangdeman/gopkg/convert" + "github.com/pkg/errors" +) + +// Exist 检测一个key在map中是否存在 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 4:52 下午 2021/11/16 +func Exist(source map[interface{}]interface{}, key interface{}) bool { + if nil == source { + return false + } + if _, exist := source[key]; !exist { + return false + } + return true +} + +// GetInt 获取int值 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 4:30 下午 2021/11/16 +func GetInt(source map[interface{}]interface{}, key interface{}, defaultVal int) int { + var result int + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetInt8 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 4:59 下午 2021/11/16 +func GetInt8(source map[interface{}]interface{}, key interface{}, defaultVal int8) int8 { + var result int8 + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetInt16 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 4:59 下午 2021/11/16 +func GetInt16(source map[interface{}]interface{}, key interface{}, defaultVal int16) int16 { + var result int16 + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetInt32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:00 下午 2021/11/16 +func GetInt32(source map[interface{}]interface{}, key interface{}, defaultVal int32) int32 { + var result int32 + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetInt64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:00 下午 2021/11/16 +func GetInt64(source map[interface{}]interface{}, key interface{}, defaultVal int64) int64 { + var result int64 + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetUint ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:01 下午 2021/11/16 +func GetUint(source map[interface{}]interface{}, key interface{}, defaultVal uint) uint { + var result uint + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetUint8 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:01 下午 2021/11/16 +func GetUint8(source map[interface{}]interface{}, key interface{}, defaultVal uint8) uint8 { + var result uint8 + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetUint16 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:02 下午 2021/11/16 +func GetUint16(source map[interface{}]interface{}, key interface{}, defaultVal uint16) uint16 { + var result uint16 + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetUint32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:02 下午 2021/11/16 +func GetUint32(source map[interface{}]interface{}, key interface{}, defaultVal uint32) uint32 { + var result uint32 + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetUint64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:03 下午 2021/11/16 +func GetUint64(source map[interface{}]interface{}, key interface{}, defaultVal uint64) uint64 { + var result uint64 + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetFloat32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:03 下午 2021/11/16 +func GetFloat32(source map[interface{}]interface{}, key interface{}, defaultVal float32) float32 { + var result float32 + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetFloat64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:04 下午 2021/11/16 +func GetFloat64(source map[interface{}]interface{}, key interface{}, defaultVal float64) float64 { + var result float64 + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetString ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:07 下午 2021/11/16 +func GetString(source map[interface{}]interface{}, key interface{}, defaultVal string) string { + var result string + if err := GetDataWithReceiver(source, key, &result); nil != err { + return defaultVal + } + return result +} + +// GetDataWithReceiver 使用制定的数据指针接受结果 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 4:54 下午 2021/11/16 +func GetDataWithReceiver(source map[interface{}]interface{}, key interface{}, receiver interface{}) error { + if !Exist(source, key) { + return errors.New("key is not found") + } + return convert.ConvertAssign(receiver, source[key]) +}