diff --git a/data/.cache/.data.db b/data/.cache/.data.db index 0d8d21106bcda3bb442c298e4c95d5e392cb98f1..16d7fbf3c3e0dc460df80f328ee36eec8fe6ba8b 100644 Binary files a/data/.cache/.data.db and b/data/.cache/.data.db differ diff --git a/data/chinese/words/v1.xlsx b/data/chinese/words/v1.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..984922e68d5b1d516fbfcdf5db5a5e7dcb3c2d2e Binary files /dev/null and b/data/chinese/words/v1.xlsx differ diff --git a/demo/chinese.yaml b/demo/chinese.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b4f91e3972be6ab89f2ffacfdf806059050ee14c --- /dev/null +++ b/demo/chinese.yaml @@ -0,0 +1,22 @@ +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' diff --git a/res/en/sample.yaml b/res/en/sample.yaml index b3727d4a33ad817ff61d2745170066f620f4d445..28c82fc6e3cdded8c76a9bd21020fa54c2eb1cf9 100644 --- a/res/en/sample.yaml +++ b/res/en/sample.yaml @@ -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. diff --git a/res/zh/sample.yaml b/res/zh/sample.yaml index fdcc899634b7b124bb8618410917aba8769b05b1..a8176d85f76e9ac9fab1c8ad7b0261bd8122f811 100644 --- a/res/zh/sample.yaml +++ b/res/zh/sample.yaml @@ -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 # 通过格式化字符串输出。 diff --git a/src/action/generator.go b/src/action/generator.go index 2ffd7d13007039a525a74f9aede59feedef528f8..75b30692df7e4389175cdcf380e67b2e4ccf5957 100644 --- a/src/action/generator.go +++ b/src/action/generator.go @@ -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("\n\n Test Data") + logUtils.PrintLine("\n\n Test Data") } 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 diff --git a/src/gen/decode.go b/src/gen/decode.go index 634e9568b0ab25e810e9f3babde2faf925be3bc4..f33e02280b733e48008513015974a0c6e80f82bd 100644 --- a/src/gen/decode.go +++ b/src/gen/decode.go @@ -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{}) { diff --git a/src/utils/log/print.go b/src/utils/log/print.go index 084e39d4d547605b0130c7b45a24366e49240030..d7f99be471d15df900c13cd6711bf5c522fb955f 100644 --- a/src/utils/log/print.go +++ b/src/utils/log/print.go @@ -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) +} diff --git a/src/zd.go b/src/zd.go index 07570853b6df6ce8ed818b4e3f38b41d58febd74..1120ed35375181002f8e345d4ec28d62ba390689 100644 --- a/src/zd.go +++ b/src/zd.go @@ -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))