You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.4 KiB
75 lines
1.4 KiB
package transform
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Obj2mapStr
|
|
// 将map[string]any 转为字符串 a="123",b=234 这种格式
|
|
func Obj2mapStr(dataMap map[string]any) string {
|
|
ItemCount := 0
|
|
fields := strings.Builder{}
|
|
for k, v := range dataMap {
|
|
sValue := ""
|
|
switch v.(type) {
|
|
case []any:
|
|
sValue = strings.ReplaceAll(fmt.Sprintf(`%s="%v"`, k, v), " ", ",")
|
|
case string:
|
|
sValue = fmt.Sprintf(`%s="%v"`, k, v)
|
|
case float64, float32:
|
|
sValue = fmt.Sprintf(`%s=%.5f`, k, v)
|
|
case int64, int32:
|
|
sValue = fmt.Sprintf(`%s=%d`, k, v)
|
|
default:
|
|
mJson, _ := json.Marshal(dataMap)
|
|
log.Printf("数据类型异常[k=%v,v=%v] 原始dataMap=%v", k, v, mJson)
|
|
}
|
|
if len(sValue) > 0 {
|
|
fields.WriteString(sValue)
|
|
}
|
|
ItemCount += 1
|
|
if ItemCount < len(dataMap) {
|
|
fields.WriteString(",")
|
|
}
|
|
|
|
}
|
|
return fields.String()
|
|
}
|
|
|
|
func Numerical(raw map[string]any) map[string]any {
|
|
result := map[string]any{}
|
|
for k, v := range raw {
|
|
switch k {
|
|
case "humanid", "idcard_number":
|
|
result[k] = v
|
|
default:
|
|
result[k] = numerical(v)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func numerical(d any) any {
|
|
var newV any
|
|
switch d.(type) {
|
|
case int:
|
|
if v, err := strconv.Atoi(d.(string)); err == nil {
|
|
newV = v
|
|
}
|
|
case string:
|
|
if v, err := strconv.ParseFloat(d.(string), 64); err == nil {
|
|
newV = v
|
|
}
|
|
default:
|
|
newV = d
|
|
}
|
|
|
|
if newV == nil {
|
|
newV = d
|
|
}
|
|
return newV
|
|
}
|
|
|