提交 24722e8c 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

fix issues

上级 6c79edf9
......@@ -2,6 +2,7 @@ package helper
import (
"fmt"
"math"
"strconv"
)
......@@ -50,9 +51,9 @@ func GenInt(hasSign bool) (ret, note string) {
}
func GenBigint(hasSign bool) (ret, note string) {
if hasSign {
ret = "-9223372036854775808-9223372036854775807:R"
ret = fmt.Sprintf("%d-%d:R", math.MinInt64, math.MaxInt64)
} else {
ret = "0-18446744073709551615:R"
ret = fmt.Sprintf("0-%d:R", math.MaxInt64)
}
note = "bigint 2^64"
......@@ -90,20 +91,19 @@ func GenDecimal(hasSign bool) (ret, note string) {
return
}
func GenChar(param string) (ret string) {
rang := `a-z`
func GenChar(param string) (ret string, loop string) {
ret = `a-z`
paramInt, _ := strconv.Atoi(param)
if paramInt > 0 {
rang += fmt.Sprintf("{%d!}", paramInt)
loopInt, _ := strconv.Atoi(param)
if loopInt > 0 {
loop = fmt.Sprintf("%d", loopInt)
}
return
}
func GenBin() (from, format string) {
format = "binary"
format = "binary()"
return
}
......
......@@ -85,7 +85,7 @@ func generateDefByVarcharType(param string, info *FieldTypeInfo) {
info.Use = "length32_random"
} else if info.VarcharType == consts.JsonStr {
info.Format = "id_card()"
info.Format = "json()"
} else if info.VarcharType == consts.Md5 {
info.Rang = "1-100"
......@@ -213,28 +213,33 @@ func GetVarcharTypeByRecords(records []interface{}) (ret consts.VarcharType) {
if govalidator.IsEmail(val) {
ret = consts.Email
} else if govalidator.IsCreditCard(val) {
ret = consts.CreditCard
} else if govalidator.IsMAC(val) {
ret = consts.Mac
} else if govalidator.IsUUID(val) {
ret = consts.Uuid
} else if govalidator.IsJSON(val) {
ret = consts.JsonStr
//} else if govalidator.IsUnixTime(val) {
// ret = consts.UnixTime
} else if govalidator.IsMD5(val) {
ret = consts.Md5
} else if IsMobilePhone(val) {
ret = consts.MobileNumber
} else if IsTelPhone(val) {
ret = consts.TelNumber
} else if IsIDCard(val) {
ret = consts.IdCard
} else if govalidator.IsURL(val) {
ret = consts.Url
} else if govalidator.IsJSON(val) {
ret = consts.JsonStr
}
return
......@@ -248,141 +253,132 @@ type FieldTypeInfo struct {
Precision int
HasSign bool
Note string
Rang string
Type string
Format string
From string
Use string
Select string
Prefix string
Note string
Rang string
Loop string
Loopfix string
Type string
Format string
From string
Use string
Select string
Prefix string
}
func GenDefByColumnType(param string, ret *FieldTypeInfo) {
rang := ""
typ := ""
format := ""
from := ""
selectStr := ""
note := ""
switch ret.ColumnType {
// int
case "bit":
rang, note = GenBit()
ret.Rang, ret.Note = GenBit()
case "tinyint":
rang, note = GenTinyint(ret.HasSign)
ret.Rang, ret.Note = GenTinyint(ret.HasSign)
case "smallint":
rang, note = GenSmallint(ret.HasSign)
ret.Rang, ret.Note = GenSmallint(ret.HasSign)
case "mediumint":
rang, note = GenMediumint(ret.HasSign)
ret.Rang, ret.Note = GenMediumint(ret.HasSign)
case "int":
rang, note = GenInt(ret.HasSign)
ret.Rang, ret.Note = GenInt(ret.HasSign)
case "bigint":
rang, note = GenBigint(ret.HasSign)
ret.Rang, ret.Note = GenBigint(ret.HasSign)
// float
case "float":
rang, note = GenFloat(ret.HasSign)
ret.Rang, ret.Note = GenFloat(ret.HasSign)
case "double":
rang, note = GenDouble(ret.HasSign)
ret.Rang, ret.Note = GenDouble(ret.HasSign)
// fixed-point
case "decimal":
rang, note = GenDecimal(ret.HasSign)
ret.Rang, ret.Note = GenDecimal(ret.HasSign)
// string
case "char":
rang = GenChar(param)
ret.Rang, ret.Loop = GenChar(param)
case "tinytext":
from = "idiom.v1.idiom"
selectStr = "word"
ret.From = "idiom.v1.idiom"
ret.Select = "word"
case "text":
from = "xiehouyu.v1.xiehouyu"
selectStr = "riddle"
ret.From = "xiehouyu.v1.xiehouyu"
ret.Select = "riddle"
case "mediumtext":
from = "joke.v1.joke"
selectStr = "content"
ret.From = "joke.v1.joke"
ret.Select = "content"
case "longtext":
from = "song.v1.song"
selectStr = "lyric"
ret.From = "song.v1.song"
ret.Select = "lyric"
// binary data
case "tinyblob":
from, format = GenBin()
ret.From, ret.Format = GenBin()
case "blob":
from, format = GenBin()
ret.From, ret.Format = GenBin()
case "mediumblob":
from, format = GenBin()
ret.From, ret.Format = GenBin()
case "longblob":
from, format = GenBin()
ret.From, ret.Format = GenBin()
case "binary":
from, format = GenBin()
ret.From, ret.Format = GenBin()
case "varbinary":
from, format = GenBin()
ret.From, ret.Format = GenBin()
// date and time type
case "date":
rang, typ, format = GenDate()
ret.Rang, ret.Type, ret.Format = GenDate()
case "time":
rang, typ, format = GenTime()
ret.Rang, ret.Type, ret.Format = GenTime()
case "year":
rang, typ, format = GenYear()
ret.Rang, ret.Type, ret.Format = GenYear()
case "datetime":
rang, typ, format = GenDatetime()
ret.Rang, ret.Type, ret.Format = GenDatetime()
case "timestamp":
rang, typ, format = GenTimestamp()
ret.Rang, ret.Type, ret.Format = GenTimestamp()
// other type
case "enum":
rang = getEnumValue(param)
ret.Rang = getEnumValue(param)
case "set":
rang = getSetValue(param)
ret.Rang, ret.Loop, ret.Loopfix = getSetValue(param)
//case "geometry":
// rang = `"geometry"`
// ret.Rang = `"geometry"`
//case "point":
// rang = `"point"`
// ret.Rang = `"point"`
//case "linestring":
// rang = `"linestring"`
// ret.Rang = `"linestring"`
//case "polygon":
// rang = `"polygon"`
// ret.Rang = `"polygon"`
//case "multipoint":
// rang = `"multipoint"`
// ret.Rang = `"multipoint"`
//case "multilinestring":
// rang = `"multilinestring"`
// ret.Rang = `"multilinestring"`
//case "multipolygon":
// rang = `"multipolygon"`
// ret.Rang = `"multipolygon"`
//case "geometrycollection":
// rang = `"geometrycollection"`
// ret.Rang = `"geometrycollection"`
default:
}
ret.Rang = rang
ret.Format = format
ret.Type = typ
ret.From = from
ret.Select = selectStr
ret.Note = note
return
}
func getEnumValue(param string) (ret string) {
arr := strings.Split(param, ",")
num := getRandNum(len(arr))
ret = strings.Trim(arr[num], "'")
ret = strings.ReplaceAll(param, "'", "")
return
}
func getSetValue(param string) (ret string) {
//arr := strings.Split(param, ",")
//ret = strings.Join(arr, ",")
func getSetValue(param string) (ret, loop, loopfix string) {
ret = strings.ReplaceAll(param, "'", "")
arr := strings.Split(param, ",")
start := 1
if len(arr) > 2 {
start = 2
}
loop = fmt.Sprintf("%d-%d", start, len(arr))
return
}
......
......@@ -19,6 +19,7 @@ import (
)
func FormatStr(format string, val interface{}, precision int) (ret string, pass bool) {
format = strings.TrimSpace(format)
if format == "" {
return val.(string), true
}
......@@ -47,12 +48,12 @@ func FormatStr(format string, val interface{}, precision int) (ret string, pass
pass = true
return
} else if strings.Index(format, "uuid") > -1 {
} else if strings.Index(format, "uuid") == 0 {
ret = GenerateUuid(format)
pass = true
return
} else if strings.Index(format, "password") > -1 {
} else if strings.Index(format, "password") == 0 {
length := 8
regx := regexp.MustCompile(`password\(\s*(\d+)\s*\)`)
arr := regx.FindStringSubmatch(format)
......@@ -67,12 +68,12 @@ func FormatStr(format string, val interface{}, precision int) (ret string, pass
pass = true
return
} else if strings.Index(format, "binary") > -1 {
} else if strings.Index(format, "binary") == 0 {
ret = ImgBindata
pass = true
return
} else if strings.Index(format, "credit_card") > -1 {
} else if strings.Index(format, "credit_card") == 0 {
cardType := ""
regx := regexp.MustCompile(`credit_card\(\s*(\S+)\s*\)`)
......@@ -85,19 +86,24 @@ func FormatStr(format string, val interface{}, precision int) (ret string, pass
pass = true
return
} else if strings.Index(format, "id_card") > -1 {
} else if strings.Index(format, "id_card") == 0 {
ret = GenerateIdCard()
pass = true
return
} else if strings.Index(format, "mac") > -1 {
} else if strings.Index(format, "mac") == 0 {
ret = GenerateMac()
pass = true
return
} else if strings.Index(format, "token") > -1 {
} else if strings.Index(format, "token") == 0 {
ret = GenerateToken(format)
pass = true
return
} else if strings.Index(format, "json") == 0 {
ret = Json
pass = true
return
}
......
......@@ -243,11 +243,12 @@ func (s *SqlParseService) genTablesYaml(statementMap map[string]string,
field.Range = types[col].Rang
field.Type = types[col].Type
field.Loop = types[col].Loop
field.Format = types[col].Format
field.From = types[col].From
field.Use = types[col].Use
field.From = types[col].From
field.Select = types[col].From
field.Select = types[col].Select
field.Prefix = types[col].Prefix
field.Note = types[col].Note
......
......@@ -191,5 +191,5 @@ fields:
# postfix: "\t"
# - field: test
# format: "binary"
# format: "binary()"
# postfix: "\t"
\ No newline at end of file
......@@ -12,20 +12,20 @@ fields:
- field: field_format_func_md5
range: a-z
format: md5
format: md5()
postfix: "\t"
- field: field_format_func_sha1
range: password
format: sha1
format: sha1()
postfix: "\t"
- field: field_format_func_base64
range: "http://zendata.cn?&=word+"
format: base64
format: base64()
postfix: "\t"
- field: field_format_func_urlencode
range: "http://zendata.cn?&=word+"
format: urlencode
format: urlencode()
postfix: "\t"
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册