提交 ae430cb4 编写于 作者: 陈琦

support file path in use property

上级 b6e6b336
......@@ -111,6 +111,7 @@ func getResFromYaml(resFile string) (valueMap map[string][]string) { // , resNam
insts := model.ResInstances{}
err = yaml.Unmarshal(yamlContent, &insts)
if err == nil && insts.Instances != nil && len(insts.Instances) > 0 { // instances
insts.FileDir = fileUtils.GetAbsDir(resFile)
valueMap = getResFromInstances(insts)
//resName = insts.Field
} else {
......@@ -139,7 +140,6 @@ func getResFromInstances(insts model.ResInstances) (groupedValue map[string][]st
groupedValue = map[string][]string{}
for _, inst := range insts.Instances {
for _, instField := range inst.Fields {
prepareNestedInstanceRes(insts, inst, instField)
}
......@@ -175,6 +175,7 @@ func prepareNestedInstanceRes(insts model.ResInstances, inst model.ResInstancesI
instField.From = inst.From
}
}
instField.FileDir = insts.FileDir
if instField.Use != "" { // refer to another instances or ranges
if vari.Res[instField.From] == nil {
......@@ -185,8 +186,8 @@ func prepareNestedInstanceRes(insts model.ResInstances, inst model.ResInstancesI
groupedValueReferenced = getResFromRanges(referencedRanges)
} else if len(referencedInstants.Instances) > 0 { // refer to instances
for _, referencedInst := range referencedInstants.Instances {
for _, referencedInstField := range referencedInst.Fields {
for _, referencedInst := range referencedInstants.Instances { // iterate items
for _, referencedInstField := range referencedInst.Fields { // if item had children, iterate children
prepareNestedInstanceRes(referencedInstants, referencedInst, referencedInstField)
}
......@@ -218,13 +219,16 @@ func getReferencedRangeOrInstant(inst model.DefField) (referencedRanges model.Re
}
err1 := yaml.Unmarshal(yamlContent, &referencedRanges)
if err1 != nil || referencedRanges.Ranges == nil || len(referencedRanges.Ranges) == 0 { // instances
if err1 != nil || referencedRanges.Ranges == nil || len(referencedRanges.Ranges) == 0 { // parse ranges failed
err2 := yaml.Unmarshal(yamlContent, &referencedInsts)
if err2 != nil || referencedInsts.Instances == nil || len(referencedInsts.Instances) == 0 { // ranges
if err2 != nil || referencedInsts.Instances == nil || len(referencedInsts.Instances) == 0 { // parse instances failed
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_parse_file", resFile))
return
} else { // is instances
referencedInsts.FileDir = fileUtils.GetAbsDir(resFile)
}
} else { // is ranges
referencedRanges.FileDir = fileUtils.GetAbsDir(resFile)
}
return
......
......@@ -23,12 +23,14 @@ type ResConfig struct {
// range res
type ResRanges struct {
ClsInfo `yaml:",inline"`
FileDir string `yaml:"fileDir,omitempty"`
Ranges map[string]string `yaml:"ranges"`
}
// instance res
type ResInstances struct {
ClsBase `yaml:",inline"`
FileDir string `yaml:"fileDir,omitempty"`
Instances []ResInstancesItem `yaml:"instances,flow"`
}
type ResInstancesItem struct {
......
......@@ -255,7 +255,7 @@ func (s *DefService) saveFieldToDB(field *model.ZdField, def model.ZdDef, currPa
refer.Condition = field.Where
refer.Rand = field.Rand
_, sheet := fileUtils.ConvertResExcelPath(field.From)
_, sheet := fileUtils.ConvertResExcelPath(field.From, currPath)
refer.File = field.From
refer.Sheet = sheet
......
......@@ -222,7 +222,7 @@ func (s *InstancesService) saveItemToDB(item *model.ZdInstancesItem, instances m
refer.Condition = item.Where
refer.Rand = item.Rand
_, sheet := fileUtils.ConvertResExcelPath(item.From)
_, sheet := fileUtils.ConvertResExcelPath(item.From, currPath)
refer.File = item.From
refer.Sheet = sheet
......
......@@ -201,12 +201,14 @@ func GetResProp(from, currFileDir string) (resFile, resType, sheet string) { //
return
}
func ConvertReferRangeToPath(f, currFile string) (path string) {
path = ConvertResYamlPath(f)
func ConvertReferRangeToPath(file, currFile string) (path string) {
dir := GetAbsDir(currFile)
path = ConvertResYamlPath(file, dir)
if path == "" {
resPath := GetAbsDir(currFile) + f
resPath := GetAbsDir(currFile) + file
if !FileExist(resPath) { // in same folder
resPath = vari.ZdPath + f
resPath = vari.ZdPath + file
if !FileExist(resPath) { // in res file
resPath = ""
}
......@@ -218,6 +220,12 @@ func ConvertReferRangeToPath(f, currFile string) (path string) {
}
func ConvertResYamlPath(from, dir string) (ret string) {
pth := namedFileExistInDir(from, dir)
if pth != "" {
ret = pth
return
}
arr := strings.Split(from, ".")
for i := 0; i < len(arr); i++ {
dir := ""
......@@ -248,6 +256,12 @@ func ConvertResYamlPath(from, dir string) (ret string) {
}
func ConvertResExcelPath(from, dir string) (ret, sheet string) {
pth := namedFileExistInDir(from, dir)
if pth != "" {
ret = pth
return
}
path1 := from // address.cn.v1
index := strings.LastIndex(from, ".")
path2 := from[:index] // address.cn.v1.china
......@@ -298,7 +312,7 @@ func ConvertResExcelPath(from, dir string) (ret, sheet string) {
return
}
func ComputerReferFilePath(file, dir string) (resPath string) {
func ComputerReferFilePath(file string) (resPath string) {
resPath = file
if filepath.IsAbs(resPath) && FileExist(resPath) {
return
......@@ -421,3 +435,22 @@ func RemovePathSepLeftIfNeeded(pth string) string {
return pth
}
func namedFileExistInDir(file, dir string) (pth string) {
if IsAbsPath(file) { // abs path, return it
if FileExist(file) {
pth = file
return
} else {
return
}
} else {
file = path.Join(dir, file)
if FileExist(file) {
pth = file
return
}
}
return
}
title: 私有IP地址
desc:
author: wwccss
version: 1.0
field: ip
instances:
- instance: privateC
note: 私有C类IP地址
prefix: 192.168.
fields:
- field: part3
range: 0-254
postfix: .
- field: part4
range: 1-254
- instance: privateB
note: 私有B类IP地址
prefix: 172.
fields:
- field: part2
range: 16-31
postfix: .
- field: part3
range: 0-254
postfix: .
- field: part4
range: 1-254
- instance: privateA
note: 私有A类IP地址
prefix: 10.
fields:
- field: part2
range: 0-254
postfix: .
- field: part3
range: 0-254
postfix: .
- field: part4
range: 1-254
- instance: publicC
note: 公有C类IP地址
mode: r
fields:
- field: part1
range: 192-223
postfix: .
- field: part2
range: 0-254
postfix: .
- field: part3
range: 0-254
postfix: .
- field: part4
range: 1-254
- instance: publicB
note: 公有B类IP地址
mode: r
fields:
- field: part1
range: 128-191
postfix: .
- field: part2
range: 0-254
postfix: .
- field: part3
range: 0-254
postfix: .
- field: part4
range: 1-254
- instance: publicA
note: 公有A类IP地址
mode: r
fields:
- field: part1
range: 1-126
postfix: .
- field: part2
range: 0-254
postfix: .
- field: part3
range: 0-254
postfix: .
- field: part4
range: 1-254
\ No newline at end of file
title: 邮件地址
desc:
author: zentao
version: 1.0
field: email
instances:
- instance: pinyin_with_esp
note: 来源于ZD目录下的嵌套资源
fields:
- field: chinese_account
from: name.cnaccount.v1.yaml
use: common_underline
postfix: "@"
- field: esp_domain
from: domain.esp.v1.yaml
use: esp
- instance: ip_address
note: 来源于相对路径的文件
fields:
- field: ip
from: sub/../sub/sub.yaml
use: sub
\ No newline at end of file
title: 测试
desc:
author: zentao
version: 1.0
field: test
instances:
- instance: sub
note: 来源于相对路径的文件
fields:
- field: from-insts
from: ../../ip.file.yaml
use: privateA
postfix: "\t"
- field: from-ranges
from: ../../../yaml/zentao/number/v1.yaml
use: netmask
\ No newline at end of file
......@@ -6,6 +6,6 @@ version: 1.0
fields:
- field: part1
range: A,test-nested1.yaml{3}
range: A,test-nested-range1.yaml{3}
prefix: ""
postfix: ""
......@@ -9,4 +9,4 @@ fields:
postfix: ";\t"
- field: part2
range: X,test-nested2.yaml{3}
\ No newline at end of file
range: X,test-nested-range2.yaml{3}
\ No newline at end of file
title:
desc:
author:
version: 1.0
fields:
- field: field_use_config
config: ../demo/number.yaml # 引用/demo/number.yaml
postfix: "\t"
- field: field_use_ranges1
from: zentao.number.v1.yaml # 引用/yaml/zentao/number/v1.yaml
use: medium
postfix: "\t"
- field: field_use_ranges2
from: zentao.number.file.yaml # 当前目录下同名文件
use: tiny
postfix: "\t"
- field: field_use_instance1
from: ip.v1.yaml # 引用/yaml/ip/v1.yaml
use: privateC,privateB
postfix: "\t"
- field: field_use_instance2
from: ip.file.yaml # 当前目录下同名文件
use: privateC,privateB
postfix: "\t"
- field: field_use_excel
from: address.cn.v1.china # 引用data/address/v1.xlsx
select: city
where: state like '%山东%'
rand: true
postfix: "\t"
# 引用res/email.yaml文件,其嵌套引用了ZD dir下的资源
- field: field_use_nested1
from: res/mixed.yaml
use: pinyin_with_esp
prefix: "["
postfix: "]\t"
- field: field_use_nested2
from: res/mixed.yaml
use: ip_address
prefix: "["
postfix: "]\t"
\ No newline at end of file
title: 数字系列
desc:
author: wwccss
version: 1.0
field: number
note: 数值类型
prefix: "test-"
postfix: "'"
ranges:
tiny: 0.1-0.9
small: 1-9
medium: 101-199
large: 10001-99999
netmask: 8,16,24
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册