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

new features for 1.1

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