2022-05-14 13:45:51 +08:00
|
|
|
// Package util ...
|
|
|
|
//
|
|
|
|
// Description : 从map中读取数据
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2021-11-16 4:28 下午
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2022-05-14 15:56:09 +08:00
|
|
|
// ownMap ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:53 2022/5/14
|
|
|
|
type ownMap struct {
|
|
|
|
}
|
|
|
|
|
2022-05-14 13:45:51 +08:00
|
|
|
// Exist 检测一个key在map中是否存在
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 4:52 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) Exist(source map[interface{}]interface{}, key interface{}) bool {
|
2022-05-14 13:45:51 +08:00
|
|
|
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
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetInt(source map[interface{}]interface{}, key interface{}, defaultVal int) int {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result int
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInt8 ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 4:59 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetInt8(source map[interface{}]interface{}, key interface{}, defaultVal int8) int8 {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result int8
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInt16 ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 4:59 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetInt16(source map[interface{}]interface{}, key interface{}, defaultVal int16) int16 {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result int16
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInt32 ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:00 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetInt32(source map[interface{}]interface{}, key interface{}, defaultVal int32) int32 {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result int32
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInt64 ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:00 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetInt64(source map[interface{}]interface{}, key interface{}, defaultVal int64) int64 {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result int64
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUint ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:01 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetUint(source map[interface{}]interface{}, key interface{}, defaultVal uint) uint {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result uint
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUint8 ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:01 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetUint8(source map[interface{}]interface{}, key interface{}, defaultVal uint8) uint8 {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result uint8
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUint16 ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:02 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetUint16(source map[interface{}]interface{}, key interface{}, defaultVal uint16) uint16 {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result uint16
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUint32 ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:02 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetUint32(source map[interface{}]interface{}, key interface{}, defaultVal uint32) uint32 {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result uint32
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUint64 ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:03 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetUint64(source map[interface{}]interface{}, key interface{}, defaultVal uint64) uint64 {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result uint64
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFloat32 ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:03 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetFloat32(source map[interface{}]interface{}, key interface{}, defaultVal float32) float32 {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result float32
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFloat64 ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:04 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetFloat64(source map[interface{}]interface{}, key interface{}, defaultVal float64) float64 {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result float64
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetString ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:07 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetString(source map[interface{}]interface{}, key interface{}, defaultVal string) string {
|
2022-05-14 13:45:51 +08:00
|
|
|
var result string
|
2022-05-14 15:56:09 +08:00
|
|
|
if err := om.GetDataWithReceiver(source, key, &result); nil != err {
|
2022-05-14 13:45:51 +08:00
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDataWithReceiver 使用制定的数据指针接受结果
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 4:54 下午 2021/11/16
|
2022-05-14 15:56:09 +08:00
|
|
|
func (om *ownMap) GetDataWithReceiver(source map[interface{}]interface{}, key interface{}, receiver interface{}) error {
|
|
|
|
if !om.Exist(source, key) {
|
2022-05-14 13:45:51 +08:00
|
|
|
return errors.New("key is not found")
|
|
|
|
}
|
2022-05-14 14:01:21 +08:00
|
|
|
return ConvertAssign(receiver, source[key])
|
2022-05-14 13:45:51 +08:00
|
|
|
}
|
2023-07-22 21:59:15 +08:00
|
|
|
|
|
|
|
// ToStruct map转结构体
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:48 2023/7/22
|
|
|
|
func (om *ownMap) ToStruct(source map[string]interface{}, receiver interface{}) error {
|
|
|
|
if nil == source {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
byteData := JSON.MarshalForByte(source)
|
|
|
|
return JSON.UnmarshalWithNumber(byteData, receiver)
|
|
|
|
}
|