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

Optimize code #31

Merged
merged 7 commits into from
Dec 9, 2021
Merged
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
129 changes: 11 additions & 118 deletions src/action/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,116 +32,14 @@ func Generate(files []string, fieldsToExportStr, format, table string) (lines []
logUtils.PrintLine(buf)

count = 1

} else { // default gen from yaml
fieldsToExport := make([]string, 0)
if fieldsToExportStr != "" {
fieldsToExport = strings.Split(fieldsToExportStr, ",")
}

rows, colIsNumArr, err := gen.GenerateFromYaml(files, &fieldsToExport, true)
if err != nil {
return
}

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)
}

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

return
}

func Generate2(files []string, fieldsToExportStr, format, table string) (lines []interface{}) {
startTime := time.Now().Unix()

if len(files) == 0 {
return
}

count := 0
if strings.ToLower(filepath.Ext(files[0])) == "."+constant.FormatProto { //gen from protobuf
buf, pth := gen.GenerateFromProtobuf(files[0])

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

count = 1

} else { // default gen from yaml
fieldsToExport := make([]string, 0)
if fieldsToExportStr != "" {
fieldsToExport = strings.Split(fieldsToExportStr, ",")
}

rows, colIsNumArr, err := gen.GenerateFromYaml(files, &fieldsToExport, true)
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)
}

entTime := time.Now().Unix()
if vari.RunMode == constant.RunModeServerRequest {
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("server_response", count, entTime-startTime))
contents := gen.LoadFilesContents(files)
lines = GenerateByContent(contents, fieldsToExportStr, format, table)
}

return
}

Expand All @@ -163,17 +61,6 @@ func GenerateByContent(contents [][]byte, fieldsToExportStr, format, table strin
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
Expand All @@ -200,5 +87,11 @@ func handleRows(rows [][]string, colIsNumArr []bool, fieldsToExport []string, fo
}

count = len(rows)

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

return
}
27 changes: 22 additions & 5 deletions src/gen/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,48 @@ import (
"strings"
)


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

ret = MergeDef(ret, right, fieldsToExport)
}

return
}


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", vari.WorkDir+"tmp\\.default.yaml"), color.FgCyan)

logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_parse_file"), color.FgCyan)
return
}

return
}

func LoadFilesContents(files []string) (contents [][]byte) {
contents = make([][]byte, 0)
for _, f := range files {
if f == "" {
continue
}
pathDefaultFile := fileUtils.GetAbsolutePath(f)
if !fileUtils.FileExist(pathDefaultFile) {
return
}
content, err := ioutil.ReadFile(pathDefaultFile)
if err != nil {
logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_parse_file"), color.FgCyan)
return
}
contents = append(contents, content)
}

return
}

func LoadDataDef(files []string, fieldsToExport *[]string) (ret model.DefData) {
newFiles := make([]string, 0)
for _, f := range files {
Expand Down
84 changes: 13 additions & 71 deletions src/gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,10 @@ import (
"time"
)


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 {
vari.Def = LoadContentDef(files, fieldsToExport)
vari.ConfigFileDir = vari.WorkDir + "demo\\"
} else {
vari.ConfigFileDir = fileUtils.GetAbsDir(files[0])
vari.Def = LoadDataDef(files, fieldsToExport)
}

if len(vari.Def.Fields) == 0 {
err = errors.New("")
Expand All @@ -84,12 +48,6 @@ func GenerateFromYaml(files []string, fieldsToExport *[]string) (
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 Expand Up @@ -165,6 +123,17 @@ func Generate(fieldsToExport *[]string) (rows [][]string, colIsNumArr []bool, er
return
}

func GenerateFromYaml(files []string, fieldsToExport *[]string) (
rows [][]string, colIsNumArr []bool, err error) {

vari.ConfigFileDir = fileUtils.GetAbsDir(files[0])

contents := LoadFilesContents(files)
rows, colIsNumArr, err = GenerateFromContent(contents, fieldsToExport)

return
}

func GenerateForFieldRecursive(field *model.DefField, withFix bool) (values []string) {
if len(field.Fields) > 0 { // has sub fields
fieldNameToValuesMap := map[string][]string{} // refer field name to values
Expand Down Expand Up @@ -236,9 +205,8 @@ func GenerateForFieldRecursive(field *model.DefField, withFix bool) (values []st
valuesForAdd := getRepeatValuesFromAll(groupValues, numLimit, repeat)
values = append(values, valuesForAdd...)
} else {
//valuesForAdd := getRepeatValuesFromGroups(groupValues, use, numLimit, repeat)
infos := parseUse(uses)
valuesForAdd := getRepeatValuesFromGroups2(groupValues, infos)
valuesForAdd := getRepeatValuesFromGroups(groupValues, infos)
values = append(values, valuesForAdd...)
}
} else if field.Select != "" { // refer to excel
Expand Down Expand Up @@ -637,34 +605,8 @@ exit:
return
}

func getRepeatValuesFromGroups(groupValues map[string][]string, use string, numLimit, repeat int) (ret []string) {
if repeat == 0 {
repeat = 1
}

groupNames := strings.Split(use, ",")

count := 0
exit:
for _, groupName := range groupNames {
arr := groupValues[groupName]

for _, item := range arr {
for i := 0; i < repeat; i++ {
ret = append(ret, item)
count++

if numLimit > 0 && count >= numLimit {
break exit
}
}
}
}

return
}

func getRepeatValuesFromGroups2(groupValues map[string][]string, info []retsInfo) (ret []string) {
func getRepeatValuesFromGroups(groupValues map[string][]string, info []retsInfo) (ret []string) {
count := 0

exit:
Expand Down
Loading