Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #30

Merged
merged 4 commits into from
Dec 9, 2021
Merged

Dev #30

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 80 additions & 22 deletions src/action/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,39 +108,97 @@ func Generate2(files []string, fieldsToExportStr, format, table string) (lines [
if err != nil {
return
}
lines, count = handleRows(rows, colIsNumArr, fieldsToExport, format, table)
//if format == constant.FormatExcel || format == constant.FormatCsv { // for excel and cvs
// gen.Write(rows, table, colIsNumArr, fieldsToExport)
//} else { // returned is for preview, sql exec and article writing
// lines = gen.Print(rows, format, table, colIsNumArr, fieldsToExport)
//}
//
//// exec insert sql
//if vari.DBDsn != "" {
// helper.ExecSqlInUserDB(lines)
//}
//
//// article need to write to more than one files
//if format == constant.FormatText && vari.Def.Type == constant.ConfigTypeArticle {
// var filePath = logUtils.FileWriter.Name()
// defer logUtils.FileWriter.Close()
// fileUtils.RmFile(filePath)
//
// for index, line := range lines {
// articlePath := fileUtils.GenArticleFiles(filePath, index)
// fileWriter, _ := os.OpenFile(articlePath, os.O_RDWR|os.O_CREATE, 0777)
// fmt.Fprint(fileWriter, line)
// fileWriter.Close()
// }
//}
//
//count = len(rows)
}

if format == constant.FormatExcel || format == constant.FormatCsv { // for excel and cvs
gen.Write(rows, table, colIsNumArr, fieldsToExport)
} else { // returned is for preview, sql exec and article writing
lines = gen.Print(rows, format, table, colIsNumArr, fieldsToExport)
}
entTime := time.Now().Unix()
if vari.RunMode == constant.RunModeServerRequest {
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("server_response", count, entTime-startTime))
}

// exec insert sql
if vari.DBDsn != "" {
helper.ExecSqlInUserDB(lines)
}
return
}

// article need to write to more than one files
if format == constant.FormatText && vari.Def.Type == constant.ConfigTypeArticle {
var filePath = logUtils.FileWriter.Name()
defer logUtils.FileWriter.Close()
fileUtils.RmFile(filePath)
func GenerateByContent(contents [][]byte, fieldsToExportStr, format, table string) (lines []interface{}) {
startTime := time.Now().Unix()

for index, line := range lines {
articlePath := fileUtils.GenArticleFiles(filePath, index)
fileWriter, _ := os.OpenFile(articlePath, os.O_RDWR|os.O_CREATE, 0777)
fmt.Fprint(fileWriter, line)
fileWriter.Close()
}
}
if contents == nil {
return
}

count = len(rows)
count := 0

fieldsToExport := make([]string, 0)
if fieldsToExportStr != "" {
fieldsToExport = strings.Split(fieldsToExportStr, ",")
}

rows, colIsNumArr, err := gen.GenerateFromContent(contents, &fieldsToExport)
if err != nil {
return
}
lines, count = handleRows(rows, colIsNumArr, fieldsToExport, format, table)

entTime := time.Now().Unix()
if vari.RunMode == constant.RunModeServerRequest {
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("server_response", count, entTime-startTime))
}

return
}

func handleRows(rows [][]string, colIsNumArr []bool, fieldsToExport []string, format, table string) (lines []interface{}, count int) {
if format == constant.FormatExcel || format == constant.FormatCsv { // for excel and cvs
gen.Write(rows, table, colIsNumArr, fieldsToExport)
} else { // returned is for preview, sql exec and article writing
lines = gen.Print(rows, format, table, colIsNumArr, fieldsToExport)
}

// exec insert sql
if vari.DBDsn != "" {
helper.ExecSqlInUserDB(lines)
}

// article need to write to more than one files
if format == constant.FormatText && vari.Def.Type == constant.ConfigTypeArticle {
var filePath = logUtils.FileWriter.Name()
defer logUtils.FileWriter.Close()
fileUtils.RmFile(filePath)

for index, line := range lines {
articlePath := fileUtils.GenArticleFiles(filePath, index)
fileWriter, _ := os.OpenFile(articlePath, os.O_RDWR|os.O_CREATE, 0777)
fmt.Fprint(fileWriter, line)
fileWriter.Close()
}
}

count = len(rows)
return
}
14 changes: 9 additions & 5 deletions src/gen/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ import (
"strings"
)

func LoadContentDef(files []string, fieldsToExport *[]string) (ret model.DefData) {

func LoadDataContentDef(filesContents [][]byte, fieldsToExport *[]string) (ret model.DefData) {
ret = model.DefData{}
for _, f := range files {
right := LoadDef2([]byte(f))
for _, f := range filesContents {
right := LoadContentDef(f)

ret = MergeDef(ret, right, fieldsToExport)
}

return
}

func LoadDef2(content []byte) (ret model.DefData) {

func LoadContentDef(content []byte) (ret model.DefData) {
content = stringUtils.ReplaceSpecialChars(content)
err := yaml.Unmarshal(content, &ret)
if err != nil {
logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_parse_file", "D:\\GoProject\\src\\github.com\\Hind3ight\\zendata\\tmp\\.default.yaml"), color.FgCyan)
logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_parse_file", vari.WorkDir+"tmp\\.default.yaml"), color.FgCyan)

return
}

Expand Down
38 changes: 37 additions & 1 deletion src/gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,37 @@ import (
"time"
)

func GenerateFromYaml(files []string, fieldsToExport *[]string, isContent bool) (

func GenerateFromContent(fileContents [][]byte, fieldsToExport *[]string) (
rows [][]string, colIsNumArr []bool, err error) {

vari.Def = LoadDataContentDef(fileContents, fieldsToExport)
vari.ConfigFileDir = vari.WorkDir + "demo\\"

if len(vari.Def.Fields) == 0 {
err = errors.New("")
return
} else if vari.Def.Type == constant.ConfigTypeArticle && vari.Out == "" {
errMsg := i118Utils.I118Prt.Sprintf("gen_article_must_has_out_param")
logUtils.PrintErrMsg(errMsg)
err = errors.New(errMsg)
return
}

if vari.Total < 0 {
if vari.Def.Type == constant.ConfigTypeArticle {
vari.Total = 1
} else {
vari.Total = constant.DefaultNumber
}
}

rows, colIsNumArr, err = Generate(fieldsToExport)
return
}

func GenerateFromYaml(files []string, fieldsToExport *[]string) (

rows [][]string, colIsNumArr []bool, err error) {

if isContent {
Expand Down Expand Up @@ -54,6 +84,12 @@ func GenerateFromYaml(files []string, fieldsToExport *[]string, isContent bool)
vari.Res = LoadResDef(*fieldsToExport)
vari.ResLoading = false

rows, colIsNumArr, err = Generate(fieldsToExport)

return
}

func Generate(fieldsToExport *[]string) (rows [][]string, colIsNumArr []bool, err error) {
// 迭代fields生成值列表
topLevelFieldNameToValuesMap := map[string][]string{}
for index, field := range vari.Def.Fields {
Expand Down
49 changes: 32 additions & 17 deletions src/server/utils/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package serverUtils

import (
constant "github.com/easysoft/zendata/src/utils/const"
fileUtils "github.com/easysoft/zendata/src/utils/file"
"github.com/easysoft/zendata/src/utils/vari"
"net/http"
"strconv"
"strings"
Expand All @@ -26,24 +24,41 @@ func ParseGenParams(req *http.Request) (defaultFile, configFile, fields string,
format = constant.FormatJson
table = ""

if req.Method == http.MethodPost {
req.ParseForm()
trimStr = strings.ToLower(strings.TrimSpace(trimStr))
if trimStr == "t" || trimStr == "true" {
trim = true
}

defaultDefContent := ParserPostParams(req, "default", "d", "", true)
configDefContent := ParserPostParams(req, "config", "c", "", true)
trimStr = ParserPostParams(req, "trim", "T", trimStr, false)
countStr = ParserPostParams(req, "lines", "n", countStr, false)

if defaultDefContent != "" {
defaultFile = vari.ZdPath + "tmp" + constant.PthSep + ".default.yaml"
fileUtils.WriteFile(defaultFile, defaultDefContent)
}
if configDefContent != "" {
configFile = vari.ZdPath + "tmp" + constant.PthSep + ".config.yaml"
fileUtils.WriteFile(configFile, configDefContent)
}
countFromForm, err := strconv.Atoi(countStr)
if err == nil {
count = countFromForm
}

return
}

func ParseGenParamsToByte(req *http.Request) (defaultDefContent, configDefContent []byte, fields string, count int,
format string, trim bool, table string, decode bool, input, output string) {
query := req.URL.Query()

trimStr := ParserGetParams(query, "trim", "T")
countStr := ParserGetParams(query, "lines", "n")
if countStr == "" {
countStr = "10"
}

fields = ParserGetParams(query, "field", "F")

format = constant.FormatJson
table = ""

req.ParseForm()

defaultDefContent = ParserPostParamsToByte(req, "default", "d", "", true)
configDefContent = ParserPostParamsToByte(req, "config", "c", "", true)
trimStr = string(ParserPostParamsToByte(req, "trim", "T", trimStr, false))
countStr = string(ParserPostParamsToByte(req, "lines", "n", countStr, false))

trimStr = strings.ToLower(strings.TrimSpace(trimStr))
if trimStr == "t" || trimStr == "true" {
trim = true
Expand Down
28 changes: 16 additions & 12 deletions src/server/utils/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func ParserGetParams(values url.Values, name, short string) (val string) {
return val
}

func ParserPostParams(req *http.Request, paramName, paramNameShort string, dft string, isFile bool) (ret string) {
if paramNameShort != "" && req.FormValue(paramNameShort) != "" {
ret = req.FormValue(paramNameShort)
} else if paramName != "" && req.FormValue(paramName) != "" { // high priority than paramNameShort
ret = req.FormValue(paramName)
func ParserPostParams(req *http.Request, paramName1, paramName2 string, dft string, isFile bool) (ret string) {
if paramName2 != "" && req.FormValue(paramName2) != "" {
ret = req.FormValue(paramName2)
} else if paramName1 != "" && req.FormValue(paramName1) != "" { // high priority than paramName2
ret = req.FormValue(paramName1)
}

if isFile && ret == "" {
Expand Down Expand Up @@ -85,16 +85,18 @@ func ParserPostParams(req *http.Request, paramName, paramNameShort string, dft s

return
}
func ParserPostParamsToByte(req *http.Request, paramName, paramNameShort string, dft string, isFile bool) (ret []byte) {

func ParserPostParamsToByte(req *http.Request, paramName1, paramName2 string, dft string, isFile bool) (ret []byte) {
value := ""
if paramNameShort != "" && req.FormValue(paramNameShort) != "" {
value = req.FormValue(paramNameShort)
} else if paramName != "" && req.FormValue(paramName) != "" { // high priority than paramNameShort
value = req.FormValue(paramName)
if paramName2 != "" && req.FormValue(paramName2) != "" {
value = req.FormValue(paramName2)
} else if paramName1 != "" && req.FormValue(paramName1) != "" { // high priority than paramName2
value = req.FormValue(paramName1)
}

if isFile && value == "" {
postFile, _, _ := req.FormFile(paramNameShort)
postFile, _, _ := req.FormFile(paramName2)

if postFile != nil {
defer postFile.Close()

Expand All @@ -104,7 +106,9 @@ func ParserPostParamsToByte(req *http.Request, paramName, paramNameShort string,
}

if ret == nil {
postFile, _, _ = req.FormFile(paramName)

postFile, _, _ = req.FormFile(paramName1)

if postFile != nil {
defer postFile.Close()

Expand Down
26 changes: 21 additions & 5 deletions src/zd.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,14 @@ func toGen(files []string) {

} else if vari.RunMode == constant.RunModeServerRequest {
// use the files from post data
// todo 直接从流中读取,不生成临时文件
vari.Format = constant.FormatJson
//action.Generate(files, fields, vari.Format, vari.Table)
action.Generate2(files, fields, vari.Format, vari.Table)
if defaultFile != "" || configFile != "" {
files := []string{defaultFile, configFile}
action.Generate(files, fields, vari.Format, vari.Table)
} else {
contents := [][]byte{defaultDefContent, configDefContent}
action.GenerateByContent(contents, fields, vari.Format, vari.Table)
}

} else if vari.RunMode == constant.RunModeGen {
if vari.Human {
Expand Down Expand Up @@ -313,13 +317,25 @@ func Handler(s *server.Server) http.Handler {
func DataHandler(writer http.ResponseWriter, req *http.Request) {
logUtils.HttpWriter = writer

defaultDefContent, configDefContent, fields, vari.Total,
vari.Format, vari.Trim, vari.Table, decode, input, vari.Out = serverUtils.ParseGenParamsToByte(req)
if req.Method == http.MethodGet {
defaultFile, configFile, fields, vari.Total,
vari.Format, vari.Trim, vari.Table, decode, input, vari.Out = serverUtils.ParseGenParams(req)
} else if req.Method == http.MethodPost {
defaultDefContent, configDefContent, fields, vari.Total,
vari.Format, vari.Trim, vari.Table, decode, input, vari.Out = serverUtils.ParseGenParamsToByte(req)
}


if decode {
files := []string{defaultFile, configFile}
gen.Decode(files, fields, input)
} else if defaultDefContent != nil || configDefContent != nil {

vari.RunMode = constant.RunModeServerRequest
logUtils.PrintToWithoutNewLine(i118Utils.I118Prt.Sprintf("server_request", req.Method, req.URL))
toGen(nil)
} else if defaultFile != "" || configFile != "" {

vari.RunMode = constant.RunModeServerRequest
logUtils.PrintToWithoutNewLine(i118Utils.I118Prt.Sprintf("server_request", req.Method, req.URL))
files := []string{string(defaultDefContent), string(configDefContent)}
Expand Down