提交 4c3a92fb 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

new features for 1.1

上级 0702e8e7
...@@ -10,14 +10,12 @@ zendata是一款通用的数据生成工具,您可以使用yaml文件来定义 ...@@ -10,14 +10,12 @@ zendata是一款通用的数据生成工具,您可以使用yaml文件来定义
-F --field 可通过该参数指定要输出的字段列表,用逗号分隔。 默认是所有的字段。 -F --field 可通过该参数指定要输出的字段列表,用逗号分隔。 默认是所有的字段。
-t --table 输出格式为sql时,需通过该参数指定要插入数据的表名。 -t --table 输出格式为sql时,需通过该参数指定要插入数据的表名。
-H --human 输出可读格式,打印字段名,并使用tab键进行分割。 -H --human 输出可读格式,打印字段名,并使用tab键进行分割。
-r --recursive 递归模式。如不指定,默认为平行模式。平行模式下各个字段独立循环。
-W --width 生成的字符串宽度,默认侧补空格。 递归模式下每个字段的取值依赖于前一字段。可增强数据的随机性。
-P --leftPad 宽度不足时,左侧补充的字符,默认为空格。此参数为非空时,RightPad设置无效。
-R --rightPad 宽度不足时,右侧补充的字符,仅在LeftPad为空时有效。
-p --port 在指定端口上运行HTTP服务。可通过http://ip/接口获得JSON格式的数据。服务模式下只支持数据生成。 -p --port 在指定端口上运行HTTP服务。可通过http://ip/接口获得JSON格式的数据。服务模式下只支持数据生成。
-b --bind 监听的ip地址,默认监听所有的ip地址。 -b --bind 监听的ip地址,默认监听所有的ip地址。
-r --root 运行HTTP服务时根目录。客户端可调用该根目录下面的配置文件。如果不指定,取zd可执行文件所在目录。 -R --root 运行HTTP服务时根目录。客户端可调用该根目录下面的配置文件。如果不指定,取zd可执行文件所在目录。
-i --input 指定一个schema文件,输出每个表的yaml配置文件。需通过-o参数指定一个输出的目录。 -i --input 指定一个schema文件,输出每个表的yaml配置文件。需通过-o参数指定一个输出的目录。
-s --server 数据库服务器类型,支持mysql|oracle|sqlite|sqlserver,默认为mysql。可用于解析yaml文件或者生成SQL。 -s --server 数据库服务器类型,支持mysql|oracle|sqlite|sqlserver,默认为mysql。可用于解析yaml文件或者生成SQL。
......
...@@ -14,10 +14,13 @@ Parameters ...@@ -14,10 +14,13 @@ Parameters
-t --table If the output format is sql, using it to specify the table name to insert data to. -t --table If the output format is sql, using it to specify the table name to insert data to.
-H --human Output a readable format, print the field name, and use the tab key to split. -H --human Output a readable format, print the field name, and use the tab key to split.
-r --recursive Recursive mode. The default mode is parallel, in which each field loops independently.
The value of a field in the recursive mode depends on that of the previous field, which enables the random data.
-p --port Run the HTTP on the specified port. The data in JSON format can be obtained via http://ip/ port. -p --port Run the HTTP on the specified port. The data in JSON format can be obtained via http://ip/ port.
Only data generation is supported. Only data generation is supported.
-b --bind Listen IP addresses. All IP addresses are listened by default. -b --bind Listen IP addresses. All IP addresses are listened by default.
-r --root The root directory when running HTTP. The client can call the config file under the root directory. -R --root The root directory when running HTTP. The client can call the config file under the root directory.
If not specified, take the directory where the zd executable file is located. If not specified, take the directory where the zd executable file is located.
-i --input Specify a sql schema file and output the YAML config file for each table. -i --input Specify a sql schema file and output the YAML config file for each table.
......
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"github.com/easysoft/zendata/src/model" "github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const" constant "github.com/easysoft/zendata/src/utils/const"
logUtils "github.com/easysoft/zendata/src/utils/log" logUtils "github.com/easysoft/zendata/src/utils/log"
stringUtils "github.com/easysoft/zendata/src/utils/string"
"github.com/easysoft/zendata/src/utils/vari" "github.com/easysoft/zendata/src/utils/vari"
"strings" "strings"
) )
...@@ -74,7 +73,7 @@ func Print(rows [][]string, format string, table string, colTypes []bool, fields ...@@ -74,7 +73,7 @@ func Print(rows [][]string, format string, table string, colTypes []bool, fields
row.Cols = append(row.Cols, col) row.Cols = append(row.Cols, col)
colVal := col colVal := col
colVal = stringUtils.AddPad(colVal) //colVal = stringUtils.AddPad(colVal, vari.Def.Fields[j])
if !colTypes[j] { colVal = "'" + colVal + "'" } if !colTypes[j] { colVal = "'" + colVal + "'" }
valueList = valueList + colVal valueList = valueList + colVal
} }
......
...@@ -49,8 +49,8 @@ func LinesToMap(str string, fieldsToExport []string) (ret []map[string]string) { ...@@ -49,8 +49,8 @@ func LinesToMap(str string, fieldsToExport []string) (ret []map[string]string) {
for j, field := range vari.Def.Fields { for j, field := range vari.Def.Fields {
col := "" // TODO: use post/pre fix to seperate col := "" // TODO: use post/pre fix to seperate
col = left[:field.Length] col = left[:field.Width]
left = left[field.Length:] left = left[field.Width:]
rowMap[fieldsToExport[j]] = col rowMap[fieldsToExport[j]] = col
} }
......
...@@ -164,8 +164,8 @@ func CopyField(child model.DefField, parent *model.DefField) { ...@@ -164,8 +164,8 @@ func CopyField(child model.DefField, parent *model.DefField) {
if child.Precision != 0 { if child.Precision != 0 {
(*parent).Precision = child.Precision (*parent).Precision = child.Precision
} }
if child.Length != 0 { if child.Width != 0 {
(*parent).Length = child.Length (*parent).Width = child.Width
} }
} }
......
...@@ -228,8 +228,8 @@ func GenerateFieldValWithFix(field model.DefField, fieldValue model.FieldValue, ...@@ -228,8 +228,8 @@ func GenerateFieldValWithFix(field model.DefField, fieldValue model.FieldValue,
loopStr = prefix + loopStr + postfix loopStr = prefix + loopStr + postfix
} }
if field.Length > runewidth.StringWidth(loopStr) { if field.Width > runewidth.StringWidth(loopStr) {
loopStr = loopStr + strings.Repeat(" ", field.Length - runewidth.StringWidth(loopStr)) loopStr = stringUtils.AddPad(loopStr, field)
} }
return loopStr return loopStr
......
...@@ -34,7 +34,9 @@ type DefData struct { ...@@ -34,7 +34,9 @@ type DefData struct {
type DefField struct { type DefField struct {
FieldBase `yaml:",inline"` FieldBase `yaml:",inline"`
Fields []DefField `yaml:"fields,flow"` Fields []DefField `yaml:"fields,flow"`
Length int `yaml:"length"` Width int `yaml:"width"`
LeftPad string `yaml:"leftPad"`
RightPad string `yaml:"rightPad"`
Path string Path string
} }
......
...@@ -14,7 +14,7 @@ const ( ...@@ -14,7 +14,7 @@ const (
md5Col = "CW" md5Col = "CW"
) )
func AddMd5(path string, salt string) { func AddMd5(path string) {
excel, err := excelize.OpenFile(path) excel, err := excelize.OpenFile(path)
if err != nil { if err != nil {
logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_read_file", path)) logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_read_file", path))
...@@ -46,7 +46,7 @@ func AddMd5(path string, salt string) { ...@@ -46,7 +46,7 @@ func AddMd5(path string, salt string) {
val := strings.TrimSpace(col) val := strings.TrimSpace(col)
str = str + val str = str + val
} }
md5Str := md5V(str, salt) md5Str := md5V(str)
excel.SetCellValue(sheet, md5Col + strconv.Itoa(index + 1), md5Str) excel.SetCellValue(sheet, md5Col + strconv.Itoa(index + 1), md5Str)
} }
} }
...@@ -56,9 +56,8 @@ func AddMd5(path string, salt string) { ...@@ -56,9 +56,8 @@ func AddMd5(path string, salt string) {
} }
} }
func md5V(str, salt string) string { func md5V(str string) string {
h := md5.New() h := md5.New()
h.Write([]byte(str)) h.Write([]byte(str))
h.Write([]byte(salt))
return hex.EncodeToString(h.Sum(nil)) return hex.EncodeToString(h.Sum(nil))
} }
\ No newline at end of file
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
) )
func ParseRequestParams(req *http.Request) (root, defaultFile, yamlFile string, count int, func ParseRequestParams(req *http.Request) (root, defaultFile, yamlFile string, count int,
fields, human string, width int, leftPad, rightPad, format, table string) { fields, human string, format, table string) {
query := req.URL.Query() query := req.URL.Query()
root = GetRequestParams(query,"root", "r") root = GetRequestParams(query,"root", "r")
...@@ -24,10 +24,6 @@ func ParseRequestParams(req *http.Request) (root, defaultFile, yamlFile string, ...@@ -24,10 +24,6 @@ func ParseRequestParams(req *http.Request) (root, defaultFile, yamlFile string,
table = "" table = ""
human = GetRequestParams(query,"human", "H") human = GetRequestParams(query,"human", "H")
widthStr := GetRequestParams(query,"width", "W")
width, _ = strconv.Atoi(widthStr)
leftPad = GetRequestParams(query,"leftPad", "L")
rightPad = GetRequestParams(query,"rightPad", "R")
req.ParseForm() req.ParseForm()
defaultDefContent := req.FormValue("default") defaultDefContent := req.FormValue("default")
......
...@@ -5,8 +5,8 @@ import ( ...@@ -5,8 +5,8 @@ import (
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const" constant "github.com/easysoft/zendata/src/utils/const"
"github.com/easysoft/zendata/src/utils/vari"
"github.com/mattn/go-runewidth" "github.com/mattn/go-runewidth"
"strconv" "strconv"
"strings" "strings"
...@@ -109,20 +109,20 @@ func InArray(need interface{}, arr []string) bool { ...@@ -109,20 +109,20 @@ func InArray(need interface{}, arr []string) bool {
return false return false
} }
func AddPad(str string) string { func AddPad(str string, field model.DefField) string {
if vari.Length > 0 { if field.Width > 0 && field.Width > runewidth.StringWidth(str) {
gap := vari.Length - len(str) gap := field.Width - len(str)
if vari.LeftPad != "" { if field.LeftPad != "" {
vari.LeftPad = vari.LeftPad[:1] field.LeftPad = field.LeftPad[:1]
pads := strings.Repeat(vari.LeftPad, gap) pads := strings.Repeat(field.LeftPad, gap)
str = pads + str str = pads + str
} else if vari.RightPad != "" { } else if field.RightPad != "" {
vari.RightPad = vari.RightPad[:1] field.RightPad = field.RightPad[:1]
pads := strings.Repeat(vari.RightPad, gap) pads := strings.Repeat(field.RightPad, gap)
str = str + pads str = str + pads
} else { } else {
vari.LeftPad = " " field.LeftPad = " "
pads := strings.Repeat(vari.LeftPad, gap) pads := strings.Repeat(field.LeftPad, gap)
str = pads + str str = pads + str
} }
} }
......
...@@ -23,10 +23,6 @@ var ( ...@@ -23,10 +23,6 @@ var (
WithHead bool WithHead bool
HeadSep string HeadSep string
Length int
LeftPad string
RightPad string
JsonResp string = "[]" JsonResp string = "[]"
Ip string Ip string
Port int Port int
......
...@@ -41,7 +41,6 @@ var ( ...@@ -41,7 +41,6 @@ var (
viewRes string viewRes string
viewDetail string viewDetail string
md5 string md5 string
salt string
example bool example bool
help bool help bool
...@@ -88,7 +87,6 @@ func main() { ...@@ -88,7 +87,6 @@ func main() {
flagSet.StringVar(&viewRes, "view", "", "") flagSet.StringVar(&viewRes, "view", "", "")
flagSet.StringVar(&md5, "md5", "", "") flagSet.StringVar(&md5, "md5", "", "")
flagSet.StringVar(&salt, "salt", "", "")
flagSet.StringVar(&vari.HeadSep, "H", "", "") flagSet.StringVar(&vari.HeadSep, "H", "", "")
flagSet.StringVar(&vari.HeadSep, "human", "", "") flagSet.StringVar(&vari.HeadSep, "human", "", "")
...@@ -96,18 +94,11 @@ func main() { ...@@ -96,18 +94,11 @@ func main() {
flagSet.StringVar(&analyse, "a", "", "") flagSet.StringVar(&analyse, "a", "", "")
flagSet.StringVar(&analyse, "analyse", "", "") flagSet.StringVar(&analyse, "analyse", "", "")
flagSet.IntVar(&vari.Length, "W", 0, "")
flagSet.IntVar(&vari.Length, "width", 0, "")
flagSet.StringVar(&vari.LeftPad, "L", "", "")
flagSet.StringVar(&vari.LeftPad, "leftPad", "", "")
flagSet.StringVar(&vari.RightPad, "R", "", "")
flagSet.StringVar(&vari.RightPad, "rightPad", "", "")
flagSet.StringVar(&vari.Ip, "b", "", "") flagSet.StringVar(&vari.Ip, "b", "", "")
flagSet.StringVar(&vari.Ip, "bind", "", "") flagSet.StringVar(&vari.Ip, "bind", "", "")
flagSet.IntVar(&vari.Port, "p", 0, "") flagSet.IntVar(&vari.Port, "p", 0, "")
flagSet.IntVar(&vari.Port, "port", 0, "") flagSet.IntVar(&vari.Port, "port", 0, "")
flagSet.StringVar(&root, "r", "", "") flagSet.StringVar(&root, "R", "", "")
flagSet.StringVar(&root, "root", "", "") flagSet.StringVar(&root, "root", "", "")
flagSet.BoolVar(&example, "e", false, "") flagSet.BoolVar(&example, "e", false, "")
...@@ -137,7 +128,7 @@ func main() { ...@@ -137,7 +128,7 @@ func main() {
service.ViewRes(viewRes) service.ViewRes(viewRes)
return return
} else if md5 != "" { } else if md5 != "" {
service.AddMd5(md5, salt) service.AddMd5(md5)
return return
} else if analyse != "" { } else if analyse != "" {
gen.Analyse(output, fields, analyse, defaultFile) gen.Analyse(output, fields, analyse, defaultFile)
...@@ -203,8 +194,7 @@ func StartServer() { ...@@ -203,8 +194,7 @@ func StartServer() {
} }
func DataHandler(w http.ResponseWriter, req *http.Request) { func DataHandler(w http.ResponseWriter, req *http.Request) {
root, defaultFile, configFile, count, fields, root, defaultFile, configFile, count, fields, vari.HeadSep,
vari.HeadSep, vari.Length, vari.LeftPad, vari.RightPad,
format, table = service.ParseRequestParams(req) format, table = service.ParseRequestParams(req)
vari.RunMode = constant.RunModeServerRequest vari.RunMode = constant.RunModeServerRequest
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册