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

new features for 1.1

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