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

task 7498,7501

上级 73ce975f
......@@ -4,6 +4,9 @@ author: zentao
version: 1.0
fields:
- field: test
range: 9-1:2{2}
- field: field_use_instance # 引用default.xml里的嵌套字段
prefix: "[" # 复写前缀
postfix: "]" # 特殊字符加引号,否则无法解析
......
......@@ -17,17 +17,16 @@ func GenerateIntItemsByStep(start int64, end int64, step int, repeat int) []inte
arr := make([]interface{}, 0)
total := 0
for round := 0; round < repeat; round++ {
for i := 0; true; {
val := start + int64(i*step)
if val > end {
break
}
for i := 0; true; {
val := start + int64(i*step)
if (val > end && step > 0) || (val < end && step < 0) {
break
}
for round := 0; round < repeat; round++ {
arr = append(arr, val)
i++
total++
total++
if total > constant.MaxNumb {
break
}
......@@ -36,6 +35,7 @@ func GenerateIntItemsByStep(start int64, end int64, step int, repeat int) []inte
if total >= constant.MaxNumb {
break
}
i++
}
return arr
......
package gen
import (
"fmt"
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
"math/rand"
"regexp"
"strconv"
"strings"
)
......@@ -40,10 +38,8 @@ func GenerateFieldValues(field *model.DefField, fieldValue *model.FieldValue) {
}
func GenerateFieldValuesFromList(field *model.DefField, fieldValue *model.FieldValue) {
//rang := strings.TrimSpace(field.Range)
rang := field.Range
rangeItems := ParseRange(rang)
rangeItems := ParseRange(rang) // 1
index := 0
for _, rangeItem := range rangeItems {
......@@ -51,11 +47,11 @@ func GenerateFieldValuesFromList(field *model.DefField, fieldValue *model.FieldV
if rangeItem == "" { continue }
entry, stepStr, repeat := ParseRangeItem(rangeItem)
typ, desc := ParseEntry(entry)
typ, desc := ParseEntry(entry) // 2
items := make([]interface{}, 0)
if typ == "literal" {
items = GenerateValuesFromLiteral(field, desc, stepStr, repeat)
items = GenerateValuesFromLiteral(desc, stepStr, repeat)
} else if typ == "interval" {
items = GenerateValuesFromInterval(field, desc, stepStr, repeat)
}
......@@ -72,8 +68,8 @@ func GenerateFieldValuesFromList(field *model.DefField, fieldValue *model.FieldV
func CheckRangeType(startStr string, endStr string, stepStr string) (string, interface{}, int, bool) {
rand := false
_, errInt1 := strconv.ParseInt(startStr, 0, 64)
_, errInt2 := strconv.ParseInt(endStr, 0, 64)
int1, errInt1 := strconv.ParseInt(startStr, 0, 64)
int2, errInt2 := strconv.ParseInt(endStr, 0, 64)
var errInt3 error
if strings.ToLower(stepStr) != "r" {
_, errInt3 = strconv.ParseInt(stepStr, 0, 64)
......@@ -89,11 +85,14 @@ func CheckRangeType(startStr string, endStr string, stepStr string) (string, int
rand = true
}
if int1 > int2 && step.(int) > 0 {
step = -1 * step.(int)
}
return "int", step, 0, rand
} else {
startFloat, errFloat1 := strconv.ParseFloat(startStr, 64)
_, errFloat2 := strconv.ParseFloat(endStr, 64)
float1, errFloat1 := strconv.ParseFloat(startStr, 64)
float2, errFloat2 := strconv.ParseFloat(endStr, 64)
var errFloat3 error
if strings.ToLower(stepStr) != "r" {
_, errFloat3 = strconv.ParseFloat(stepStr, 64)
......@@ -109,7 +108,11 @@ func CheckRangeType(startStr string, endStr string, stepStr string) (string, int
rand = true
}
precision := getPrecision(startFloat, step)
precision := getPrecision(float1, step)
if float1 > float2 && step.(int) > 0 {
step = -1 * step.(int)
}
return "float", step, precision, rand
} else if len(startStr) == 1 && len(endStr) == 1 { // is char
......@@ -123,6 +126,9 @@ func CheckRangeType(startStr string, endStr string, stepStr string) (string, int
rand = true
}
if strings.Compare(startStr,endStr) > 0 && step.(int) > 0 {
step = -1 * step.(int)
}
return "char", step, 0, rand
}
}
......@@ -130,77 +136,15 @@ func CheckRangeType(startStr string, endStr string, stepStr string) (string, int
return "string", 1, 0, false // is string
}
func ParseRange(rang string) []string {
items := make([]string, 0)
tagOpen := false
temp := ""
runeArr := make([]rune, 0)
for _, c := range rang {
runeArr = append(runeArr, c)
}
for i := 0; i < len(runeArr); i++ {
c := runeArr[i]
if c == constant.RightChar {
tagOpen = false
} else if c == constant.LeftChar {
tagOpen = true
}
if i == len(runeArr) - 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"
repeat := 1
item = strings.TrimSpace(item)
if string(item[0]) == string(constant.LeftChar) && // It's a whole when meet (xxx)
string(item[len(item) - 1]) == string(constant.RightChar) {
return item, step, repeat
}
regx := regexp.MustCompile(`\{(.*)\}`)
arr := regx.FindStringSubmatch(item)
if len(arr) == 2 {
repeat, _ = 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, repeat
}
func GenerateValuesFromLiteral(field *model.DefField, desc string, stepStr string, repeat int) []interface{} {
func GenerateValuesFromLiteral(desc string, stepStr string, repeat int) []interface{} {
items := make([]interface{}, 0)
elemArr := strings.Split(desc, ",")
stepStr = strings.ToLower(strings.TrimSpace(stepStr))
step, _ := strconv.Atoi(stepStr)
total := 0
for round := 0; round < repeat; round++ {
for i := 0; i < len(elemArr); {
for i := 0; i < len(elemArr); {
for round := 0; round < repeat; round++ {
val := ""
if stepStr == "r" {
val = elemArr[rand.Intn(len(elemArr))]
......@@ -256,20 +200,3 @@ func GenerateValuesFromInterval(field *model.DefField, desc string, stepStr stri
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
}
package gen
import (
"fmt"
"github.com/easysoft/zendata/src/utils/const"
"regexp"
"strconv"
"strings"
"unicode"
)
/**
split field range string with comma to a array, ignore the comma in []
1-2:R,[user1,user2]{2} -> 1-2:R
[user1,user2]{2}
*/
func ParseRange(rang string) []string {
items := make([]string, 0)
tagOpen := false
temp := ""
rang = strings.Trim(rang, ",")
runeArr := []rune(rang)
for i := 0; i < len(runeArr); i++ {
c := runeArr[i]
if c == constant.RightChar {
tagOpen = false
} else if c == constant.LeftChar {
tagOpen = true
}
if i == len(runeArr) - 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
}
/**
convert range item to entity, step, repeat
[user1,user2]{2} -> entry =>[user1,user2]
step =>1
repeat =>2
*/
func ParseRangeItem(item string) (entry string, step string, repeat int) {
item = strings.TrimSpace(item)
if string(item[0]) == string(constant.LeftChar) && // It's a whole when meet (xxx)
string(item[len(item) - 1]) == string(constant.RightChar) {
return item, step, repeat
}
regx := regexp.MustCompile(`\{(.*)\}`)
arr := regx.FindStringSubmatch(item)
if len(arr) == 2 {
repeat, _ = strconv.Atoi(arr[1])
}
itemWithoutRepeat := regx.ReplaceAllString(item, "")
sectionArr := strings.Split(itemWithoutRepeat, ":")
entry = sectionArr[0]
if len(sectionArr) == 2 {
step = strings.ToLower(sectionArr[1])
}
return entry, step, repeat
}
/**
get range item entity's type and desc
1-9 or [1-9] -> type => interval
desc => 1-9 or [1-9]
[user1,user2] -> type => literal
desc => user2,user3
*/
func ParseEntry(str string) (typ string, desc string) {
str = strings.TrimSpace(str)
desc = strings.ReplaceAll(str, string(constant.LeftChar), "")
desc = strings.ReplaceAll(desc,string(constant.RightChar), "")
if strings.Contains(desc, ",") || !strings.Contains(desc, "-") {
typ = "literal"
} else {
if !isBoundaryStr(desc) {
typ = "literal"
} else {
typ = "interval"
}
}
return
}
func isBoundaryStr(str string) bool {
arr := strings.Split(str, "-")
left := strings.TrimSpace(arr[0])
right := strings.TrimSpace(arr[1])
if len(left) != 1 || len(right) != 1 {
return false
} else {
leftRune := []rune(string(left[0]))[0]
rightRune := []rune(string(right[0]))[0]
if (unicode.IsLetter(leftRune) && unicode.IsLetter(rightRune)) ||
(unicode.IsNumber(leftRune) && unicode.IsNumber(rightRune)) {
return true
} else {
return false
}
}
}
title: test file
desc: this is the test file.
author: zentao
version: 1.0
fields:
- field: field1
note: 默认的列表类型,通过逗号隔成若干区段。
range: 1-10,20-25,27,29,30
prefix: int_
postfix: \t
expect: 1,2,3...,10,20,21,22...,25,27,29.30
- field: field2
note: 区间可以指定步长。
range: 1-10:2, 1-2:0.1
expect: 1,3,5,7,9,1,1.1,1.2...,2
- field: field3
note: 通过R属性指定随机。R属性和步长不能同时出现。
range: 1-10:R
expect: 1,5,8...
- field: field 4
note: 从一个文件中读取列表,并指定随机。
range: field4.txt:R
expect: 1,3,9,10
- field: field 5
note: 循环的字段。
range: a-z
loop: 3
loopfix: |
postfix: ^
expect: a|b|c^d|e|f^g|h|i
- field: field6
note: 自定义的数据类型。可以通过@tag的方式对数据进行筛选。
from: system.nubmer.yaml
use: small,large
- field: field 7
note: 自定义的数据类型,包含子字段。
from: system.ip.yaml
use: privatec,privateb
- field: field 8
note: 从excel数据源里面取数据。system代表的路径。
from: system.address.china
select: city
where: stat like '%山东%'
- field: field9
note: 嵌套的字段
fields:
- field: field9.1
range: a-z
prefix: part1_
postfix: '|'
- field: field9.2
range: A-Z
prefix: part2_
postfix: '|'
- field: field9.3
prefix: part3_
postfix:
fields:
- field: field9.3.1
prefix: int_
range: 10-20
postfix:
title: test file
desc: this is the test file
author: zentao
version: 1.0
class:
- file: password
type: password.xlsx
- name: numb
type: numb.yaml
- name: state_city
note: SQL查询
type: list
prefix: '!'
postfix: "!"
loop: 2
loopfix: " "
fields:
- name: myprefix
type: list
range: "#"
- name: city
type: address.city
format: ${seq}-${name}(${state})
range: select * from address.city where state like '%山东%'
prefix: '['
postfix: "]"
loop: 2
loopfix: ","
- name: mypostfix
type: list
range: "#"
fields1:
- name: 字段1
note:
type: list
range: ${user_name}_${numb}@${domain}
- name: user_name
note: 用户名
fields:
- name: first_name
type: custom
- name: separator
type: list
range: "_"
- name: last_name
type: custom
- name: 字段2
note:
type: list
prefix: '<'
postfix: ">"
loop: 2
loopfix: "-----"
fields:
- name: 省市
type: state_city
- name: numb
type: custom
range: " ${numb} "
- name: test
type: custom
range: " ${domain} "
- name: field1
note: 默认的列表类型,通过逗号隔成若干区段。
type: list
range: 1-3,x-z:R,10.01-19.20:3,101,102,103
prefix:
postfix:
loop: 3
loopfix: "|"
format: "%.5f"
expect: 1,2,3...,10,20,21,22...,25,27,29.30
- name: field2
note: 嵌套的字段
fields:
- name: field9.1
type: list
range: a-z
prefix: '['
postfix: ']'
loop: 3
loopfix: '|'
- name: field9.2
type: list
range: A-Z
prefix: '['
postfix: ']'
- name: field9.3
prefix: '['
postfix: ']'
fields:
- name: field9.3.1
type: list
range: X-Z
- name: field9.3.2
type: list
range: 10-20
- name: field3.1
note: 从文件读数据
type: list
range: list.txt:R
- name: field3.2
note: 从文件读数据
fields:
- name: domain
note: 字面常量
type: list
range: MY_,1-2
- name: domain
note: 域名,全局定义
type: custom
- name: numb
note: 从文件读定义
type: custom
range: custom.yaml
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册