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

fix bug#3330

上级 e70a7efd
......@@ -4,6 +4,13 @@ author: zentao
version: 1.0
fields:
- field: test0
range: 1-3{3}
loop: 2-3
loopfix: "|"
postfix: "\t"
width: 2
- field: test1
range: 9-5:R{2}
postfix: "\t"
......
......@@ -71,8 +71,8 @@ func GenerateForField(field *model.DefField, total int, withFix bool) (values []
if count > total {
count = total
}
values = connectChildrenToSingleStr(arrOfArr, count)
values = LoopSubFields(field, values, count, true)
values = combineChildrenValues(arrOfArr, count)
values = loopFieldValues(field, values, count, true)
} else if field.From != "" { // refer to res
......@@ -94,37 +94,45 @@ func GenerateForField(field *model.DefField, total int, withFix bool) (values []
values = append(values, groupValues[slct]...)
}
values = LoopSubFields(field, values, total, true)
values = loopFieldValues(field, values, total, true)
} else if field.Config != "" { // refer to config
groupValues := vari.Res[field.Config]
values = append(values, groupValues["all"]...)
values = LoopSubFields(field, values, total, true)
values = loopFieldValues(field, values, total, true)
} else { // leaf field
values = GenerateFieldItemsFromDefinition(field)
values = GenerateFieldValuesForDef(field)
}
return values
}
func GenerateFieldItemsFromDefinition(field *model.DefField) []string {
func GenerateFieldValuesForDef(field *model.DefField) []string {
values := make([]string, 0)
fieldWithValues := GenerateList(field)
fieldWithValues := CreateList(field)
index := 0
computerLoop(field)
indexOfRow := 0
count := 0
for {
// 处理格式、前后缀、loop等
val := GenerateFieldValWithFix(field, fieldWithValues, &index, true)
val := loopFieldValWithFix(field, fieldWithValues, &indexOfRow, true)
values = append(values, val)
count++
if index >= len(fieldWithValues.Values) || count >= vari.Total {
isRandomAndLoopEnd := (*field).IsLoop && (*field).LoopIndex == (*field).LoopEnd
isNotRandomAndValOver := !(*field).IsLoop && indexOfRow >= len(fieldWithValues.Values)
if count >= vari.Total || isRandomAndLoopEnd || isNotRandomAndValOver {
break
}
(*field).LoopIndex = (*field).LoopIndex + 1
if (*field).LoopIndex > (*field).LoopEnd {
(*field).LoopIndex = (*field).LoopStart
}
}
return values
......@@ -170,37 +178,42 @@ func GetFieldValStr(field model.DefField, val interface{}) string {
return str
}
func LoopSubFields(field *model.DefField, oldValues []string, total int, withFix bool) (values []string) {
func loopFieldValues(field *model.DefField, oldValues []string, total int, withFix bool) (values []string) {
fieldValue := model.FieldWithValues{}
for _, val := range oldValues {
fieldValue.Values = append(fieldValue.Values, val)
}
computerLoop(field)
indexOfRow := 0
count := 0
for {
// 处理格式、前后缀、loop等
str := GenerateFieldValWithFix(field, fieldValue, &indexOfRow, withFix)
str := loopFieldValWithFix(field, fieldValue, &indexOfRow, withFix)
values = append(values, str)
count++
if indexOfRow >= len(fieldValue.Values) || count >= total {
isRandomAndLoopEnd := (*field).IsLoop && (*field).LoopIndex == (*field).LoopEnd
isNotRandomAndValOver := !(*field).IsLoop && indexOfRow >= len(fieldValue.Values)
if count >= vari.Total || isRandomAndLoopEnd || isNotRandomAndValOver {
break
}
(*field).LoopIndex = (*field).LoopIndex + 1
if (*field).LoopIndex > (*field).LoopEnd {
(*field).LoopIndex = (*field).LoopStart
}
}
return
}
func GenerateFieldValWithFix(field *model.DefField, fieldValue model.FieldWithValues,
func loopFieldValWithFix(field *model.DefField, fieldValue model.FieldWithValues,
indexOfRow *int, withFix bool) (loopStr string) {
prefix := field.Prefix
postfix := field.Postfix
computerLoop(field)
for j := 0; j < (*field).LoopIndex; j++ {
if loopStr != "" {
loopStr = loopStr + field.Loopfix
......@@ -223,11 +236,6 @@ func GenerateFieldValWithFix(field *model.DefField, fieldValue model.FieldWithVa
loopStr = stringUtils.AddPad(loopStr, *field)
}
(*field).LoopIndex = (*field).LoopIndex + 1
if (*field).LoopIndex > (*field).LoopEnd {
(*field).LoopIndex = (*field).LoopStart
}
return
}
......@@ -264,6 +272,10 @@ func computerLoop(field *model.DefField) {
(*field).LoopEnd = 1
}
if (*field).LoopStart > 0 || (*field).LoopEnd > 0 {
(*field).IsLoop = false
}
(*field).LoopIndex = (*field).LoopStart
}
......@@ -295,7 +307,7 @@ func putChildrenToArr(arrOfArr [][]string, total int) (values [][]string) {
return
}
func connectChildrenToSingleStr(arrOfArr [][]string, total int) (ret []string) {
func combineChildrenValues(arrOfArr [][]string, total int) (ret []string) {
valueArr := putChildrenToArr(arrOfArr, total)
for _, arr := range valueArr {
......
......@@ -8,36 +8,36 @@ import (
"strings"
)
func GenerateList(field *model.DefField) model.FieldWithValues {
fieldValue := model.FieldWithValues{}
GenerateListField(field, &fieldValue)
func CreateList(field *model.DefField) model.FieldWithValues {
fieldWithValue := model.FieldWithValues{}
CreateListField(field, &fieldWithValue)
return fieldValue
return fieldWithValue
}
func GenerateListField(field *model.DefField, fieldValue *model.FieldWithValues) {
fieldValue.Field = field.Field
fieldValue.Precision = field.Precision
func CreateListField(field *model.DefField, fieldWithValue *model.FieldWithValues) {
fieldWithValue.Field = field.Field
fieldWithValue.Precision = field.Precision
if len(field.Fields) > 0 {
for _, child := range field.Fields {
childValue := model.FieldWithValues{}
GenerateListField(&child, &childValue)
childFieldWithValue := model.FieldWithValues{}
CreateListField(&child, &childFieldWithValue)
}
} else {
GenerateFieldValues(field, fieldValue)
CreateFieldValues(field, fieldWithValue)
}
}
func GenerateFieldValues(field *model.DefField, fieldValue *model.FieldWithValues) {
func CreateFieldValues(field *model.DefField, fieldValue *model.FieldWithValues) {
if strings.Index(field.Range, ".txt") > -1 {
GenerateFieldValuesFromText(field, fieldValue)
CreateFieldValuesFromText(field, fieldValue)
} else {
GenerateFieldValuesFromList(field, fieldValue)
CreateFieldValuesFromList(field, fieldValue)
}
}
func GenerateFieldValuesFromList(field *model.DefField, fieldValue *model.FieldWithValues) {
func CreateFieldValuesFromList(field *model.DefField, fieldValue *model.FieldWithValues) {
rang := field.Range
rangeItems := ParseRange(rang) // 1
......@@ -50,15 +50,13 @@ func GenerateFieldValuesFromList(field *model.DefField, fieldValue *model.FieldW
typ, desc := ParseEntry(entry) // 2
items := make([]interface{}, 0)
itemsWithPlaceholder := make([]string, 0)
if typ == "literal" {
items = GenerateValuesFromLiteral(field, desc, stepStr, repeat)
items = CreateValuesFromLiteral(field, desc, stepStr, repeat)
} else if typ == "interval" {
items = GenerateValuesFromInterval(field, desc, stepStr, repeat)
items = CreateValuesFromInterval(field, desc, stepStr, repeat)
}
fieldValue.Values = append(fieldValue.Values, items...)
fieldValue.ValuesWithPlaceholder = append(fieldValue.ValuesWithPlaceholder, itemsWithPlaceholder...)
index = index + len(items)
}
......@@ -139,7 +137,7 @@ func CheckRangeType(startStr string, endStr string, stepStr string) (string, int
return "string", 1, 0, false // is string
}
func GenerateValuesFromLiteral(field *model.DefField, desc string, stepStr string, repeat int) (items []interface{}) {
func CreateValuesFromLiteral(field *model.DefField, desc string, stepStr string, repeat int) (items []interface{}) {
elemArr := strings.Split(desc, ",")
step, _ := strconv.Atoi(stepStr)
total := 0
......@@ -173,7 +171,7 @@ func GenerateValuesFromLiteral(field *model.DefField, desc string, stepStr strin
return
}
func GenerateValuesFromInterval(field *model.DefField, desc string, stepStr string, repeat int) (items []interface{}) {
func CreateValuesFromInterval(field *model.DefField, desc string, stepStr string, repeat int) (items []interface{}) {
elemArr := strings.Split(desc, "-")
startStr := elemArr[0]
endStr := startStr
......
......@@ -15,7 +15,7 @@ import (
"strings"
)
func GenerateFieldValuesFromText(field *model.DefField, fieldValue *model.FieldWithValues) {
func CreateFieldValuesFromText(field *model.DefField, fieldValue *model.FieldWithValues) {
// get file and step string
rang := strings.TrimSpace(field.Range)
sectionArr := strings.Split(rang, ":")
......
......@@ -72,6 +72,7 @@ type FieldSimple struct {
LoopStart int
LoopEnd int
LoopIndex int
IsLoop bool
}
type FieldWithValues struct {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册