This commit is contained in:
XM-GO
2023-08-22 15:17:14 +08:00
parent 85f4f328f4
commit 4344771547
143 changed files with 13004 additions and 6957 deletions

View File

@@ -1,6 +1,8 @@
package tool
import (
"log"
"reflect"
"regexp"
"strings"
)
@@ -15,3 +17,66 @@ func ToCamelCase(s string) string {
}
return strings.Join(words, "")
}
func RegexpKey(str string) []string {
// 定义正则表达式
re := regexp.MustCompile(`\${([^}]+)}`)
matches := re.FindAllStringSubmatch(str, -1)
// 提取匹配项的内容
var results []string
for _, match := range matches {
if len(match) >= 2 {
results = append(results, match[1])
}
}
return results
}
func RegexpGetSql(str string) string {
// 定义正则表达式
re := regexp.MustCompile(`\${([^}]+)}`)
return re.ReplaceAllString(str, "?")
}
func GetStructKeys(obj interface{}) []string {
val := reflect.ValueOf(obj)
typ := val.Type()
keys := make([]string, 0, typ.NumField())
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
keys = append(keys, field.Name)
}
return keys
}
func GetMapKeys(obj map[string]interface{}) []string {
keys := make([]string, 0, len(obj))
for key := range obj {
keys = append(keys, key)
}
return keys
}
func CheckInterfaceIsArray(data interface{}) (bool, []map[string]interface{}) {
if data == nil {
return false, nil
}
valueType := reflect.TypeOf(data)
// 判断类型是否为数组或切片
if valueType.Kind() == reflect.Slice || valueType.Kind() == reflect.Array {
var maps []map[string]interface{}
for _, item := range data.([]interface{}) {
log.Println("item", item)
if m, ok := item.(map[string]interface{}); ok {
maps = append(maps, m)
}
}
return true, maps
}
return false, nil
}