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

new definition format

上级 b89db667
......@@ -6,9 +6,9 @@ import (
"fmt"
"github.com/easysoft/zendata/src/gen"
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
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"
"strconv"
......@@ -18,13 +18,11 @@ import (
func Generate(def string, total int, fieldsToExportStr string, out string, format string, table string) {
startTime := time.Now().Unix()
vari.InputDir = filepath.Dir(def) + string(os.PathSeparator)
fieldsToExport := strings.Split(fieldsToExportStr, ",")
constant.InputDir = filepath.Dir(def) + string(os.PathSeparator)
referRangeFields, referInstFields := gen.LoadDefinitionFromFile(def, fieldsToExport) // init constant.RootDef
gen.LoadReferRes(referRangeFields, referInstFields) // init const.ResMap
gen.LoadRootDef(def, fieldsToExport)
rows, colTypes := gen.GenerateForDefinition(total, fieldsToExport)
content := Print(rows, format, table, colTypes, fieldsToExport)
......
......@@ -7,10 +7,11 @@ import (
stringUtils "github.com/easysoft/zendata/src/utils/string"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strings"
)
func LoadDefinitionFromFile(file string, fieldsToExport []string) ([]model.ClsRange, []model.ClsInst) {
func LoadRootDef(file string, fieldsToExport []string) ([]model.ClsRange, []model.ClsInst) {
referRangeFields := make([]model.ClsRange, 0)
referInstFields := make([]model.ClsInst, 0)
......@@ -27,27 +28,54 @@ func LoadDefinitionFromFile(file string, fieldsToExport []string) ([]model.ClsRa
return referRangeFields, referInstFields
}
if strings.Index(file, "def") != 0 && constant.RootDef.Title == "" { // only add the fields in first level yaml file
constant.RootDef = def
}
constant.RootDef = def
//constant.ResMap =
for _, field := range def.Fields {
if !stringUtils.FindInArr(field.Field, fieldsToExport) { continue }
// TODO: dealwith referRangeFields and referInstFields for constant.ResMap
if field.From != "" {
if field.Select != "" { // excel
} else if field.Use != "" { // range or instance format
//referFile, referType := getReferPath(field.From)
// init const.ResMap
}
}
// TODO:
}
return referRangeFields, referInstFields
}
func LoadReferRes([]model.ClsRange, []model.ClsInst) {
// init const.ResMap
func getReferPath(from string) (string, string, string) {
referFile := ""
referType := ""
tableName := ""
sep := string(os.PathSeparator)
index := strings.LastIndex(from, ".yaml")
if index > -1 { // system.nubmer.yaml
left := from[:index]
left = strings.ReplaceAll(left, ".", sep)
//for _, field := range def.Fields {
// fieldValue := model.FieldValue{}
// // TODO: 生成fieldValue
// constant.ResMap[field.Field] = fieldValue // add to a map
//}
}
referFile = left + ".yaml"
} else { // system.address.china
index = strings.LastIndex(from, ".")
left := from[:index]
left = strings.ReplaceAll(left, ".", sep)
referFile = left + ".xlsx"
tableName = from[index:]
}
if strings.Index(referFile, "system") > -1 {
referFile = constant.ResDir + referFile
}
return referFile, referType, tableName
}
\ No newline at end of file
......@@ -86,7 +86,7 @@ func GenerateForField(field *model.DefField, total int) []string {
} else if field.From != "" && field.Range != "" { // refer to yaml file
if field.Range != "" { // specific custom file
//LoadDefinitionFromFile(constant.InputDir + field.Range)
//LoadRootDef(constant.InputDir + field.Range)
}
//referField := constant.ResMap[field.Field]
......
......@@ -4,6 +4,7 @@ import (
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
logUtils "github.com/easysoft/zendata/src/utils/log"
"github.com/easysoft/zendata/src/utils/vari"
"io/ioutil"
"os"
"strconv"
......@@ -18,9 +19,9 @@ func GenerateFieldValuesFromText(field *model.DefField, fieldValue *model.FieldV
stepStr := "1"
if len(sectionArr) == 2 { stepStr = sectionArr[1] }
// read from file
// read frome
list := make([]string, 0)
relaPath := constant.InputDir + file
relaPath := vari.InputDir + file
content, err := ioutil.ReadFile(relaPath)
if err != nil {
logUtils.Screen("fail to read " + relaPath + ", try to use global config")
......
......@@ -18,10 +18,11 @@ import (
)
func InitConfig() {
vari.ZDataDir = fileUtils.GetZTFDir()
vari.WorkDir = fileUtils.GetExeDir()
CheckConfigPermission()
constant.ConfigFile = vari.ZDataDir + constant.ConfigFile
constant.ConfigFile = vari.WorkDir + constant.ConfigFile
vari.Config = getInst()
InitScreenSize()
......@@ -36,7 +37,7 @@ func InitScreenSize() {
}
func SaveConfig(conf model.Config) error {
fileUtils.MkDirIfNeeded(fileUtils.GetZTFDir() + "def")
fileUtils.MkDirIfNeeded(fileUtils.GetExeDir() + "def")
conf.Version = constant.ConfigVer
......@@ -103,12 +104,12 @@ func getInst() model.Config {
}
func CheckConfigPermission() {
//err := syscall.Access(vari.ZDataDir, syscall.O_RDWR)
//err := syscall.Access(vari.WorkDir, syscall.O_RDWR)
err := fileUtils.MkDirIfNeeded(vari.ZDataDir + "def")
err := fileUtils.MkDirIfNeeded(vari.WorkDir + "def")
if err != nil {
logUtils.PrintToWithColor(
fmt.Sprintf("Permission denied to open %s for write. Please change work dir.", vari.ZDataDir), color.FgRed)
fmt.Sprintf("Permission denied to open %s for write. Please change work dir.", vari.WorkDir), color.FgRed)
os.Exit(0)
}
}
......
......@@ -27,9 +27,6 @@ var (
MaxNumb = 100000 // max number in array
WorkDir string = ""
InputDir string = ""
RootDef model.DefData = model.DefData{}
ResMap map[string]model.FieldValue = map[string]model.FieldValue{}
......
......@@ -133,7 +133,7 @@ func ReadResData(path string) string {
return jsonStr
}
func GetZTFDir() string { // where ztf command in
func GetExeDir() string { // where ztf command in
var dir string
arg1 := strings.ToLower(os.Args[0])
......@@ -155,7 +155,7 @@ func GetZTFDir() string { // where ztf command in
}
func GetLogDir() string {
path := vari.ZDataDir + constant.LogDir
path := vari.WorkDir + constant.LogDir
dir, _ := ioutil.ReadDir(path)
......
......@@ -8,10 +8,11 @@ import (
var (
Config = model.Config{}
RunMode constant.RunMode
ZDataDir string
LogDir string
ScreenWidth int
RunMode constant.RunMode
WorkDir string
InputDir string
LogDir string
ScreenWidth int
ScreenHeight int
RequestType string
......
......@@ -37,7 +37,7 @@ fields:
- field: field6
note: 自定义的数据类型。可以通过@tag的方式对数据进行筛选。
from: system.nubmer.yaml
range: small,large
use: small,large
- field: field 7
note: 自定义的数据类型,包含子字段。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册