mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-25 14:38:34 +08:00
【升级go 1.18】
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func ExportExcel(head []string, datas [][]interface{}, filePath string) error {
|
||||
func ExportExcel(head []string, datas [][]any, filePath string) error {
|
||||
excel := excelize.NewFile()
|
||||
excel.SetSheetRow("Sheet1", "A1", &head)
|
||||
for i, data := range datas {
|
||||
@@ -23,9 +23,9 @@ func GetFileName(path, model string) string {
|
||||
return path + fmt.Sprintf("%s_%s.xlsx", model, kgo.KTime.Date("20060102150405", time.Now()))
|
||||
}
|
||||
|
||||
func InterfaceToExcel(data interface{}, fileName string) {
|
||||
func InterfaceToExcel(data any, fileName string) {
|
||||
heads := make([]string, 0)
|
||||
datas := make([][]interface{}, 0)
|
||||
datas := make([][]any, 0)
|
||||
v := reflect.ValueOf(data)
|
||||
max := int64(v.Len())
|
||||
for i := 0; i < int(max); i++ {
|
||||
@@ -38,7 +38,7 @@ func InterfaceToExcel(data interface{}, fileName string) {
|
||||
heads = append(heads, key.Field(i).Name)
|
||||
}
|
||||
}
|
||||
field := make([]interface{}, 0)
|
||||
field := make([]any, 0)
|
||||
for i := 0; i < num; i++ {
|
||||
field = append(field, val.Field(i).Interface())
|
||||
}
|
||||
|
||||
@@ -10,12 +10,13 @@ const ipurl = "http://whois.pconline.com.cn/ipJson.jsp"
|
||||
|
||||
const UNKNOWN = "XX XX"
|
||||
|
||||
//获取真实地址
|
||||
//GetRealAddressByIP 获取真实地址
|
||||
func GetRealAddressByIP(ip string) string {
|
||||
if ip == "127.0.0.1" || ip == "localhost" {
|
||||
return "内部IP"
|
||||
}
|
||||
url := fmt.Sprintf("%s?ip=%s&json=true", ipurl, ip)
|
||||
|
||||
res := httpclient.NewRequest(url).Get()
|
||||
if res == nil || res.StatusCode != 200 {
|
||||
return UNKNOWN
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func Json2Map(jsonStr string) map[string]interface{} {
|
||||
var res map[string]interface{}
|
||||
func Json2Map(jsonStr string) map[string]any {
|
||||
var res map[string]any
|
||||
if jsonStr == "" {
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ func Contains(haystack, needle string, startOffset ...int) int {
|
||||
}
|
||||
|
||||
// 字符串模板解析
|
||||
func TemplateResolve(temp string, data interface{}) string {
|
||||
func TemplateResolve(temp string, data any) string {
|
||||
t, _ := template.New("string-temp").Parse(temp)
|
||||
var tmplBytes bytes.Buffer
|
||||
|
||||
@@ -62,7 +62,7 @@ func TemplateResolve(temp string, data interface{}) string {
|
||||
return tmplBytes.String()
|
||||
}
|
||||
|
||||
func ReverStrTemplate(temp, str string, res map[string]interface{}) {
|
||||
func ReverStrTemplate(temp, str string, res map[string]any) {
|
||||
index := UnicodeIndex(temp, "{")
|
||||
ei := UnicodeIndex(temp, "}") + 1
|
||||
next := kgo.KStr.Trim(temp[ei:], " ")
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
// Copy copy things,引用至copier
|
||||
func Copy(toValue interface{}, fromValue interface{}) (err error) {
|
||||
func Copy(toValue any, fromValue any) (err error) {
|
||||
var (
|
||||
isSlice bool
|
||||
amount = 1
|
||||
@@ -133,7 +133,7 @@ func Copy(toValue interface{}, fromValue interface{}) (err error) {
|
||||
}
|
||||
|
||||
// 对结构体的每个字段以及字段值执行doWith回调函数, 包括匿名属性的字段
|
||||
func DoWithFields(str interface{}, doWith func(fType reflect.StructField, fValue reflect.Value) error) error {
|
||||
func DoWithFields(str any, doWith func(fType reflect.StructField, fValue reflect.Value) error) error {
|
||||
t := IndirectType(reflect.TypeOf(str))
|
||||
if t.Kind() != reflect.Struct {
|
||||
return errors.New("非结构体")
|
||||
@@ -217,7 +217,7 @@ func set(to, from reflect.Value) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func Map2Struct(m map[string]interface{}, s interface{}) error {
|
||||
func Map2Struct(m map[string]any, s any) error {
|
||||
toValue := Indirect(reflect.ValueOf(s))
|
||||
if !toValue.CanAddr() {
|
||||
return errors.New("to value is unaddressable")
|
||||
@@ -275,7 +275,7 @@ func Map2Struct(m map[string]interface{}, s interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Maps2Structs(maps []map[string]interface{}, structs interface{}) error {
|
||||
func Maps2Structs(maps []map[string]any, structs any) error {
|
||||
structsV := reflect.Indirect(reflect.ValueOf(structs))
|
||||
valType := structsV.Type()
|
||||
valElemType := valType.Elem()
|
||||
@@ -314,8 +314,8 @@ func getFiledValueByPath(path string, value reflect.Value) reflect.Value {
|
||||
return value
|
||||
}
|
||||
|
||||
func getInnerStructMaps(m map[string]interface{}) map[string]map[string]interface{} {
|
||||
key2map := make(map[string]map[string]interface{})
|
||||
func getInnerStructMaps(m map[string]any) map[string]map[string]any {
|
||||
key2map := make(map[string]map[string]any)
|
||||
for k, v := range m {
|
||||
if !strings.Contains(k, ".") {
|
||||
continue
|
||||
@@ -324,7 +324,7 @@ func getInnerStructMaps(m map[string]interface{}) map[string]map[string]interfac
|
||||
prefix := k[0:lastIndex]
|
||||
m2 := key2map[prefix]
|
||||
if m2 == nil {
|
||||
key2map[prefix] = map[string]interface{}{k[lastIndex+1:]: v}
|
||||
key2map[prefix] = map[string]any{k[lastIndex+1:]: v}
|
||||
} else {
|
||||
m2[k[lastIndex+1:]] = v
|
||||
}
|
||||
@@ -335,7 +335,7 @@ func getInnerStructMaps(m map[string]interface{}) map[string]map[string]interfac
|
||||
|
||||
// decode等方法摘抄自mapstructure库
|
||||
|
||||
func decode(name string, input interface{}, outVal reflect.Value) error {
|
||||
func decode(name string, input any, outVal reflect.Value) error {
|
||||
var inputVal reflect.Value
|
||||
if input != nil {
|
||||
inputVal = reflect.ValueOf(input)
|
||||
@@ -374,7 +374,7 @@ func decode(name string, input interface{}, outVal reflect.Value) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func decodeInt(name string, data interface{}, val reflect.Value) error {
|
||||
func decodeInt(name string, data any, val reflect.Value) error {
|
||||
dataVal := reflect.Indirect(reflect.ValueOf(data))
|
||||
dataKind := getKind(dataVal)
|
||||
dataType := dataVal.Type()
|
||||
@@ -416,7 +416,7 @@ func decodeInt(name string, data interface{}, val reflect.Value) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeUint(name string, data interface{}, val reflect.Value) error {
|
||||
func decodeUint(name string, data any, val reflect.Value) error {
|
||||
dataVal := reflect.Indirect(reflect.ValueOf(data))
|
||||
dataKind := getKind(dataVal)
|
||||
dataType := dataVal.Type()
|
||||
@@ -472,7 +472,7 @@ func decodeUint(name string, data interface{}, val reflect.Value) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeFloat(name string, data interface{}, val reflect.Value) error {
|
||||
func decodeFloat(name string, data any, val reflect.Value) error {
|
||||
dataVal := reflect.Indirect(reflect.ValueOf(data))
|
||||
dataKind := getKind(dataVal)
|
||||
dataType := dataVal.Type()
|
||||
@@ -514,7 +514,7 @@ func decodeFloat(name string, data interface{}, val reflect.Value) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeString(name string, data interface{}, val reflect.Value) error {
|
||||
func decodeString(name string, data any, val reflect.Value) error {
|
||||
dataVal := reflect.Indirect(reflect.ValueOf(data))
|
||||
dataKind := getKind(dataVal)
|
||||
|
||||
@@ -566,7 +566,7 @@ func decodeString(name string, data interface{}, val reflect.Value) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodePtr(name string, data interface{}, val reflect.Value) (bool, error) {
|
||||
func decodePtr(name string, data any, val reflect.Value) (bool, error) {
|
||||
// If the input data is nil, then we want to just set the output
|
||||
// pointer to be nil as well.
|
||||
isNil := data == nil
|
||||
|
||||
@@ -55,7 +55,7 @@ func TestGetFieldNames(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMaps2Structs(t *testing.T) {
|
||||
mapInstance := make(map[string]interface{})
|
||||
mapInstance := make(map[string]any)
|
||||
mapInstance["Username"] = "liang637210"
|
||||
mapInstance["Id"] = 28
|
||||
mapInstance["CreateTime"] = time.Now()
|
||||
@@ -66,7 +66,7 @@ func TestMaps2Structs(t *testing.T) {
|
||||
mapInstance["Inner.Dest.Username"] = "inner dest uername"
|
||||
mapInstance["Inner.Dest.Inner.Desc"] = "inner dest inner desc"
|
||||
|
||||
mapInstance2 := make(map[string]interface{})
|
||||
mapInstance2 := make(map[string]any)
|
||||
mapInstance2["Username"] = "liang6372102"
|
||||
mapInstance2["Id"] = 282
|
||||
mapInstance2["CreateTime"] = time.Now()
|
||||
@@ -77,7 +77,7 @@ func TestMaps2Structs(t *testing.T) {
|
||||
mapInstance2["Inner.Dest.Username"] = "inner dest uername2"
|
||||
mapInstance2["Inner.Dest.Inner.Desc"] = "inner dest inner desc2"
|
||||
|
||||
maps := make([]map[string]interface{}, 2)
|
||||
maps := make([]map[string]any, 2)
|
||||
maps[0] = mapInstance
|
||||
maps[1] = mapInstance2
|
||||
res := new([]Src)
|
||||
@@ -88,7 +88,7 @@ func TestMaps2Structs(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMap2Struct(t *testing.T) {
|
||||
mapInstance := make(map[string]interface{})
|
||||
mapInstance := make(map[string]any)
|
||||
mapInstance["Username"] = "liang637210"
|
||||
mapInstance["Id"] = 12
|
||||
mapInstance["CreateTime"] = time.Now()
|
||||
@@ -149,8 +149,8 @@ func TestMap2Struct(t *testing.T) {
|
||||
fmt.Printf("map2struct后得到的 struct 内容为:%v", s)
|
||||
}
|
||||
|
||||
func getPrefixKeyMap(m map[string]interface{}) map[string]map[string]interface{} {
|
||||
key2map := make(map[string]map[string]interface{})
|
||||
func getPrefixKeyMap(m map[string]any) map[string]map[string]any {
|
||||
key2map := make(map[string]map[string]any)
|
||||
for k, v := range m {
|
||||
if !strings.Contains(k, ".") {
|
||||
continue
|
||||
@@ -159,7 +159,7 @@ func getPrefixKeyMap(m map[string]interface{}) map[string]map[string]interface{}
|
||||
prefix := k[0:lastIndex]
|
||||
m2 := key2map[prefix]
|
||||
if m2 == nil {
|
||||
key2map[prefix] = map[string]interface{}{k[lastIndex+1:]: v}
|
||||
key2map[prefix] = map[string]any{k[lastIndex+1:]: v}
|
||||
} else {
|
||||
m2[k[lastIndex+1:]] = v
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func parse(t *template.Template, vars interface{}) string {
|
||||
func parse(t *template.Template, vars any) string {
|
||||
var tmplBytes bytes.Buffer
|
||||
|
||||
err := t.Execute(&tmplBytes, vars)
|
||||
@@ -18,7 +18,7 @@ func parse(t *template.Template, vars interface{}) string {
|
||||
// 模板字符串解析
|
||||
// str 模板字符串
|
||||
// vars 参数变量
|
||||
func TemplateParse(str string, vars interface{}) string {
|
||||
func TemplateParse(str string, vars any) string {
|
||||
tmpl, err := template.New("tmpl").Parse(str)
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -9,7 +9,7 @@ type INode interface {
|
||||
// IsRoot 判断当前节点是否是顶层根节点
|
||||
IsRoot() bool
|
||||
|
||||
SetChildren(childern interface{})
|
||||
SetChildren(childern any)
|
||||
}
|
||||
|
||||
type INodes []INode
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
// 从指定路径加载yaml文件
|
||||
func LoadYml(path string, out interface{}) error {
|
||||
func LoadYml(path string, out any) error {
|
||||
yamlFileBytes, readErr := ioutil.ReadFile(path)
|
||||
if readErr != nil {
|
||||
return readErr
|
||||
@@ -21,7 +21,7 @@ func LoadYml(path string, out interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadYmlByString(yamlStr string, out interface{}) error {
|
||||
func LoadYmlByString(yamlStr string, out any) error {
|
||||
// yaml解析
|
||||
return yaml.Unmarshal([]byte(yamlStr), out)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user