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

support literal and interval field definition

上级 bd4aa329
......@@ -5,10 +5,10 @@ version: 1.0
fields:
- field: objectype
range: [bug,task]:2{3},story{2}
range: [bug,task,story]:2{3},[testcase]{2}
- field: objectid
range: 1-3:1{2}
range: 1.0-9.0:3{2}
- field: action
range: [create,resolve,close]{10},[create,finish,close]{10}
\ No newline at end of file
range: [create,resolve,close]{2},[create,finish,close]{10}
\ No newline at end of file
......@@ -5,27 +5,37 @@ import (
"math/rand"
)
func GenerateByteItems(start byte, end byte, step interface{}, rand bool, limit int) []interface{} {
func GenerateByteItems(start byte, end byte, step interface{}, rand bool, repeat int) []interface{} {
if !rand {
return GenerateByteItemsByStep(start, end, step.(int), limit)
return GenerateByteItemsByStep(start, end, step.(int), repeat)
} else {
return GenerateByteItemsRand(start, end, step.(int), limit)
return GenerateByteItemsRand(start, end, step.(int), repeat)
}
}
func GenerateByteItemsByStep(start byte, end byte, step int, limit int) []interface{} {
func GenerateByteItemsByStep(start byte, end byte, step int, repeat int) []interface{} {
arr := make([]interface{}, 0)
count := constant.MaxNumb
for i := 0; i < constant.MaxNumb; {
val := start + byte(int(i) * step)
if val > end || i > limit {
break
total := 0
for round := 0; round < repeat; round++ {
for i := 0; true; {
val := start + byte(int(i)*step)
if val > end || i > repeat {
break
}
arr = append(arr, val)
i++
total++
if total > constant.MaxNumb {
break
}
}
arr = append(arr, val)
count++
i++
if total > constant.MaxNumb {
break
}
}
return arr
......@@ -34,20 +44,26 @@ func GenerateByteItemsByStep(start byte, end byte, step int, limit int) []interf
func GenerateByteItemsRand(start byte, end byte, step int, repeat int) []interface{} {
arr := make([]interface{}, 0)
count := int(end - start) / step + 1
if count > repeat {
count = repeat
}
if count > constant.MaxNumb {
count = constant.MaxNumb
}
countInRound := int(end - start) / step + 1
total := 0
for round := 0; round < repeat; round++ {
for i := 0; i < countInRound; {
ran := rand.Intn(countInRound)
val := start + byte(ran)
arr = append(arr, val)
i++
total++
for i := 0; i < count; {
ran := rand.Intn(count)
val := start + byte(ran)
if total > constant.MaxNumb {
break
}
}
arr = append(arr, val)
i++
if total > constant.MaxNumb {
break
}
}
return arr
......
......@@ -7,26 +7,37 @@ import (
"strings"
)
func GenerateFloatItems(start float64, end float64, step interface{}, rand bool, limit int) []interface{} {
func GenerateFloatItems(start float64, end float64, step interface{}, rand bool, repeat int) []interface{} {
if !rand{
return GenerateFloatItemsByStep(start, end, step.(float64), limit)
return GenerateFloatItemsByStep(start, end, step.(float64), repeat)
} else {
return GenerateFloatItemsRand(start, end, step.(float64), limit)
return GenerateFloatItemsRand(start, end, step.(float64), repeat)
}
}
func GenerateFloatItemsByStep(start float64, end float64, step interface{}, limit int) []interface{} {
func GenerateFloatItemsByStep(start float64, end float64, step interface{}, repeat int) []interface{} {
arr := make([]interface{}, 0)
for i := 0; i < constant.MaxNumb; {
gap := float64(i) * step.(float64)
val := start + gap
if val > end || i > limit {
total := 0
for round := 0; round < repeat; round++ {
for i := 0; true; {
gap := float64(i) * step.(float64)
val := start + gap
if val > end {
break
}
arr = append(arr, val)
i++
total++
if total > constant.MaxNumb {
break
}
}
if total > constant.MaxNumb {
break
}
arr = append(arr, val)
i++
}
return arr
......@@ -35,19 +46,25 @@ func GenerateFloatItemsByStep(start float64, end float64, step interface{}, limi
func GenerateFloatItemsRand(start float64, end float64, step float64, repeat int) []interface{} {
arr := make([]interface{}, 0)
count := (end - start) / step
if count > float64(repeat) {
count = float64(repeat)
}
if count > float64(constant.MaxNumb) {
count = float64(constant.MaxNumb)
}
countInRound := (end - start) / step
for i := float64(0); i < count; {
val := start + float64(rand.Int63n(int64(count))) * step
total := 0
for round := 0; round < repeat; round++ {
for i := float64(0); i < countInRound; {
val := start + float64(rand.Int63n(int64(countInRound)))*step
arr = append(arr, val)
i++
arr = append(arr, val)
i++
total++
if total > constant.MaxNumb {
break
}
}
if total > constant.MaxNumb {
break
}
}
return arr
......
......@@ -33,7 +33,7 @@ func GenerateIntItemsByStep(start int64, end int64, step int, repeat int) []inte
}
}
if total > constant.MaxNumb {
if total >= constant.MaxNumb {
break
}
}
......@@ -44,8 +44,7 @@ func GenerateIntItemsByStep(start int64, end int64, step int, repeat int) []inte
func GenerateIntItemsRand(start int64, end int64, step int, repeat int) []interface{} {
arr := make([]interface{}, 0)
mod := (end - start) % int64(step)
countInRound := (end - start) / int64(step)
countInRound := (end - start) / int64(step) + 1
total := 0
for round := 0; round < repeat; round++ {
......
......@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
"math/rand"
"regexp"
"strconv"
"strings"
......@@ -54,13 +55,9 @@ func GenerateFieldValuesFromList(field *model.DefField, fieldValue *model.FieldV
items := make([]interface{}, 0)
if typ == "literal" {
items = GenerateValuesFromLiteral(field, desc, stepStr, repeat)
} 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, repeat)
items = GenerateValuesFromInterval(field, desc, stepStr, repeat)
}
fieldValue.Values = append(fieldValue.Values, items...)
......@@ -176,7 +173,45 @@ func ParseRangeItem(item string) (string, string, int) {
return entry, step, repeat
}
func GenerateValuesFromInterval(field *model.DefField, startStr string, endStr string, stepStr string, repeat int) []interface{} {
func GenerateValuesFromLiteral(field *model.DefField, 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); {
val := ""
if stepStr == "r" {
val = elemArr[rand.Intn(len(elemArr))]
} else {
val = elemArr[i]
}
items = append(items, val)
i += step
total++
if total > constant.MaxNumb {
break
}
}
if total >= constant.MaxNumb {
break
}
}
return items
}
func GenerateValuesFromInterval(field *model.DefField, desc string, stepStr string, repeat int) []interface{} {
elemArr := strings.Split(desc, "-")
startStr := elemArr[0]
endStr := startStr
if len(elemArr) > 1 { endStr = elemArr[1] }
items := make([]interface{}, 0)
dataType, step, precision, rand := CheckRangeType(startStr, endStr, stepStr)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册