提交 0d494d7b 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

new features for 1.1

上级 853b87c2
......@@ -10,8 +10,6 @@ import (
logUtils "github.com/easysoft/zendata/src/utils/log"
stringUtils "github.com/easysoft/zendata/src/utils/string"
"github.com/easysoft/zendata/src/utils/vari"
"os"
"path/filepath"
"strings"
)
......@@ -27,8 +25,7 @@ func Generate(defaultFile string, configFile string, total int, fieldsToExportSt
if fieldsToExportStr != "" {
fieldsToExport = strings.Split(fieldsToExportStr, ",")
}
abs, _ := filepath.Abs(filepath.Dir(configFile))
vari.InputDir = abs + string(os.PathSeparator)
constant.Total = total
rows, colTypes := gen.GenerateForDefinition(defaultFile, configFile, &fieldsToExport, total)
......
......@@ -5,19 +5,14 @@ import (
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"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"time"
)
func ParseSql(file string, out string) {
startTime := time.Now().Unix()
abs, _ := filepath.Abs(filepath.Dir(file))
vari.InputDir = abs + string(os.PathSeparator)
statements := getCreateStatement(file)
for tableName, statement := range statements {
......
......@@ -10,10 +10,11 @@ import (
"strings"
)
func LoadRootDef(defaultFile, ymlFile string, fieldsToExport *[]string) model.DefData {
func LoadConfigDef(defaultFile, configFile string, fieldsToExport *[]string) model.DefData {
defaultDef := model.DefData{}
ymlDef := model.DefData{}
configDef := model.DefData{}
// load defaultDef
if defaultFile != "" {
defaultContent, err := ioutil.ReadFile(defaultFile)
defaultContent = ReplaceSpecialChars(defaultContent)
......@@ -28,43 +29,46 @@ func LoadRootDef(defaultFile, ymlFile string, fieldsToExport *[]string) model.De
}
}
yamlContent, err := ioutil.ReadFile(ymlFile)
// load configDef
yamlContent, err := ioutil.ReadFile(configFile)
yamlContent = ReplaceSpecialChars(yamlContent)
if err != nil {
logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_read_file", ymlFile))
return ymlDef
logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_read_file", configFile))
return configDef
}
err = yaml.Unmarshal(yamlContent, &ymlDef)
err = yaml.Unmarshal(yamlContent, &configDef)
if err != nil {
logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_parse_file", ymlFile))
return ymlDef
logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_parse_file", configFile))
return configDef
}
// use all fields as default
if len(*fieldsToExport) == 0 {
for _, field := range ymlDef.Fields {
for _, field := range configDef.Fields {
*fieldsToExport = append(*fieldsToExport, field.Field)
}
}
MergerDefine(&defaultDef, &ymlDef)
MergerDefine(&defaultDef, &configDef)
return defaultDef
}
func MergerDefine(defaultDef, ymlDef *model.DefData) {
func MergerDefine(defaultDef, configDef *model.DefData) {
defaultFieldMap := map[string]*model.DefField{}
ymlFieldMap := map[string]*model.DefField{}
configFieldMap := map[string]*model.DefField{}
sortedKeys := make([]string, 0)
for i := range defaultDef.Fields {
CreatePathToFieldMap(&defaultDef.Fields[i], defaultFieldMap, nil)
}
for i := range ymlDef.Fields {
CreatePathToFieldMap(&ymlDef.Fields[i], ymlFieldMap, &sortedKeys)
for i := range configDef.Fields {
CreatePathToFieldMap(&configDef.Fields[i], configFieldMap, &sortedKeys)
}
for path, field := range ymlFieldMap {
// overwrite
for path, field := range configFieldMap {
parent, exist := defaultFieldMap[path]
if exist {
CopyField(*field, parent)
......@@ -72,9 +76,10 @@ func MergerDefine(defaultDef, ymlDef *model.DefData) {
}
}
// append
for _, key := range sortedKeys {
field := ymlFieldMap[key]
if strings.Index(field.Path, "~~") > -1 { continue } // only for top fields
field := configFieldMap[key]
if strings.Index(field.Path, "~~") > -1 { continue } // ignore no-top fields
_, exist := defaultFieldMap[field.Path]
if !exist {
......
......@@ -4,27 +4,32 @@ import (
"fmt"
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
fileUtils "github.com/easysoft/zendata/src/utils/file"
stringUtils "github.com/easysoft/zendata/src/utils/string"
"github.com/easysoft/zendata/src/utils/vari"
"strconv"
"strings"
)
func GenerateForDefinition(defaultFile, configFile string, fieldsToExport *[]string, total int) ([][]string, []bool) {
constant.Def = LoadRootDef(defaultFile, configFile, fieldsToExport)
constant.Res = LoadResDef(*fieldsToExport)
vari.DefaultDir = fileUtils.GetAbsDir(defaultFile)
vari.ConfigDir = fileUtils.GetAbsDir(configFile)
vari.Def = LoadConfigDef(defaultFile, configFile, fieldsToExport)
vari.Res = LoadResDef(*fieldsToExport)
fieldNameToValues := map[string][]string{}
colTypes := make([]bool, 0)
// 为每个field生成值列表
for index, field := range constant.Def.Fields {
for index, field := range vari.Def.Fields {
if !stringUtils.FindInArr(field.Field, *fieldsToExport) {
continue
}
values := GenerateForField(&field, total, true)
constant.Def.Fields[index].Precision = field.Precision
vari.Def.Fields[index].Precision = field.Precision
fieldNameToValues[field.Field] = values
colTypes = append(colTypes, field.IsNumb)
......@@ -33,7 +38,7 @@ func GenerateForDefinition(defaultFile, configFile string, fieldsToExport *[]str
// 生成指定数量行的数据
rows := make([][]string, 0)
for i := 0; i < total; i++ {
for _, field := range constant.Def.Fields {
for _, field := range vari.Def.Fields {
if !stringUtils.FindInArr(field.Field, *fieldsToExport) {
continue
}
......@@ -66,18 +71,23 @@ func GenerateForField(field *model.DefField, total int, withFix bool) []string {
// should be done by calling LoopSubFields func as below, so disable this line
//concat = field.Prefix + concat + field.Postfix
values = append(values, concat)
}
values = LoopSubFields(field, values, total, true)
} else if field.From != "" { // refer to res
groupValues := constant.Res[field.From]
groupValues := vari.Res[field.From]
if field.Use != "" { // refer to yaml
groups := strings.Split(field.Use, ",")
for _, group := range groups {
values = append(values, groupValues[group]...)
if group == "all" {
for _, arr := range groupValues { // add all
values = append(values, arr...)
}
} else {
values = append(values, groupValues[group]...)
}
}
} else { // refer to excel
slct := field.Select
......
......@@ -12,18 +12,17 @@ import (
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func LoadResDef(fieldsToExport []string) map[string]map[string][]string {
res := map[string]map[string][]string{}
for _, field := range constant.Def.Fields {
for _, field := range vari.Def.Fields {
if !stringUtils.FindInArr(field.Field, fieldsToExport) { continue }
loadResField(&field, &res)
}
return res
}
......@@ -62,22 +61,27 @@ func getResProp(from string) (string, string) { // from resource
resType = "excel"
}
if strings.Index(resFile, "system") == -1 { // no system resource
resPath := vari.WorkDir + resFile
if !fileUtils.FileExist(resPath) { // not in work dir
resPath = vari.InputDir + resFile
if strings.Index(resFile, "system") > -1 { // system resource
resFile = vari.ExeDir + constant.ResDir + resFile
} else {
resPath := resFile
if !filepath.IsAbs(resPath) {
if !fileUtils.FileExist(resPath) { // not in input dir (same dir as yaml file in)
resPath = vari.ExeDir + resFile
resPath = vari.DefaultDir + resFile
if !fileUtils.FileExist(resPath) {
if !fileUtils.FileExist(resPath) { // not in exe dir
resPath = vari.ConfigDir + resFile
if !fileUtils.FileExist(resPath) {
resPath = ""
}
}
} else {
if !fileUtils.FileExist(resPath) {
resPath = ""
}
}
resFile = resPath
} else { // system resource
resFile = constant.ResDir + resFile
}
return resFile, resType
......
......@@ -2,7 +2,6 @@ package constant
import (
"fmt"
"github.com/easysoft/zendata/src/model"
"os"
)
......@@ -28,9 +27,6 @@ var (
Total = 10
MaxNumb = 100000 // max number in array
Def = model.DefData{}
Res = map[string]map[string][]string{}
FormatText = "text"
FormatJson = "json"
FormatXml = "xml"
......@@ -44,7 +40,6 @@ var (
DefaultRoot = "./"
ResDir = "data/"
ResPath = ResDir + "system/buildin.yaml"
SqliteDriver = "sqlite3"
SqliteSource = "file:" + ResDir + ".cache/.data.db"
......
......@@ -163,7 +163,7 @@ func GetWorkDir() string { // where run command in
}
func GetLogDir() string {
path := vari.WorkDir + constant.LogDir
path := vari.ExeDir + constant.LogDir
dir, _ := ioutil.ReadDir(path)
......@@ -234,3 +234,10 @@ func CopyFile(src, dst string) (int64, error) {
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
func GetAbsDir(path string) string {
abs, _ := filepath.Abs(filepath.Dir(path))
abs = UpdateDir(abs)
return abs
}
......@@ -11,8 +11,6 @@ var (
RunMode constant.RunMode
ExeDir string
WorkDir string
InputDir string
LogDir string
ScreenWidth int
......@@ -32,4 +30,10 @@ var (
JsonResp string = "[]"
Ip string
Port int
Def = model.DefData{}
Res = map[string]map[string][]string{}
DefaultDir string
ConfigDir string
)
......@@ -133,16 +133,14 @@ func main() {
func toGen() {
if vari.RunMode == constant.RunModeServer {
vari.ExeDir = fileUtils.GetExeDir()
vari.WorkDir = fileUtils.GetWorkDir()
StartServer()
} else if vari.RunMode == constant.RunModeParse {
action.ParseSql(input, output)
} else if vari.RunMode == constant.RunModeGen {
vari.ExeDir = fileUtils.GetExeDir()
vari.WorkDir = fileUtils.GetWorkDir()
if root != "" {
vari.WorkDir = root
vari.ExeDir = root
}
if vari.HeadSep != "" {
vari.WithHead = true
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册