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

close task#7690

上级 12b71395
title: zendata数据配置语法说明
desc:
author: zentao
version: 1.0
type: lines # lines|media|binary|article
fields:
- field: subject
from: chinese.words.v1.cn
select: word
where: ming='y' and dong="n"
- field: verb
from: chinese.words.v1.cn
select: word
where: dong='y'
- field: object
from: chinese.words.v1.cn
select: word
where: ming='y'
......@@ -89,7 +89,7 @@ fields:
postfix: "\t"
- field: field_repeat # Use {} to define repeated elements.
range: u-1{3},[u2,u3]{2},[1-3]{3} # u-1,u-1,u-1,u2,u2,user3,user3,1,1,1,2,2,2,3,3,3
range: u-1{3},[u2,u3]{2},[1-3]{3} # u-1,u-1,u-1,u2,u2,u3,u3,1,1,1,2,2,2,3,3,3
postfix: "\t"
- field: field_format # Output as formatted strings.
......
......@@ -89,7 +89,7 @@ fields:
postfix: "\t"
- field: field_repeat # 通过{}定义重复的元素。
range: user-1{3},[user2,user3]{2} # user-1,user-1,user-1,user2,user3,user2,user3
range: user-1{3},[user2,user3]{2} # user-1,user-1,user-1,user2,user2,user3,user3
postfix: "\t"
- field: field_format # 通过格式化字符串输出。
......
......@@ -9,20 +9,12 @@ import (
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/fatih/color"
"github.com/mattn/go-runewidth"
"net/http"
"os"
"regexp"
"strings"
"time"
)
var (
FileWriter *os.File
HttpWriter http.ResponseWriter
)
func Generate(defaultFile string, configFile string, total int, fieldsToExportStr string, out string, format string, table string) {
startTime := time.Now().Unix()
......@@ -92,13 +84,13 @@ func Print(rows [][]string, format string, table string, colIsNumArr []bool, fie
}
if format == constant.FormatText {
printLine(lineForText)
logUtils.PrintLine(lineForText)
} else if format == constant.FormatSql {
printLine(genSqlLine(strings.Join(valuesForSql, ", "), i, len(rows)))
logUtils.PrintLine(genSqlLine(strings.Join(valuesForSql, ", "), i, len(rows)))
} else if format == constant.FormatJson {
printLine(genJsonLine(i, row, len(rows), fields))
logUtils.PrintLine(genJsonLine(i, row, len(rows), fields))
} else if format == constant.FormatXml {
printLine(getXmlLine(i, rowMap, len(rows)))
logUtils.PrintLine(getXmlLine(i, rowMap, len(rows)))
}
}
}
......@@ -115,21 +107,21 @@ func printTextHeader(fields []string) {
}
}
printLine(headerLine)
logUtils.PrintLine(headerLine)
}
func printSqlHeader(fields []string, table string) {
fieldNames := make([]string, 0)
for _, f := range fields { fieldNames = append(fieldNames, "`" + f + "`") }
printLine(fmt.Sprintf("INSERT INTO %s(%s)", table, strings.Join(fieldNames, ", ")))
logUtils.PrintLine(fmt.Sprintf("INSERT INTO %s(%s)", table, strings.Join(fieldNames, ", ")))
}
func printJsonHeader() {
printLine("[")
logUtils.PrintLine("[")
}
func printXmlHeader(fields []string, table string) {
printLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testdata>\n <title>Test Data</title>")
logUtils.PrintLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testdata>\n <title>Test Data</title>")
}
func RowToJson(cols []string, fieldsToExport []string) string {
......@@ -248,27 +240,4 @@ func getValForPlaceholder(placeholderStr string, count int) []string {
}
return strs
}
func PrintErrMsg(msg string) {
logUtils.PrintToWithColor(msg, color.FgCyan)
}
func printLine(line string) {
if FileWriter != nil {
PrintToFile(line)
} else if vari.RunMode == constant.RunModeServerRequest {
PrintToHttp(line)
} else {
PrintToScreen(line)
}
}
func PrintToFile(line string) {
fmt.Fprintln(FileWriter, line)
}
func PrintToHttp(line string) {
fmt.Fprintln(HttpWriter, line)
}
func PrintToScreen(line string) {
fmt.Println(line)
}
\ No newline at end of file
......@@ -3,11 +3,12 @@ package gen
import (
"encoding/json"
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
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"
"os"
"path/filepath"
"strings"
)
......@@ -16,6 +17,13 @@ const (
)
func Decode(defaultFile, configFile, fieldsToExportStr, input, output string) {
if output != "" {
fileUtils.MkDirIfNeeded(filepath.Dir(output))
fileUtils.RemoveExist(output)
logUtils.FileWriter, _ = os.OpenFile(output, os.O_RDWR|os.O_CREATE, 0777)
defer logUtils.FileWriter.Close()
}
vari.DefaultDir = fileUtils.GetAbsDir(defaultFile)
vari.ConfigDir = fileUtils.GetAbsDir(configFile)
......@@ -37,9 +45,7 @@ func Decode(defaultFile, configFile, fieldsToExportStr, input, output string) {
vari.JsonResp = string(jsonObj)
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("analyse_success", output ))
if vari.RunMode != constant.RunModeServerRequest {
fileUtils.WriteFile(output, vari.JsonResp)
}
logUtils.PrintLine(vari.JsonResp)
}
func LinesToMap(str string, fieldsToExport []string, ret *[]map[string]interface{}) {
......
......@@ -3,9 +3,11 @@ package logUtils
import (
"fmt"
commonUtils "github.com/easysoft/zendata/src/utils/common"
constant "github.com/easysoft/zendata/src/utils/const"
fileUtils "github.com/easysoft/zendata/src/utils/file"
"github.com/easysoft/zendata/src/utils/vari"
"github.com/fatih/color"
"net/http"
"os"
"regexp"
"strings"
......@@ -14,6 +16,9 @@ import (
var (
exampleFile = fmt.Sprintf("res%sen%ssample.yaml", string(os.PathSeparator), string(os.PathSeparator))
usageFile = fmt.Sprintf("res%sen%susage.txt", string(os.PathSeparator), string(os.PathSeparator))
FileWriter *os.File
HttpWriter http.ResponseWriter
)
func PrintExample() {
......@@ -69,3 +74,26 @@ func PrintToWithColor(msg string, attr color.Attribute) {
color.New(attr).Fprintf(output, msg+"\n")
}
}
func PrintErrMsg(msg string) {
PrintToWithColor(msg, color.FgCyan)
}
func PrintLine(line string) {
if FileWriter != nil {
PrintToFile(line)
} else if vari.RunMode == constant.RunModeServerRequest {
PrintToHttp(line)
} else {
PrintToScreen(line)
}
}
func PrintToFile(line string) {
fmt.Fprintln(FileWriter, line)
}
func PrintToHttp(line string) {
fmt.Fprintln(HttpWriter, line)
}
func PrintToScreen(line string) {
fmt.Println(line)
}
......@@ -190,8 +190,8 @@ func toGen() {
if output != "" {
fileUtils.MkDirIfNeeded(filepath.Dir(output))
fileUtils.RemoveExist(output)
action.FileWriter, _ = os.OpenFile(output, os.O_RDWR | os.O_CREATE, 0777)
defer action.FileWriter.Close()
logUtils.FileWriter, _ = os.OpenFile(output, os.O_RDWR | os.O_CREATE, 0777)
defer logUtils.FileWriter.Close()
ext := strings.ToLower(path.Ext(output))
if len(ext) > 1 {
......@@ -203,7 +203,7 @@ func toGen() {
}
if format == constant.FormatSql && table == "" {
action.PrintErrMsg(i118Utils.I118Prt.Sprintf("miss_table_name"))
logUtils.PrintErrMsg(i118Utils.I118Prt.Sprintf("miss_table_name"))
return
}
......@@ -232,14 +232,13 @@ func StartServer() {
}
func DataHandler(writer http.ResponseWriter, req *http.Request) {
action.HttpWriter = writer
logUtils.HttpWriter = writer
defaultFile, configFile, fields, count,
format, table, decode, input, output = service.ParseRequestParams(req)
if decode {
gen.Decode(defaultFile, configFile, fields, input, output)
fmt.Fprintln(writer, vari.JsonResp)
} else if defaultFile != "" || configFile != "" {
vari.RunMode = constant.RunModeServerRequest
logUtils.PrintToWithoutNewLine(i118Utils.I118Prt.Sprintf("server_request", req.Method, req.URL))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册