2020-07-23 09:52:13 +00:00
|
|
|
package utils
|
2020-07-23 05:59:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
2020-07-23 09:52:13 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2020-07-23 05:59:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 驼峰转蛇形
|
2020-07-23 09:52:13 +00:00
|
|
|
func ToSnakeString(s string) string {
|
2020-07-23 05:59:39 +00:00
|
|
|
data := make([]byte, 0, len(s)*2)
|
|
|
|
j := false
|
|
|
|
num := len(s)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
d := s[i]
|
|
|
|
if i > 0 && d >= 'A' && d <= 'Z' && j {
|
|
|
|
data = append(data, '_')
|
|
|
|
}
|
|
|
|
if d != '_' {
|
|
|
|
j = true
|
|
|
|
}
|
|
|
|
data = append(data, d)
|
|
|
|
}
|
|
|
|
return strings.ToLower(string(data[:]))
|
2020-07-23 09:52:13 +00:00
|
|
|
|
2020-07-23 05:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 蛇形转驼峰
|
|
|
|
func ToCamelString(s string) string {
|
|
|
|
data := make([]byte, 0, len(s))
|
|
|
|
j := false
|
|
|
|
k := false
|
|
|
|
num := len(s) - 1
|
|
|
|
for i := 0; i <= num; i++ {
|
|
|
|
d := s[i]
|
|
|
|
if k == false && d >= 'A' && d <= 'Z' {
|
|
|
|
k = true
|
|
|
|
}
|
|
|
|
if d >= 'a' && d <= 'z' && (j || k == false) {
|
|
|
|
d = d - 32
|
|
|
|
j = false
|
|
|
|
k = true
|
|
|
|
}
|
|
|
|
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
|
|
|
|
j = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
data = append(data, d)
|
|
|
|
}
|
|
|
|
return string(data[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
//递归的语义化分析
|
|
|
|
// todo 目前只做单层,以后支持多层
|
2020-07-23 09:52:13 +00:00
|
|
|
func GetStructCodeFromJson(s string, structName string) string {
|
2020-07-23 05:59:39 +00:00
|
|
|
gencode := "type " + structName + " struct {\r\n"
|
|
|
|
dat := map[string]interface{}{}
|
2020-07-23 09:52:13 +00:00
|
|
|
e := json.Unmarshal([]byte(s), &dat)
|
|
|
|
if nil != e {
|
2020-07-23 05:59:39 +00:00
|
|
|
log.Print(e.Error())
|
|
|
|
return ""
|
|
|
|
}
|
2020-07-23 09:52:13 +00:00
|
|
|
for k, v := range dat {
|
|
|
|
log.Print(k, " ", reflect.TypeOf(v).Kind().String())
|
2020-07-23 05:59:39 +00:00
|
|
|
//如果是切片,切片类型
|
2020-07-23 09:52:13 +00:00
|
|
|
if reflect.TypeOf(v).Kind() == reflect.Slice {
|
|
|
|
gencode += " " + ToCamelString(k) + " " + reflect.TypeOf(v).Kind().String() + " `json:\"" + k + "\"` \r\n"
|
|
|
|
log.Print(reflect.ValueOf(v).Slice(0, 0).Kind())
|
2020-07-23 05:59:39 +00:00
|
|
|
//reflect.ValueOf(v).Slice(0,1)
|
|
|
|
// todo 先暂时用interface代替
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// 如果是字典
|
2020-07-23 09:52:13 +00:00
|
|
|
if reflect.TypeOf(v).Kind() == reflect.Map {
|
|
|
|
gencode += " " + ToCamelString(k) + " map[string]interface{} " + " `json:\"" + k + "\"` \r\n"
|
2020-07-23 05:59:39 +00:00
|
|
|
|
2020-07-23 09:52:13 +00:00
|
|
|
for _, v := range reflect.ValueOf(v).MapKeys() {
|
2020-07-23 05:59:39 +00:00
|
|
|
log.Print(v)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2020-07-23 09:52:13 +00:00
|
|
|
gencode += " " + ToCamelString(k) + " " + reflect.TypeOf(v).Kind().String() + " `json:\"" + k + "\"` \r\n"
|
2020-07-23 05:59:39 +00:00
|
|
|
// 基础类型直接加代码
|
|
|
|
}
|
|
|
|
gencode += "}"
|
|
|
|
return gencode
|
2020-07-23 09:52:13 +00:00
|
|
|
}
|