wrapper/map.go

52 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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