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

gen map style json

上级 48054864
title: test
desc: wordpress article publis.
author: zentao
version: 1.0
fields:
- field: title
note: 文章标题
postfix: ", "
fields:
- field: title1
type: list
range: "Title"
postfix: "-"
- field: title2
range: 1-999
format: "%03d"
- field: content
node: 文章内容
fields:
- field: field4.1
type: list
range: [Epic,Feature,Story]:R{1}
prefix: "<h5>"
postfix: ":</h5>"
- field: field4.2
postfix: "<br/>Aaron 2020/06/18"
fields:
- field: field4.2.1
loop: 3
fields:
- field: field4.2.1.1
postfix: "<br/>"
loop: 3
fields:
- field: field4.2.2
range: a-z:R
postfix: " "
loop: 6
......@@ -3,8 +3,8 @@
$>zd.exe -y demo/test.yaml -c 15 -field field1 -o demo/output.txt -f text
执行数据生成命令。使用配置文件demo/test.yaml,生成15行数据,以text格式的输出到demo/output.txt文件中。
$>zd.exe -y demo/test.yaml -c 15 -s
执行数据生成命令。使用配置文件demo/test.yaml,生成15行数据,以行列的json形式输出到http://<ip_address/data接口。
$>zd.exe -y demo/test.yaml -head -sep , -s
执行数据生成命令。带逗号分隔的标题行,默认生成10行,以Map形式的JSON格式输出到http://127.0.0.1/data接口。
$>zd.exe -y demo/test.yaml -c 15 -field field1 -o demo/insert.sql -f sql -t table_name
执行数据生成命令。以SQL语句形式输出到demo/insert.sql文件中,表名为table_name。
......
......@@ -8,9 +8,11 @@
--field 需要输出的字段,用逗号分隔。未指定时,默认输出-yaml文件中的所有字段。
-o --out 指定输出文件
-f --format 输出格式,支持text、json、xml和sql语句,默认为text。
-t --table 输出格式为table时,用于指定insert语句的表名
-s --service 启动http服务,用户可从http://<ip_address/data接口获取json格式的行列数据。
--verbose 增加此参数,显示详细调试信息。
-t --table 输出格式为table时,用于指定insert语句的表名。
--head 生成标题行。文本格式增加标题行,JSON数据转化为Map格式(JSON默认为二维数组)。
--sep 生成标题行时,纸短见的分隔符,默认为逗号。
-s --service 启动http服务,用户可从http://<ip_address/data接口获取JSON格式的行列数据。
--verbose 增加此参数,显示详细调试信息。
为了方便在任意目录中执行%s命令,建议将其加入环境变量中,具体方法参照以下地址。
https://www.ztesting.net/book/ztf-doc/add-to-path-46.html
\ No newline at end of file
......@@ -61,6 +61,18 @@ func Print(rows [][]string, format string, table string, colTypes []bool, fields
content := ""
sql := ""
if vari.WithHead {
line := ""
for idx, field := range fields {
line += field
if idx < len(fields) - 1 {
line += vari.HeadSep
}
}
logUtils.Screen(fmt.Sprintf("%s", line))
content += line + "\n"
}
testData := model.TestData{}
testData.Title = "Test Data"
......@@ -102,23 +114,37 @@ func Print(rows [][]string, format string, table string, colTypes []bool, fields
}
}
jsonStr := "[]"
respJson := "[]"
if format == "json" || vari.HttpService {
jsonObj, _ := json.Marshal(rows)
content = string(jsonObj)
if vari.HttpService {
jsonStr = content
if vari.WithHead {
mapObj := RowsToMap(rows, fields)
jsonObj, _ := json.Marshal(mapObj)
respJson = string(jsonObj)
} else {
jsonObj, _ := json.Marshal(rows)
respJson = string(jsonObj)
}
}
if format == "xml" {
if format == "json" {
content = respJson
} else if format == "xml" {
xml, _ := xml.Marshal(testData)
content = string(xml)
} else if format == "sql" {
content = sql
}
return content, jsonStr
return content, respJson
}
func RowsToMap(rows [][]string, fieldsToExport []string) (ret map[string]string) {
for _, cols := range rows {
for j, col := range cols {
ret[fieldsToExport[j]] = col
}
}
return
}
func DataHandler(w http.ResponseWriter, r *http.Request) {
......
......@@ -22,6 +22,8 @@ var (
Verbose bool
Interpreter string
WithHead bool
HeadSep string
HttpService bool
JsonResp string = "[]"
)
......@@ -69,6 +69,9 @@ func main() {
flagSet.StringVar(&viewRes, "v", "", "")
flagSet.StringVar(&viewDetail, "vv", "", "")
flagSet.BoolVar(&vari.WithHead, "head", false, "")
flagSet.StringVar(&vari.HeadSep, "sep", ",", "")
flagSet.BoolVar(&vari.HttpService, "s", false, "")
flagSet.BoolVar(&vari.Verbose, "verbose", false, "")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册