package helper
|
|
|
|
import (
|
|
"errors"
|
|
)
|
|
|
|
func MergeStringMap(x, y map[string]string) map[string]string {
|
|
|
|
n := make(map[string]string)
|
|
for i, v := range x {
|
|
for j, w := range y {
|
|
if i == j {
|
|
n[i] = w
|
|
|
|
} else {
|
|
if _, ok := n[i]; !ok {
|
|
n[i] = v
|
|
}
|
|
if _, ok := n[j]; !ok {
|
|
n[j] = w
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
/**
|
|
* 转换为map string数组
|
|
* 2021/1/5
|
|
* gz
|
|
*/
|
|
func ToMapStringArray(x interface{}) ([]map[string]string, error) {
|
|
list, ok := x.([]interface{})
|
|
if !ok {
|
|
return []map[string]string{}, errors.New("type error")
|
|
}
|
|
|
|
if len(list) < 1 {
|
|
return []map[string]string{}, nil
|
|
}
|
|
var ret []map[string]string = make([]map[string]string, len(list))
|
|
var tmp map[string]interface{}
|
|
var map_tmp map[string]string
|
|
var err error
|
|
|
|
for key, item := range list {
|
|
if tmp, ok = item.(map[string]interface{}); ok {
|
|
map_tmp = make(map[string]string)
|
|
for k, v := range tmp {
|
|
map_tmp[k] = ToStr(v)
|
|
}
|
|
ret[key] = map_tmp
|
|
} else {
|
|
err = errors.New("data type error")
|
|
break
|
|
}
|
|
}
|
|
|
|
return ret, err
|
|
}
|
|
|
|
/**
|
|
* 将map[string]string数组转成map[string]interface数组
|
|
*/
|
|
func MapStringToInterfaceArr(src []map[string]string)([]map[string]interface{}){
|
|
var dest []map[string]interface{}
|
|
|
|
for i := range src {
|
|
m := src[i]
|
|
n := make(map[string]interface{})
|
|
for k,v := range m {
|
|
n[k] = interface{}(v)
|
|
}
|
|
dest = append(dest, n)
|
|
}
|
|
|
|
return dest
|
|
}
|
|
|
|
/**
|
|
* 将map[string]string转成map[string]interface
|
|
*/
|
|
func MapStringToInterface(src map[string]string)(map[string]interface{}){
|
|
target := make(map[string]interface{})
|
|
for k,v := range src {
|
|
target[k] = interface{}(v)
|
|
}
|
|
|
|
return target
|
|
}
|