代码拉取完成,页面将自动刷新
// 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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。