diff --git a/data/.cache/.data.db b/data/.cache/.data.db index 16d7fbf3c3e0dc460df80f328ee36eec8fe6ba8b..772ead6900f13523d48983b7f93d2a327f5a1a4d 100644 Binary files a/data/.cache/.data.db and b/data/.cache/.data.db differ diff --git a/data/address/cn.v1.xlsx b/data/address/cn.v1.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..0d8e94c1d1d8fe0443a24ba90bbabe33f483ad53 Binary files /dev/null and b/data/address/cn.v1.xlsx differ diff --git a/data/system/address/v1.xlsx b/data/system/address/v1.xlsx deleted file mode 100644 index efb197f754c2fdad379b678c1ea5a7dc8c46206a..0000000000000000000000000000000000000000 Binary files a/data/system/address/v1.xlsx and /dev/null differ diff --git a/demo/advanced.yaml b/demo/advanced.yaml index c0e8f326b8125568bf9f6a32a75bb3ec22955288..ce8600305d5629b8b78fa07a8c006b88fbede091 100644 --- a/demo/advanced.yaml +++ b/demo/advanced.yaml @@ -36,4 +36,7 @@ fields: use: small{2} postfix: "\t" - from: custom.test.number.v1.yaml - use: large{3} \ No newline at end of file + use: large{3} + + - field: field_blank + range: [user-1,] \ No newline at end of file diff --git a/demo/default.yaml b/demo/default.yaml index 9299b0ca8013ba3c04a7100e59b5cba005828dd0..1d8a3c2dbd2d762cc8e08cf3df3863fde93b29f3 100644 --- a/demo/default.yaml +++ b/demo/default.yaml @@ -118,7 +118,7 @@ fields: postfix: "}" - field: field_use_excel # 从excel数据源里面取数据。 - from: system.address.v1.china # 从data/system/address/v1.xlsx文件中读取名为china的工作簿。 + from: address.cn.v1.china # 从data/address/cn.v1.xlsx文件中读取名为china的工作表。 select: city # 查询city字段。 where: state like '%山东%' # 条件是省份包含山东。 limit: 10 diff --git a/src/gen/excel.go b/src/gen/excel.go index dc4c08097f0e55b580337fb867aeb4e4581dfd48..eeab183664d185e12a5de579699e04e8699f8133 100644 --- a/src/gen/excel.go +++ b/src/gen/excel.go @@ -15,20 +15,19 @@ import ( "time" ) -func GenerateFieldValuesFromExcel(path string, field *model.DefField) (map[string][]string, string) { +func GenerateFieldValuesFromExcel(path, sheet string, field *model.DefField) (map[string][]string, string) { values := map[string][]string{} - idx := strings.LastIndex(field.From, ".") - tableName := field.From[idx + 1:] - - arr := strings.Split(field.From, ".") - dbName := arr[len(arr) - 3] + "_" + arr[len(arr) - 2] + dbName := getDbName(path) list := make([]string, 0) selectCol := "" - ConvertExcelToSQLiteIfNeeded(dbName, path) + firstSheet := ConvertExcelToSQLiteIfNeeded(dbName, path) + if sheet == "" { + sheet = firstSheet + } - list, selectCol = ReadDataFromSQLite(*field, dbName, tableName) + list, selectCol = ReadDataFromSQLite(*field, dbName, sheet) // get index for data retrieve numbs := GenerateIntItems(0, (int64)(len(list)-1), 1, false, 1) // get data by index @@ -50,13 +49,22 @@ func GenerateFieldValuesFromExcel(path string, field *model.DefField) (map[strin return values, dbName } -func ConvertExcelToSQLiteIfNeeded(dbName string, path string) { +func getDbName(path string) (dbName string) { + dbName = strings.Replace(path, vari.WorkDir + constant.ResDir, "", -1) + dbName = strings.Replace(dbName, constant.PthSep, "_", -1) + dbName = strings.Replace(dbName, ".", "_", -1) + + return +} + +func ConvertExcelToSQLiteIfNeeded(dbName string, path string) (firstSheet string) { excel, err := excelize.OpenFile(path) if err != nil { logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_read_file", path)) return } + firstSheet = excel.GetSheetList()[0] if !isExcelChanged(path) { return } @@ -148,6 +156,8 @@ func ConvertExcelToSQLiteIfNeeded(dbName string, path string) { return } } + + return } func ReadDataFromSQLite(field model.DefField, dbName string, tableName string) ([]string, string) { diff --git a/src/gen/res.go b/src/gen/res.go index a21e37b07b2dfff9122f068ea205ccde7af6149b..b80e6c621d698dab13e3b22a84f22cc5e0f92621 100644 --- a/src/gen/res.go +++ b/src/gen/res.go @@ -47,19 +47,17 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) { loadResField(&child, res) } } else if field.From != "" { - resFile, resType := getResProp(field.From) - values, _ := getResValue(resFile, resType, field) + resFile, resType, sheet := getResProp(field.From) + values, _ := getResValue(resFile, resType, sheet, field) (*res)[field.From] = values } else if field.Config != "" { - resFile, resType := getResProp(field.Config) - values, _ := getResValue(resFile, resType, field) + resFile, resType, _ := getResProp(field.Config) + values, _ := getResValue(resFile, resType, "", field) (*res)[field.Config] = values } } -func getResProp(from string) (string, string) { // from resource - resFile := "" - resType := "" +func getResProp(from string) (resFile, resType, sheet string) { // from resource index := strings.LastIndex(from, ".yaml") if index > -1 { // yaml, system.ip.v1.yaml @@ -68,13 +66,8 @@ func getResProp(from string) (string, string) { // from resource resFile = left + ".yaml" resType = "yaml" - } else { // excel, system.address.v1.city - index = strings.LastIndex(from, ".") - - left := from[:index] - left = strings.ReplaceAll(left, ".", constant.PthSep) - - resFile = left + ".xlsx" + } else { // excel, like address.cn.v1.china + resFile, sheet = convertExcelPath(from) resType = "excel" } @@ -101,24 +94,60 @@ func getResProp(from string) (string, string) { // from resource resFile = resPath } - return resFile, resType + return +} + +func convertExcelPath(from string) (ret, sheet string) { + path1 := from // address.cn.v1 + index := strings.LastIndex(from, ".") + path2 := from[:index] // address.cn.v1.china + + paths := [2]string{path1, path2} + for index, path := range paths { + + arr := strings.Split(path, ".") + for i := 0; i < len(arr); i++ { + dir := "" + if i > 0 { + dir = strings.Join(arr[:i], constant.PthSep) + } + file := strings.Join(arr[i:], ".") + ".xlsx" + + if dir != "" { + ret = dir + constant.PthSep + file + } else { + ret = file + } + + realPth := vari.WorkDir + constant.ResDir + ret + if fileUtils.FileExist(realPth) { + if index == 1 { + sheet = from[strings.LastIndex(from, ".")+1:] + } + ret = realPth + return + } + } + } + + return } -func getResValue(resFile string, resType string, field *model.DefField) (map[string][]string, string) { +func getResValue(resFile, resType, sheet string, field *model.DefField) (map[string][]string, string) { resName := "" groupedValues := map[string][]string{} if resType == "yaml" { groupedValues, resName = getResForYaml(resFile) } else if resType == "excel" { - groupedValues, resName = getResForExcel(resFile, field) + groupedValues, resName = getResForExcel(resFile, sheet, field) } return groupedValues, resName } -func getResForExcel(resFile string, field *model.DefField) (map[string][]string, string) { - valueMap, resName := GenerateFieldValuesFromExcel(resFile, field) +func getResForExcel(resFile, sheet string, field *model.DefField) (map[string][]string, string) { + valueMap, resName := GenerateFieldValuesFromExcel(resFile, sheet, field) return valueMap, resName } @@ -199,7 +228,7 @@ func getResForInstances(insts model.ResInsts) map[string][]string { } func getRootRangeOrInstant(inst model.DefField) (parentRanges model.ResRanges, parentInsts model.ResInsts) { - resFile, _ := getResProp(inst.From) + resFile, _, _ := getResProp(inst.From) yamlContent, err := ioutil.ReadFile(resFile) if err != nil {