From 860494a3a46d32b7dd30081328614f750574d0ac Mon Sep 17 00:00:00 2001 From: aaron <462826@qq.com> Date: Fri, 24 Jul 2020 11:22:26 +0800 Subject: [PATCH] close task #7508 --- .gitignore | 1 + data/system/ip/private.yaml | 15 ----------- data/system/misc/timetamp.yaml | 42 ------------------------------ data/system/name/chinese-name.yaml | 42 ------------------------------ data/system/name/english-name.yaml | 42 ------------------------------ demo/number_ranges.yaml | 15 ----------- demo/test.yaml | 12 ++++++--- src/gen/res.go | 40 ++++++++++++++++------------ 8 files changed, 34 insertions(+), 175 deletions(-) delete mode 100644 data/system/ip/private.yaml delete mode 100644 demo/number_ranges.yaml diff --git a/.gitignore b/.gitignore index a474d70..ee6cda2 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /data/.cache/.data.db-wal /demo/out/ /test/output/ +/data/_test/ diff --git a/data/system/ip/private.yaml b/data/system/ip/private.yaml deleted file mode 100644 index 251ed6d..0000000 --- a/data/system/ip/private.yaml +++ /dev/null @@ -1,15 +0,0 @@ -title: 私有IP -desc: -author: zentao -version: 1.0 - -field: privateIP -instances: - - - instance: all - fields: - - field: part - from: system.ip.v1.yaml - use: privateA, privateB, privateC - prefix: "" - postfix: "/24" diff --git a/data/system/misc/timetamp.yaml b/data/system/misc/timetamp.yaml index 19c6105..e69de29 100644 --- a/data/system/misc/timetamp.yaml +++ b/data/system/misc/timetamp.yaml @@ -1,42 +0,0 @@ -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 \ No newline at end of file diff --git a/data/system/name/chinese-name.yaml b/data/system/name/chinese-name.yaml index 19c6105..e69de29 100644 --- a/data/system/name/chinese-name.yaml +++ b/data/system/name/chinese-name.yaml @@ -1,42 +0,0 @@ -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 \ No newline at end of file diff --git a/data/system/name/english-name.yaml b/data/system/name/english-name.yaml index 19c6105..e69de29 100644 --- a/data/system/name/english-name.yaml +++ b/data/system/name/english-name.yaml @@ -1,42 +0,0 @@ -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 \ No newline at end of file diff --git a/demo/number_ranges.yaml b/demo/number_ranges.yaml deleted file mode 100644 index db26de1..0000000 --- a/demo/number_ranges.yaml +++ /dev/null @@ -1,15 +0,0 @@ -title: number -desc: 数字分组 -author: wwccss -version: 1.0 - -field: number -note: 数值类型数据 -prefix: "'" -postfix: "'" -loop: 2 -loopfix: "," -ranges: - small: 1-9 - medium: 11-99 - large: 10001-99999 diff --git a/demo/test.yaml b/demo/test.yaml index a29e42e..5a0c47c 100644 --- a/demo/test.yaml +++ b/demo/test.yaml @@ -20,8 +20,14 @@ fields: prefix: "[" # 复写前缀 postfix: "]" # 特殊字符加引号,否则无法解析 - - field: field_nested_inst # 引用的instance使用多层嵌套的use定义 - from: system.ip.private.yaml # 引用data/system/ip/v1.yaml + - field: field_nested_range # 引用的range使用多层嵌套的use定义 + from: system.misc.number.yaml # 引用data/system/misc/number.yaml use: all # 使用该文件中定义的privateC和privateB两个实例。 prefix: "[" # 复写前缀 - postfix: "]" # 特殊字符加引号,否则无法解析 \ No newline at end of file + postfix: "]" # 特殊字符加引号,否则无法解析 + + - field: field_nested_refer # 引用的instance使用多层嵌套的use定义 + from: _test.ip.private.yaml + use: all + prefix: "[" + postfix: "]" \ No newline at end of file diff --git a/src/gen/res.go b/src/gen/res.go index bb1ddfc..efe46d2 100644 --- a/src/gen/res.go +++ b/src/gen/res.go @@ -71,12 +71,9 @@ func getResProp(from string) (string, string) { // from resource resPath := resFile if !filepath.IsAbs(resPath) { - resPath = vari.ConfigDir + resFile + resPath = vari.ExeDir + constant.ResDir + resFile // used res should be in data folder if !fileUtils.FileExist(resPath) { - resPath = vari.DefaultDir + resFile - if !fileUtils.FileExist(resPath) { - resPath = "" - } + resPath = "" } } else { if !fileUtils.FileExist(resPath) { @@ -148,16 +145,22 @@ func getResForInstances(insts model.ResInsts) map[string][]string { for _, inst := range insts.Instances { for _, instField := range inst.Fields { // prepare referred parent instances if needed - if instField.Use != "" { // refer to another instance - parent := getRootInstant(instField) + if instField.Use != "" { // refer to another def + parentRanges, parentInstants := getRootRangeOrInstant(instField) groupedValueParent := map[string][]string{} - for _, child := range parent.Instances { - field := convertInstantToField(parent, child) - // gen values - group := child.Instance - groupedValueParent[group] = GenerateForField(&field, constant.Total, false) + if len(parentInstants.Instances) > 0 { + for _, child := range parentInstants.Instances { + field := convertInstantToField(parentInstants, child) + + // gen values + group := child.Instance + groupedValueParent[group] = GenerateForField(&field, constant.Total, false) + } + } else if len(parentRanges.Ranges) > 0 { + groupedValueParent = getResForRanges(parentRanges) } + vari.Res[instField.From] = groupedValueParent } } @@ -172,7 +175,7 @@ func getResForInstances(insts model.ResInsts) map[string][]string { return groupedValue } -func getRootInstant(inst model.DefField) (parentInsts model.ResInsts) { +func getRootRangeOrInstant(inst model.DefField) (parentRanges model.ResRanges, parentInsts model.ResInsts) { resFile, _ := getResProp(inst.From) yamlContent, err := ioutil.ReadFile(resFile) @@ -181,9 +184,14 @@ func getRootInstant(inst model.DefField) (parentInsts model.ResInsts) { return } - err = yaml.Unmarshal(yamlContent, &parentInsts) - if err == nil && parentInsts.Instances != nil && len(parentInsts.Instances) > 0 { // instances - logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_parse_file", resFile)) + err1 := yaml.Unmarshal(yamlContent, &parentRanges) + if err1 != nil || parentInsts.Instances == nil || len(parentInsts.Instances) == 0 { // instances + + err2 := yaml.Unmarshal(yamlContent, &parentInsts) + if err2 != nil || parentRanges.Ranges == nil || len(parentRanges.Ranges) == 0 { // ranges + logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_parse_file", resFile)) + return + } } return -- GitLab