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

support literal and interval field definition

上级 3042ed3a
title: test
desc: This is a test file.
author: zentao
version: 1.0
fields:
- field: objectype
range: [bug,task]:2{3},story{2}
- field: objectid
range: [1-9]:R{2},[a,b1,c]:R{2}
- field: action
range: [create,resolve,close]{10},[create,finish,close]{10}
\ No newline at end of file
......@@ -40,21 +40,30 @@ fields:
expect: 青岛、济南 ...​
- field: field4
node: 嵌套字段
fields:
- field: field1.1
- field: field4.1
type: list
range: 1-9
postfix: ". "
- field: field1.2
- field: field4.2
prefix: '['
postfix: ']'
loop: 2
loopfix: ","
fields:
- field: field1.2.1
- field: field4.2.1
type: list
range: X-Z
- field: field1.2.2
- field: field4.2.2
type: list
range: 1-9
loop: 2
\ No newline at end of file
loop: 2
- field: field5
note: 滚动字段
range: abc*5,123,456
loop: 3
loopfix: ","
prefix: "'"
postfix: "'"
......@@ -5,21 +5,21 @@ import (
"math/rand"
)
func GenerateByteItems(start byte, end byte, step interface{}, rand bool) []interface{} {
func GenerateByteItems(start byte, end byte, step interface{}, rand bool, limit int) []interface{} {
if !rand {
return GenerateByteItemsByStep(start, end, step.(int))
return GenerateByteItemsByStep(start, end, step.(int), limit)
} else {
return GenerateByteItemsRand(start, end, step.(int))
return GenerateByteItemsRand(start, end, step.(int), limit)
}
}
func GenerateByteItemsByStep(start byte, end byte, step int) []interface{} {
func GenerateByteItemsByStep(start byte, end byte, step int, limit int) []interface{} {
arr := make([]interface{}, 0)
count := constant.MaxNumb
for i := 0; i < constant.MaxNumb; {
val := start + byte(int(i) * step)
if val > end {
if val > end || i > limit {
break
}
......@@ -31,16 +31,19 @@ func GenerateByteItemsByStep(start byte, end byte, step int) []interface{} {
return arr
}
func GenerateByteItemsRand(start byte, end byte, step int) []interface{} {
func GenerateByteItemsRand(start byte, end byte, step int, limit int) []interface{} {
arr := make([]interface{}, 0)
genCount := int(end - start) / step + 1
if genCount > constant.MaxNumb {
genCount = constant.MaxNumb
count := int(end - start) / step + 1
if count > limit {
count = limit
}
if count > constant.MaxNumb {
count = constant.MaxNumb
}
for i := 0; i < genCount; {
ran := rand.Intn(genCount)
for i := 0; i < count; {
ran := rand.Intn(count)
val := start + byte(ran)
arr = append(arr, val)
......
......@@ -2,6 +2,7 @@ package gen
import (
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
i118Utils "github.com/easysoft/zendata/src/utils/i118"
logUtils "github.com/easysoft/zendata/src/utils/log"
"gopkg.in/yaml.v3"
......@@ -15,6 +16,7 @@ func LoadRootDef(defaultFile, ymlFile string, fieldsToExport *[]string) model.De
if defaultFile != "" {
defaultContent, err := ioutil.ReadFile(defaultFile)
defaultContent = replaceSpecialChars(defaultContent)
if err != nil {
logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_read_file", defaultFile))
return defaultDef
......@@ -27,13 +29,14 @@ func LoadRootDef(defaultFile, ymlFile string, fieldsToExport *[]string) model.De
}
yamlContent, err := ioutil.ReadFile(ymlFile)
yamlContent = replaceSpecialChars(yamlContent)
if err != nil {
logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_read_file", ymlFile))
return ymlDef
}
err = yaml.Unmarshal(yamlContent, &ymlDef)
if err != nil {
logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_read_file", ymlFile))
logUtils.Screen(i118Utils.I118Prt.Sprintf("fail_to_parse_file", ymlFile))
return ymlDef
}
......@@ -142,3 +145,20 @@ func CopyField(child model.DefField, parent *model.DefField) {
(*parent).Precision = child.Precision
}
}
func replaceSpecialChars(bytes []byte) []byte {
str := string(bytes)
ret := ""
for _, line := range strings.Split(str, "\n") {
if strings.Index(strings.TrimSpace(line), "range") == 0 {
line = strings.ReplaceAll(line,"[", string(constant.LeftChar))
line = strings.ReplaceAll(line,"]", string(constant.RightChar))
}
ret += line + "\n"
}
return []byte(ret)
}
......@@ -7,21 +7,21 @@ import (
"strings"
)
func GenerateFloatItems(start float64, end float64, step interface{}, rand bool) []interface{} {
func GenerateFloatItems(start float64, end float64, step interface{}, rand bool, limit int) []interface{} {
if !rand{
return GenerateFloatItemsByStep(start, end, step.(float64))
return GenerateFloatItemsByStep(start, end, step.(float64), limit)
} else {
return GenerateFloatItemsRand(start, end, step.(float64))
return GenerateFloatItemsRand(start, end, step.(float64), limit)
}
}
func GenerateFloatItemsByStep(start float64, end float64, step interface{}) []interface{} {
func GenerateFloatItemsByStep(start float64, end float64, step interface{}, limit int) []interface{} {
arr := make([]interface{}, 0)
for i := 0; i < constant.MaxNumb; {
gap := float64(i) * step.(float64)
val := start + gap
if val > end {
if val > end || i > limit {
break
}
......@@ -32,15 +32,19 @@ func GenerateFloatItemsByStep(start float64, end float64, step interface{}) []in
return arr
}
func GenerateFloatItemsRand(start float64, end float64, step float64) []interface{} {
func GenerateFloatItemsRand(start float64, end float64, step float64, limit int) []interface{} {
arr := make([]interface{}, 0)
genCount := (end - start) / step
if genCount > float64(constant.MaxNumb) {
genCount = float64(constant.MaxNumb)
count := (end - start) / step
if count > float64(limit) {
count = float64(limit)
}
for i := float64(0); i < genCount; {
val := start + float64(rand.Int63n(int64(genCount))) * step
if count > float64(constant.MaxNumb) {
count = float64(constant.MaxNumb)
}
for i := float64(0); i < count; {
val := start + float64(rand.Int63n(int64(count))) * step
arr = append(arr, val)
i++
......
......@@ -5,42 +5,43 @@ import (
"math/rand"
)
func GenerateIntItems(start int64, end int64, step interface{}, rand bool) []interface{} {
func GenerateIntItems(start int64, end int64, step interface{}, rand bool, limit int) []interface{} {
if !rand {
return GenerateIntItemsByStep(start, end, step.(int))
return GenerateIntItemsByStep(start, end, step.(int), limit)
} else{
return GenerateIntItemsRand(start, end, step.(int))
return GenerateIntItemsRand(start, end, step.(int), limit)
}
}
func GenerateIntItemsByStep(start int64, end int64, step int) []interface{} {
func GenerateIntItemsByStep(start int64, end int64, step int, limit int) []interface{} {
arr := make([]interface{}, 0)
count := 0
for i := 0; i < constant.MaxNumb; {
val := start + int64(i * step)
if val > end {
if val > end || i > limit {
break
}
arr = append(arr, val)
count++
i++
}
return arr
}
func GenerateIntItemsRand(start int64, end int64, step int) []interface{} {
func GenerateIntItemsRand(start int64, end int64, step int, limit int) []interface{} {
arr := make([]interface{}, 0)
genCount := (end - start) / int64(step) + 1
if genCount > int64(constant.MaxNumb) {
genCount = int64(constant.MaxNumb)
count := (end - start) / int64(step) + 1
if count > int64(limit) {
count = int64(limit)
}
if count > int64(constant.MaxNumb) {
count = int64(constant.MaxNumb)
}
for i := int64(0); i < genCount; {
val := start + int64(rand.Int63n(genCount))
for i := int64(0); i < count; {
val := start + rand.Int63n(count)
arr = append(arr, val)
i++
......
package gen
import (
"fmt"
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
"regexp"
"strconv"
"strings"
)
......@@ -34,54 +36,31 @@ func GenerateFieldValues(field *model.DefField, fieldValue *model.FieldValue) {
} else {
GenerateFieldValuesFromList(field, fieldValue)
}
//else if strings.Index(field.Range, ".xlsx") > -1 {
// GenerateFieldValuesFromExcel(field, fieldValue)
//}
}
func GenerateFieldValuesFromList(field *model.DefField, fieldValue *model.FieldValue) {
//rang := strings.TrimSpace(field.Range)
rang := field.Range
rangeItems := strings.Split(rang, ",")
rangeItems := ParseRange(rang)
index := 0
for _, item := range rangeItems {
for _, rangeItem := range rangeItems {
if index >= constant.MaxNumb { break }
if item == "" { continue }
sectionArr := strings.Split(item, ":")
if len(sectionArr) == 0 { continue }
stepStr := "1"
if len(sectionArr) == 2 { stepStr = sectionArr[1] }
if rangeItem == "" { continue }
elemArr := strings.Split(sectionArr[0], "-")
startStr := elemArr[0]
endStr := startStr
if len(elemArr) > 1 { endStr = elemArr[1] }
entry, stepStr, limit := ParseRangeItem(rangeItem)
typ, desc := ParseEntry(entry)
items := make([]interface{}, 0)
dataType, step, precision, rand := CheckRangeType(startStr, endStr, stepStr)
if dataType == "int" {
startInt, _ := strconv.ParseInt(startStr, 0, 64)
endInt, _ := strconv.ParseInt(endStr, 0, 64)
items = GenerateIntItems(startInt, endInt, step, rand)
} else if dataType == "float" {
startFloat, _ := strconv.ParseFloat(startStr, 64)
endFloat, _ := strconv.ParseFloat(endStr, 64)
field.Precision = precision
items = GenerateFloatItems(startFloat, endFloat, step.(float64), rand)
} else if dataType == "char" {
items = GenerateByteItems(byte(startStr[0]), byte(endStr[0]), step, rand)
} else if dataType == "string" {
items = append(items, startStr)
if startStr != endStr {
items = append(items, endStr)
}
if typ == "literal" {
} else if typ == "interval" {
elemArr := strings.Split(desc, "-")
startStr := elemArr[0]
endStr := startStr
if len(elemArr) > 1 { endStr = elemArr[1] }
items = GenerateValuesFromInterval(field, startStr, endStr, stepStr, limit)
}
fieldValue.Values = append(fieldValue.Values, items...)
......@@ -145,4 +124,99 @@ func CheckRangeType(startStr string, endStr string, stepStr string) (string, int
}
return "string", 1, 0, false
}
func ParseRange(rang string) []string {
items := make([]string, 0)
tagOpen := false
temp := ""
for i := 0; i < len(rang); i++ {
c := rang[i]
if int32(c) == constant.RightChar {
tagOpen = false
} else if int32(c) == constant.LeftChar {
tagOpen = true
}
if i == len(rang) - 1 {
temp += fmt.Sprintf("%c", c)
items = append(items, temp)
} else if !tagOpen && c == ',' {
items = append(items, temp)
temp = ""
tagOpen = false
} else {
temp += fmt.Sprintf("%c", c)
}
}
return items
}
func ParseRangeItem(item string) (string, string, int) {
entry := ""
step := "1"
limit := -1
regx := regexp.MustCompile(`\{(.*)\}`)
arr := regx.FindStringSubmatch(item)
if len(arr) == 2 {
limit, _ = strconv.Atoi(arr[1])
}
item = regx.ReplaceAllString(item, "")
sectionArr := strings.Split(item, ":")
entry = sectionArr[0]
if len(sectionArr) == 2 {
step = sectionArr[1]
}
return entry, step, limit
}
func GenerateValuesFromInterval(field *model.DefField, startStr string, endStr string, stepStr string, limit int) []interface{} {
items := make([]interface{}, 0)
dataType, step, precision, rand := CheckRangeType(startStr, endStr, stepStr)
if dataType == "int" {
startInt, _ := strconv.ParseInt(startStr, 0, 64)
endInt, _ := strconv.ParseInt(endStr, 0, 64)
items = GenerateIntItems(startInt, endInt, step, rand, limit)
} else if dataType == "float" {
startFloat, _ := strconv.ParseFloat(startStr, 64)
endFloat, _ := strconv.ParseFloat(endStr, 64)
field.Precision = precision
items = GenerateFloatItems(startFloat, endFloat, step.(float64), rand, limit)
} else if dataType == "char" {
items = GenerateByteItems(byte(startStr[0]), byte(endStr[0]), step, rand, limit)
} else if dataType == "string" {
items = append(items, startStr)
if startStr != endStr {
items = append(items, endStr)
}
}
return items
}
func ParseEntry(str string) (string, string) {
typ := ""
desc := ""
str = strings.TrimSpace(str)
if int32(str[0]) == constant.LeftChar {
typ = "literal"
desc = strings.ReplaceAll(str, string(constant.LeftChar), "")
desc = strings.ReplaceAll(desc,string(constant.RightChar), "")
} else {
typ = "interval"
desc = str
}
return typ, desc
}
\ No newline at end of file
......@@ -31,6 +31,9 @@ var (
Def = model.DefData{}
Res = map[string]map[string][]string{}
LeftChar = '('
RightChar = ')'
ResDir = "data/"
ResPath = ResDir + "system/buildin.yaml"
......
#!/usr/bin/env php
<?php
$xml = simplexml_load_file('../test/output/output.xml');
$val = $xml->table->row->col[0];
print(">> $val\n");
\ No newline at end of file
if (!function_exists('simplexml_load_file')) {
$xml = simplexml_load_file('../test/output/output.xml');
$val = $xml->table->row->col[0];
print(">> $val\n");
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册