1 Star 0 Fork 0

非空非零 / jupiter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
util.go 2.55 KB
一键复制 编辑 原始数据 按行查看 历史
renzhentao 提交于 2021-06-21 15:02 . 1
// Copyright 2020 Douyu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package xmap
import (
"fmt"
"reflect"
"strings"
"gitee.com/nonull/eden/pkg/util/xcast"
)
// MergeStringMap merge two map
func MergeStringMap(dest, src map[string]interface{}) {
for sk, sv := range src {
tv, ok := dest[sk]
if !ok {
// val不存在时,直接赋值
dest[sk] = sv
continue
}
svType := reflect.TypeOf(sv)
tvType := reflect.TypeOf(tv)
if svType != tvType {
fmt.Println("continue, type is different")
continue
}
switch ttv := tv.(type) {
case map[interface{}]interface{}:
tsv := sv.(map[interface{}]interface{})
ssv := ToMapStringInterface(tsv)
stv := ToMapStringInterface(ttv)
MergeStringMap(stv, ssv)
dest[sk] = stv
case map[string]interface{}:
MergeStringMap(ttv, sv.(map[string]interface{}))
dest[sk] = ttv
default:
dest[sk] = sv
}
}
}
// ToMapStringInterface cast map[interface{}]interface{} to map[string]interface{}
func ToMapStringInterface(src map[interface{}]interface{}) map[string]interface{} {
tgt := map[string]interface{}{}
for k, v := range src {
tgt[fmt.Sprintf("%v", k)] = v
}
return tgt
}
// InsensitiviseMap insensitivise map
func InsensitiviseMap(m map[string]interface{}) {
for key, val := range m {
switch val.(type) {
case map[interface{}]interface{}:
InsensitiviseMap(xcast.ToStringMap(val))
case map[string]interface{}:
InsensitiviseMap(val.(map[string]interface{}))
}
lower := strings.ToLower(key)
if key != lower {
delete(m, key)
}
m[lower] = val
}
}
// DeepSearchInMap deep search in map
func DeepSearchInMap(m map[string]interface{}, paths ...string) map[string]interface{} {
//深度拷贝
mtmp := make(map[string]interface{})
for k, v := range m {
mtmp[k] = v
}
for _, k := range paths {
m2, ok := mtmp[k]
if !ok {
m3 := make(map[string]interface{})
mtmp[k] = m3
mtmp = m3
continue
}
m3, err := xcast.ToStringMapE(m2)
if err != nil {
m3 = make(map[string]interface{})
mtmp[k] = m3
}
// continue search
mtmp = m3
}
return mtmp
}
1
https://gitee.com/nonull/eden.git
git@gitee.com:nonull/eden.git
nonull
eden
jupiter
v0.3.1

搜索帮助