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

close task#7786

上级 d1a562ca
...@@ -24,5 +24,6 @@ require ( ...@@ -24,5 +24,6 @@ require (
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc // indirect golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc // indirect
golang.org/x/sys v0.0.0-20200819171115-d785dc25833f // indirect golang.org/x/sys v0.0.0-20200819171115-d785dc25833f // indirect
gopkg.in/ini.v1 v1.60.0 gopkg.in/ini.v1 v1.60.0
gopkg.in/yaml.v2 v2.2.8
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
) )
...@@ -107,4 +107,20 @@ func (def *DefSimple) Init(tableName, author, desc, version string) { ...@@ -107,4 +107,20 @@ func (def *DefSimple) Init(tableName, author, desc, version string) {
} }
func (fld *FieldSimple) Init(field string) { func (fld *FieldSimple) Init(field string) {
fld.Field = field fld.Field = field
}
type DefExport struct {
ClsBase `yaml:",inline"`
XFields []DefFieldExport `yaml:"xfields,flow"` // control orders
}
type DefFieldExport struct {
Field string `yaml:"field"`
Prefix string `yaml:"prefix"`
Postfix string `yaml:"postfix"`
Select string `yaml:"select"`
Where string `yaml:"where"`
Rand bool `yaml:"rand"`
Limit int `yaml:"limit"`
} }
\ No newline at end of file
...@@ -36,7 +36,7 @@ func getFilesInDir(folder, ext string, files *[]string) { ...@@ -36,7 +36,7 @@ func getFilesInDir(folder, ext string, files *[]string) {
filePath := fileUtils.AddSepIfNeeded(folder) + name filePath := fileUtils.AddSepIfNeeded(folder) + name
if fi.IsDir() { if fi.IsDir() {
getFilesInDir(filePath, ext, files) getFilesInDir(filePath, ext, files)
} else if strings.Index(name, "~") != 0 && path.Ext(filePath) == ".xlsx" { } else if strings.Index(name, "~") != 0 && path.Ext(filePath) == ext {
*files = append(*files, filePath) *files = append(*files, filePath)
} }
} }
......
...@@ -6,8 +6,8 @@ import ( ...@@ -6,8 +6,8 @@ import (
"github.com/360EntSecGroup-Skylar/excelize/v2" "github.com/360EntSecGroup-Skylar/excelize/v2"
constant "github.com/easysoft/zendata/src/utils/const" constant "github.com/easysoft/zendata/src/utils/const"
i118Utils "github.com/easysoft/zendata/src/utils/i118" i118Utils "github.com/easysoft/zendata/src/utils/i118"
logUtils "github.com/easysoft/zendata/src/utils/log"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"log"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
...@@ -15,7 +15,7 @@ import ( ...@@ -15,7 +15,7 @@ import (
func TestImportSqlite(t *testing.T) { func TestImportSqlite(t *testing.T) {
files := make([]string, 0) files := make([]string, 0)
getFilesInDir("xdoc/words-9.3", "xlsx", &files) getFilesInDir("xdoc/words-9.3", ".xlsx", &files)
tableName := "words" tableName := "words"
seq := 1 seq := 1
...@@ -33,7 +33,7 @@ func TestImportSqlite(t *testing.T) { ...@@ -33,7 +33,7 @@ func TestImportSqlite(t *testing.T) {
dropSql := `DROP TABLE IF EXISTS ` + tableName + `;` dropSql := `DROP TABLE IF EXISTS ` + tableName + `;`
_, err = db.Exec(dropSql) _, err = db.Exec(dropSql)
if err != nil { if err != nil {
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_drop_table", tableName, err.Error())) log.Println(i118Utils.I118Prt.Sprintf("fail_to_drop_table", tableName, err.Error()))
return return
} }
...@@ -44,14 +44,14 @@ func TestImportSqlite(t *testing.T) { ...@@ -44,14 +44,14 @@ func TestImportSqlite(t *testing.T) {
ddlSql := fmt.Sprintf(ddlTemplate, strings.Join(ddlFields, ", \n")) ddlSql := fmt.Sprintf(ddlTemplate, strings.Join(ddlFields, ", \n"))
_, err = db.Exec(ddlSql) _, err = db.Exec(ddlSql)
if err != nil { if err != nil {
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_create_table", tableName, err.Error())) log.Println(i118Utils.I118Prt.Sprintf("fail_to_create_table", tableName, err.Error()))
return return
} }
sql := strings.Join(insertSqls, "\n") sql := strings.Join(insertSqls, "\n")
_, err = db.Exec(sql) _, err = db.Exec(sql)
if err != nil { if err != nil {
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_exec_query", sql, err.Error())) log.Println(i118Utils.I118Prt.Sprintf("fail_to_exec_query", sql, err.Error()))
return return
} }
} }
...@@ -59,7 +59,7 @@ func TestImportSqlite(t *testing.T) { ...@@ -59,7 +59,7 @@ func TestImportSqlite(t *testing.T) {
func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[]string, colMap *map[string]bool) { func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[]string, colMap *map[string]bool) {
excel, err := excelize.OpenFile(filePath) excel, err := excelize.OpenFile(filePath)
if err != nil { if err != nil {
logUtils.PrintTo("fail to read file " + filePath + ", error: " + err.Error()) log.Println("fail to read file " + filePath + ", error: " + err.Error())
return return
} }
...@@ -90,7 +90,7 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[] ...@@ -90,7 +90,7 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[]
colName = "ci" colName = "ci"
} }
if colName != "ci" { if colName != "ci" {
colName = colPrefix + ":" + colName colName = colPrefix + "-" + colName
} }
if (*colMap)[colName] == false { if (*colMap)[colName] == false {
......
package main package main
import ( import (
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
fileUtils "github.com/easysoft/zendata/src/utils/file" fileUtils "github.com/easysoft/zendata/src/utils/file"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"gopkg.in/yaml.v3"
"strconv"
"strings"
"testing" "testing"
) )
func TestGenerate(t *testing.T) { const (
strLeft = "“"
strRight = "”"
expLeft = "("
expRight = ")"
)
func TestGenerate(ts *testing.T) {
files := make([]string, 0) files := make([]string, 0)
getFilesInDir("xdoc/words-9.3", ".txt", &files) getFilesInDir("xdoc/words-9.3", ".txt", &files)
...@@ -20,7 +33,129 @@ func TestGenerate(t *testing.T) { ...@@ -20,7 +33,129 @@ func TestGenerate(t *testing.T) {
} }
func convertToYaml(article string) (content string) { func convertToYaml(article string) (content string) {
sections := parseSections(article)
conf := createDef(constant.ConfigTypeArticle, "words.v1")
prefix := ""
for index, section := range sections {
tye := section["type"]
val := section["val"]
if tye == "exp" {
field := createField(index, prefix, val)
conf.XFields = append(conf.XFields, field)
prefix = ""
} else {
prefix += val
}
}
bytes, _ := yaml.Marshal(&conf)
content = string(bytes)
// convert yaml format by using a map
m := make(map[string]interface{})
yaml.Unmarshal([]byte(content), &m)
bytes, _ = yaml.Marshal(&m)
content = string(bytes)
content = strings.Replace(content, "xfields", "\nfields", -1)
return
}
func createDef(typ, table string) (conf model.DefExport) {
conf.Title = "automation"
conf.Author = "zendata"
conf.From = table
conf.Type = typ
conf.Desc = "generated from article text automatically"
return
}
func createField(index int, prefix, exp string) (field model.DefFieldExport) {
field.Field = strconv.Itoa(index)
field.Prefix = prefix
field.Select = getPinyin(exp)
field.Where = "true"
field.Rand = true
field.Limit = 1
return
}
func parseSections(content string) (sections []map[string]string) {
strStart := false
expStart := false
content = strings.TrimSpace(content)
runeArr := []rune(content)
section := ""
for i := 0; i < len(runeArr); i++ {
item := runeArr[i]
str := string(item)
isCouple, duplicateStr := isCouple(i, runeArr)
if isCouple {
section += duplicateStr
i += 1
} else if strStart && str == strRight { // str close
addSection(section, "str", &sections)
strStart = false
section = ""
} else if expStart && str == expRight { // exp close
addSection(section, "exp", &sections)
expStart = false
section = ""
} else if !strStart && !expStart && str == strLeft { // str start
if section != "" && strings.TrimSpace(section) != "+" {
addSection(section, "str", &sections)
}
strStart = true
section = ""
} else if !strStart && !expStart && str == expLeft { // exp start
if section != "" && strings.TrimSpace(section) != "+" {
addSection(section, "str", &sections)
}
expStart = true
section = ""
} else {
section += str
}
}
return
}
func addSection(str, typ string, arr *[]map[string]string) {
mp := map[string]string{}
mp["type"] = typ
mp["val"] = str
*arr = append(*arr, mp)
}
func isCouple(i int, arr []rune) (isCouple bool, duplicateStr string) {
if string(arr[i]) == strLeft && (i + 1 < len(arr) && string(arr[i + 1]) == strLeft) {
isCouple = true
duplicateStr = string(arr[i])
} else if string(arr[i]) == strRight && (i + 1 < len(arr) && string(arr[i + 1]) == strRight) {
isCouple = true
duplicateStr = string(arr[i])
} else if string(arr[i]) == expLeft && (i + 1 < len(arr) && string(arr[i + 1]) == expLeft) {
isCouple = true
duplicateStr = string(arr[i])
} else if string(arr[i]) == expRight && (i + 1 < len(arr) && string(arr[i + 1]) == expRight) {
isCouple = true
duplicateStr = string(arr[i])
}
return return
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册