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

decode with pre/post fix

上级 bfb4a0ae
......@@ -5,17 +5,17 @@ version: 1.0
fields:
- field: field_use_instance # 引用default.xml里的嵌套字段
length: 16
prefix: "[" # 复写前缀
postfix: "]" # 特殊字符加引号,否则无法解析
- field: field_use_excel
prefix: " ===" # 复写前缀
prefix: " [" # 复写前缀
postfix: "] " # 特殊字符加引号,否则无法解析
- field: field_with_children
prefix: " [" # 复写前缀
postfix: "] " # 复写后缀
length: 40
postfix: "] " # 特殊字符加引号,否则无法解析
- field: field_loop # 引用default.xml里的自循环的字段
prefix: " [" # 复写前缀
prefix: "[" # 复写前缀
postfix: "]" # 特殊字符加引号,否则无法解析
length: 8
......@@ -10,12 +10,15 @@ zendata是一款通用的数据生成工具,您可以使用yaml文件来定义
-F --field 可通过该参数指定要输出的字段列表,用逗号分隔。 默认是所有的字段。
-t --table 输出格式为sql时,需通过该参数指定要插入数据的表名。
-H --human 输出可读格式,打印字段名,并使用tab键进行分割。
-r --recursive 递归模式。如不指定,默认为平行模式。平行模式下各个字段独立循环。
递归模式下每个字段的取值依赖于前一字段。可增强数据的随机性。
-p --port 在指定端口上运行HTTP服务。可通过http://ip/接口获得JSON格式的数据。服务模式下只支持数据生成。
-b --bind 监听的ip地址,默认监听所有的ip地址。
-R --root 运行HTTP服务时根目录。客户端可调用该根目录下面的配置文件。如果不指定,取zd可执行文件所在目录。
-i --input 指定一个schema文件,输出每个表的yaml配置文件。需通过-o参数指定一个输出的目录。
-D --decode 根据指定的配置文件,将通过-i参数指定的数据文件解析成json格式。
-e --example 打印示例的数据格式配置文件。
-l --list 列出所有支持的数据格式。
......@@ -33,7 +36,8 @@ $>zd.exe -d demo\default.yaml -c demo\test.yaml -n 100 -o test.json 输出json
$>zd.exe -d demo\default.yaml -c demo\test.yaml -n 100 -o test.xml 输出xml格式的数据。
$>zd.exe -d demo\default.yaml -n 100 -o test.sql -t user -s mysql 输出插入到user表里面的sql。
$>zd.exe -i db.sql -s mysql -o db 根据db.sql的定义生成每个表的yaml文件,存储到db目录里面。
$>zd.exe -i db.sql -o db 根据db.sql的定义生成每个表的yaml文件,存储到db目录里面。
$>zd.exe -c demo\default.yaml -i test.txt --decode 将-i指定的文件根据-d参数的配置进行解析。
$>zd.exe -l # 列出所有內置数据。
$>zd.exe -v system.address.v1 # 查看內置Excel文件system/address/v1.xlsx中的数据表。
......
......@@ -25,6 +25,8 @@ Parameters
-i --input Specify a sql schema file and output the YAML config file for each table.
You need to specify an output directory by using -o.
-D --decode Referring to the specified configuration file, parse the data file specified by -i and output json.
Also you can output the readable format via -H.
-e --example Print the data format config file of the example.
-l --list List all supported data formats.
......@@ -43,6 +45,7 @@ $>zd.exe -d demo/default.yaml -c demo/test.yaml -n 100 -o test.xml # Output da
$>zd.exe -d demo/default.yaml -n 100 -o test.sql -t user -s mysql # Output the sql inserted into the table user.
$>zd.exe -i db.sql -o db # Generate YAML files for each table by parsing db.sql and store them in the db directory.
$>zd.exe -c demo/default.yaml -i test.txt --decode # Parse the file specified by -i according to the config of -d.
$>zd.exe -l # List all build-in data types.
$>zd.exe -v system.address.v1 # View data types in build-in Excel file system/address/v1.xlsx.
......
......@@ -108,6 +108,10 @@
{
"id": "excel_data_1",
"translation": "Excel that contains below data."
},
{
"id": "analyse_success",
"translation": "Success to decode data to %s."
}
]
}
\ No newline at end of file
......@@ -99,6 +99,10 @@
{
"id": "excel_data_1",
"translation": "Excel数据源,包含以下内容."
},
{
"id": "analyse_success",
"translation": "成功解析数据到%s."
}
]
}
\ No newline at end of file
此差异已折叠。
package gen
import (
"encoding/json"
fileUtils "github.com/easysoft/zendata/src/utils/file"
"github.com/easysoft/zendata/src/utils/vari"
"strings"
)
const (
)
func Analyse(output, fieldsToExportStr, configFile, defaultFile string) {
fieldsToExport := make([]string, 0)
if fieldsToExportStr != "" {
fieldsToExport = strings.Split(fieldsToExportStr, ",")
}
vari.Def = LoadConfigDef(defaultFile, configFile, &fieldsToExport)
vari.Res = LoadResDef(fieldsToExport)
data := fileUtils.ReadFile(output)
mapArr := LinesToMap(data, fieldsToExport)
jsonObj, _ := json.Marshal(mapArr)
respJson := string(jsonObj)
path := output[:strings.LastIndex(output, ".")] + ".json"
fileUtils.WriteFile(path, respJson)
}
func LinesToMap(str string, fieldsToExport []string) (ret []map[string]string) {
start := 0
if vari.WithHead {
start = 1
}
ret = []map[string]string{}
for index, line := range strings.Split(str, "\n") {
if index < start {
continue
}
rowMap := map[string]string{}
left := line
for j, field := range vari.Def.Fields {
col := "" // TODO: use post/pre fix to seperate
col = left[:field.Width]
left = left[field.Width:]
rowMap[fieldsToExport[j]] = col
}
ret = append(ret, rowMap)
}
return
}
\ No newline at end of file
package gen
import (
"encoding/json"
"github.com/easysoft/zendata/src/model"
fileUtils "github.com/easysoft/zendata/src/utils/file"
i118Utils "github.com/easysoft/zendata/src/utils/i118"
logUtils "github.com/easysoft/zendata/src/utils/log"
"github.com/easysoft/zendata/src/utils/vari"
"github.com/mattn/go-runewidth"
"strings"
)
const (
)
func Decode(defaultFile, configFile, fieldsToExportStr, input, output string) {
fieldsToExport := make([]string, 0)
if fieldsToExportStr != "" {
fieldsToExport = strings.Split(fieldsToExportStr, ",")
}
vari.Def = LoadConfigDef(defaultFile, configFile, &fieldsToExport)
vari.Res = LoadResDef(fieldsToExport)
data := fileUtils.ReadFile(input)
ret := []map[string]interface{}{}
LinesToMap(data, fieldsToExport, &ret)
jsonObj, _ := json.Marshal(ret)
respJson := string(jsonObj)
fileUtils.WriteFile(output, respJson)
logUtils.Screen(i118Utils.I118Prt.Sprintf("analyse_success", output ))
}
func LinesToMap(str string, fieldsToExport []string, ret *[]map[string]interface{}) {
start := 0
if vari.WithHead {
start = 1
}
for index, line := range strings.Split(str, "\n") {
if index < start {
continue
}
rowMap := decodeOnLevel(line, fieldsToExport, vari.Def.Fields)
*ret = append(*ret, rowMap)
}
return
}
func decodeOnLevel(line string, fieldsToExport []string, fields []model.DefField) (ret map[string]interface{}) {
ret = map[string]interface{}{}
left := []rune(line)
for j, field := range fields {
col := ""
if field.Width > 0 {
col = string(left[:field.Width])
left = left[field.Width:]
} else {
sepStr := ""
if j < len(vari.Def.Fields) - 1 {
sepStr = field.Postfix + vari.Def.Fields[j+1].Prefix
} else {
sepStr = field.Postfix
}
sep := []rune(sepStr)
if len(sep) > 0 {
index := searchRune(left, sep)
if index > -1 {
col = string(left[: index+runewidth.StringWidth(field.Postfix)])
left = left[index+runewidth.StringWidth(field.Postfix) :]
}
}
}
ret[fieldsToExport[j]] = col
}
return
}
func searchRune(text []rune, what []rune) int {
for i := range text {
found := true
for j := range what {
if i+j < len(text) && text[i+j] != what[j] {
found = false
break
}
}
if found {
return i
}
}
return -1
}
\ No newline at end of file
......@@ -25,7 +25,7 @@ func RemoveBlankLine(str string) string {
ret := myExp.ReplaceAllString(str, "\n\n")
ret = strings.Trim(ret, "\n")
ret = strings.TrimSpace(ret)
//ret = strings.TrimSpace(ret)
return ret
}
......
......@@ -35,7 +35,7 @@ var (
output string
table string
format = constant.FormatText
analyse string
decode bool
listRes bool
viewRes string
......@@ -91,8 +91,8 @@ func main() {
flagSet.StringVar(&vari.HeadSep, "H", "", "")
flagSet.StringVar(&vari.HeadSep, "human", "", "")
flagSet.StringVar(&analyse, "a", "", "")
flagSet.StringVar(&analyse, "analyse", "", "")
flagSet.BoolVar(&decode, "D", false, "")
flagSet.BoolVar(&decode, "decode", false, "")
flagSet.StringVar(&vari.Ip, "b", "", "")
flagSet.StringVar(&vari.Ip, "bind", "", "")
......@@ -130,8 +130,8 @@ func main() {
} else if md5 != "" {
service.AddMd5(md5)
return
} else if analyse != "" {
gen.Analyse(output, fields, analyse, defaultFile)
} else if decode {
gen.Decode(defaultFile, configFile, fields, input, output)
return
}
......
package main
import (
"github.com/easysoft/zendata/src/gen"
httpUtils "github.com/easysoft/zendata/src/utils/http"
"io/ioutil"
"net/url"
"fmt"
"strings"
)
func main() {
urlStr := "http://127.0.0.1:8848/?F=field3&lines=12"
file := "test/code/test.yaml"
//urlStr := "http://127.0.0.1:8848/?F=field3&lines=12"
//file := "test/code/test.yaml"
//
//yamlContent, _ := ioutil.ReadFile(file)
//yamlContent = gen.ReplaceSpecialChars(yamlContent)
//
//data := url.Values{"config": {string(yamlContent)}}
//httpUtils.PostForm(urlStr, data)
yamlContent, _ := ioutil.ReadFile(file)
yamlContent = gen.ReplaceSpecialChars(yamlContent)
data := url.Values{"config": {string(yamlContent)}}
httpUtils.PostForm(urlStr, data)
a := "我的"
b := "dsfdsfdf是我的吗"
index := strings.Index(a, b)
fmt.Println(index)
}
cd build
cp zd-x86.exe zd.exe
zip -r zd-win-x86-1.1.zip zd.exe conf data demo
zip -r zd-win-x86-1.1.zip zd.exe data demo
rm zd.exe
cp zd-amd64.exe zd.exe
zip -r zd-win-amd64-1.1.zip zd.exe conf data demo
zip -r zd-win-amd64-1.1.zip zd.exe data demo
rm zd.exe
cp zd-linux zd
tar -zcvf zd-linux-1.1.tar.gz zd conf data demo
tar -zcvf zd-linux-1.1.tar.gz zd data demo
rm zd
cp zd-mac zd
zip -r zd-mac-1.1.zip zd conf data demo
zip -r zd-mac-1.1.zip zd data demo
rm zd
cd ..
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册