change easymap -> map

This commit is contained in:
白茶清欢 2024-11-21 14:22:09 +08:00
parent ddeb444afb
commit a56574ff65

View File

@ -10,7 +10,6 @@ package wrapper
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"git.zhangdeman.cn/zhangdeman/easymap"
"git.zhangdeman.cn/zhangdeman/serialize" "git.zhangdeman.cn/zhangdeman/serialize"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"reflect" "reflect"
@ -21,7 +20,7 @@ import (
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:02 2023/8/10 // Date : 15:02 2023/8/10
func EasyMap(mapData any) easymap.EasyMap { func EasyMap(mapData any) Map {
m, _ := EasyMapWithError(mapData) m, _ := EasyMapWithError(mapData)
return m return m
} }
@ -31,24 +30,23 @@ func EasyMap(mapData any) easymap.EasyMap {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:06 2023/8/10 // Date : 15:06 2023/8/10
func EasyMapWithError(mapData any) (easymap.EasyMap, error) { func EasyMapWithError(mapData any) (Map, error) {
if nil == mapData { if nil == mapData {
return easymap.NewNormal(), nil return map[string]any{}, nil
} }
m := easymap.NewNormal()
reflectType := reflect.TypeOf(mapData) reflectType := reflect.TypeOf(mapData)
if reflectType.Kind() != reflect.Map { if reflectType.Kind() != reflect.Map {
mapFormatData := make(map[string]any) mapFormatData := make(map[string]any)
if err := serialize.JSON.UnmarshalWithNumber(serialize.JSON.MarshalForByteIgnoreError(mapData), &mapFormatData); nil != err { if err := serialize.JSON.UnmarshalWithNumber(serialize.JSON.MarshalForByteIgnoreError(mapData), &mapFormatData); nil != err {
return m, errors.New("input data type is " + reflectType.String() + ", can not convert to map") return mapFormatData, errors.New("input data type is " + reflectType.String() + ", can not convert to map")
} }
mapData = mapFormatData return mapFormatData, nil
} }
m := Map(map[string]any{})
reflectValue := reflect.ValueOf(mapData).MapRange() reflectValue := reflect.ValueOf(mapData).MapRange()
for reflectValue.Next() { for reflectValue.Next() {
// 循环提取相关值 // 循环提取相关值
m.Set(reflectValue.Key().Interface(), reflectValue.Value().Interface()) _ = m.Set(reflectValue.Key().String(), reflectValue.Value().Interface())
} }
return m, nil return m, nil
} }
@ -58,7 +56,7 @@ func EasyMapWithError(mapData any) (easymap.EasyMap, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:11 2023/8/10 // Date : 16:11 2023/8/10
func EasyMapFromStruct(data any) easymap.EasyMap { func EasyMapFromStruct(data any) Map {
byteData, _ := json.Marshal(data) byteData, _ := json.Marshal(data)
return EasyMapFromByte(byteData) return EasyMapFromByte(byteData)
} }
@ -68,7 +66,7 @@ func EasyMapFromStruct(data any) easymap.EasyMap {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:12 2023/8/10 // Date : 16:12 2023/8/10
func EasyMapFromString(data string) easymap.EasyMap { func EasyMapFromString(data string) Map {
return EasyMapFromByte([]byte(data)) return EasyMapFromByte([]byte(data))
} }
@ -77,11 +75,11 @@ func EasyMapFromString(data string) easymap.EasyMap {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:12 2023/8/10 // Date : 16:12 2023/8/10
func EasyMapFromByte(data []byte) easymap.EasyMap { func EasyMapFromByte(data []byte) Map {
res := easymap.NewNormal() res := Map(map[string]any{})
jsonRes := gjson.Parse(string(data)) jsonRes := gjson.Parse(string(data))
jsonRes.ForEach(func(key, value gjson.Result) bool { jsonRes.ForEach(func(key, value gjson.Result) bool {
res.Set(key.Value(), value.Value()) _ = res.Set(key.String(), value.Value())
return true return true
}) })
return res return res