From 52a434f88be51c3f970bc28d5ed615a51cc9de82 Mon Sep 17 00:00:00 2001 From: aaron <462826@qq.com> Date: Mon, 27 Jul 2020 08:59:26 +0800 Subject: [PATCH] close task #7492 --- demo/smoke.yaml | 4 +++- src/gen/definition.go | 2 +- src/gen/generator.go | 43 +++++++++++++++++++++++++++++++++-------- src/model/definition.go | 6 +++++- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/demo/smoke.yaml b/demo/smoke.yaml index f460af06..feb1b848 100644 --- a/demo/smoke.yaml +++ b/demo/smoke.yaml @@ -21,5 +21,7 @@ fields: postfix: "\t" - field: test5 - range: [user-2,user3,user4,user6]:R{2} + range: [user-2,user3,user4,user6]{2} + loop: 2-9 + loopfix: "|" postfix: "\t" diff --git a/src/gen/definition.go b/src/gen/definition.go index 907cf763..bf2aa4d6 100644 --- a/src/gen/definition.go +++ b/src/gen/definition.go @@ -146,7 +146,7 @@ func CopyField(child model.DefField, parent *model.DefField) { if child.Postfix != "" { (*parent).Postfix = child.Postfix } - if child.Loop != 0 { + if child.Loop != "" { (*parent).Loop = child.Loop } if child.Loopfix != "" { diff --git a/src/gen/generator.go b/src/gen/generator.go index cc8b50b2..d3875505 100644 --- a/src/gen/generator.go +++ b/src/gen/generator.go @@ -113,7 +113,7 @@ func GenerateForField(field *model.DefField, total int, withFix bool) []string { } func GenerateFieldItemsFromDefinition(field *model.DefField) []string { - if field.Loop == 0 {field.Loop = 1} + //if field.Loop == "" {field.Loop = "1"} values := make([]string, 0) @@ -123,7 +123,7 @@ func GenerateFieldItemsFromDefinition(field *model.DefField) []string { count := 0 for { // 处理格式、前后缀、loop等 - str := GenerateFieldValWithFix(*field, fieldValue, &index, true) + str := GenerateFieldValWithFix(field, fieldValue, &index, true) values = append(values, str) count++ @@ -187,7 +187,7 @@ func GetFieldValStr(field model.DefField, val interface{}) string { } func LoopSubFields(field *model.DefField, oldValues []string, total int, withFix bool) []string { - if field.Loop == 0 {field.Loop = 1} + //if field.Loop == "" {field.Loop = "1"} values := make([]string, 0) fieldValue := model.FieldValue{} @@ -200,7 +200,7 @@ func LoopSubFields(field *model.DefField, oldValues []string, total int, withFix count := 0 for { // 处理格式、前后缀、loop等 - str := GenerateFieldValWithFix(*field, fieldValue, &index, withFix) + str := GenerateFieldValWithFix(field, fieldValue, &index, withFix) values = append(values, str) count++ @@ -212,17 +212,18 @@ func LoopSubFields(field *model.DefField, oldValues []string, total int, withFix return values } -func GenerateFieldValWithFix(field model.DefField, fieldValue model.FieldValue, indexOfRow *int, withFix bool) string { +func GenerateFieldValWithFix(field *model.DefField, fieldValue model.FieldValue, indexOfRow *int, withFix bool) string { prefix := field.Prefix postfix := field.Postfix + computerLoop(field) loopStr := "" - for j := 0; j < field.Loop; j++ { + for j := 0; j < (*field).LoopIndex; j++ { if loopStr != "" { loopStr = loopStr + field.Loopfix } - str := GenerateFieldVal(field, fieldValue, indexOfRow) + str := GenerateFieldVal(*field, fieldValue, indexOfRow) loopStr = loopStr + str *indexOfRow++ @@ -233,8 +234,34 @@ func GenerateFieldValWithFix(field model.DefField, fieldValue model.FieldValue, } if field.Width > runewidth.StringWidth(loopStr) { - loopStr = stringUtils.AddPad(loopStr, field) + loopStr = stringUtils.AddPad(loopStr, *field) + } + + (*field).LoopIndex = (*field).LoopIndex + 1 + if (*field).LoopIndex > (*field).LoopEnd { + (*field).LoopIndex = (*field).LoopStart } return loopStr +} + +func computerLoop(field *model.DefField) { + if (*field).LoopIndex != 0 { + return + } + + arr := strings.Split(field.Loop, "-") + (*field).LoopStart, _ = strconv.Atoi(arr[0]) + if len(arr) > 1 { + field.LoopEnd, _ = strconv.Atoi(arr[1]) + } + + if (*field).LoopStart == 0 { + (*field).LoopStart = 1 + } + if (*field).LoopEnd == 0 { + (*field).LoopEnd = 1 + } + + (*field).LoopIndex = (*field).LoopStart } \ No newline at end of file diff --git a/src/model/definition.go b/src/model/definition.go index 948e2fd2..2580b4cd 100644 --- a/src/model/definition.go +++ b/src/model/definition.go @@ -65,9 +65,13 @@ type FieldSimple struct { Range string `yaml:"range"` Prefix string `yaml:"prefix"` Postfix string `yaml:"postfix"` - Loop int `yaml:"loop"` + Loop string `yaml:"loop"` Loopfix string `yaml:"loopfix"` Format string `yaml:"format"` + + LoopStart int + LoopEnd int + LoopIndex int } type FieldValue struct { -- GitLab