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

cloes task#7499

上级 10bf813c
......@@ -34,8 +34,6 @@ fields:
postfix: "\t"
- field: child2
prefix: "["
postfix: "]"
fields:
- field: child2.1
range: a-b
......@@ -44,6 +42,7 @@ fields:
- field: child2.2
range: 1-9:R
prefix:
loop: 3
loopfix: ","
prefix: "["
postfix: "]"
......@@ -13,6 +13,7 @@ import (
"github.com/easysoft/zendata/src/utils/vari"
"net/http"
"os"
"regexp"
"strings"
"time"
)
......@@ -64,6 +65,8 @@ func Print(rows [][]string, format string, table string, colIsNumArr []bool, fie
lineForText := ""
for j, col := range cols {
col = replacePlaceholder(col)
lineForText = lineForText + col
row = append(row, col)
......@@ -167,6 +170,63 @@ func getXmlLine(i int, rowXml model.XmlRow, length int) string {
return text
}
func replacePlaceholder(col string) string {
ret := col
re := regexp.MustCompile("(?siU)\\${(.*)}")
arr := re.FindAllStringSubmatch(col, -1)
strForReplaceMap := map[string][]string{}
for _, childArr := range arr {
placeholderStr := childArr[1]
//if strForReplaceMap[placeholderStr] == nil {
strForReplaceMap[placeholderStr] = getValForPlaceholder(placeholderStr, len(childArr))
//}
for _, str := range strForReplaceMap[placeholderStr] {
ret = strings.Replace(ret, gen.Placeholder(placeholderStr), str, 1)
}
}
return ret
}
func getValForPlaceholder(placeholderStr string, count int) []string {
mp := vari.RandFieldNameToValuesMap[placeholderStr]
tp := mp["type"].(string)
repeatObj := mp["repeat"]
repeat := "1"
if repeatObj != nil {
repeat = repeatObj.(string)
}
strs := make([]string, 0)
if tp == "int" {
start := mp["start"].(string)
end := mp["end"].(string)
precision := mp["precision"].(string)
strs = gen.GetRandFromRange("int", start, end, "1", repeat, precision, count)
} else if tp == "float" {
start := mp["start"].(string)
end := mp["end"].(string)
precision := mp["precision"].(string)
strs = gen.GetRandFromRange("float", start, end, "1", repeat, precision, count)
} else if tp == "char" {
start := mp["start"].(string)
end := mp["end"].(string)
precision := mp["precision"].(string)
strs = gen.GetRandFromRange("char", start, end, "1", repeat, precision, count)
} else if tp == "list" {
list := mp["list"].([]interface{})
strs = gen.GetRandFromList(list, repeat, count)
}
return strs
}
func printLine(line string) {
if FileWriter != nil {
PrintToFile(line)
......
package gen
import (
commonUtils "github.com/easysoft/zendata/src/utils/common"
constant "github.com/easysoft/zendata/src/utils/const"
)
func GenerateByteItems(start byte, end byte, step interface{}, rand bool, repeat int) []interface{} {
if !rand {
return GenerateByteItemsByStep(start, end, step.(int), repeat)
} else {
return GenerateByteItemsRand(start, end, step.(int), repeat)
}
}
//func GenerateByteItems(start byte, end byte, step interface{}, rand bool, repeat int) []interface{} {
// if !rand {
// return GenerateByteItemsByStep(start, end, step.(int), repeat)
// } else {
// return GenerateByteItemsRand(start, end, step.(int), repeat)
// }
//}
func GenerateByteItemsByStep(start byte, end byte, step int, repeat int) []interface{} {
arr := make([]interface{}, 0)
......@@ -40,33 +39,33 @@ func GenerateByteItemsByStep(start byte, end byte, step int, repeat int) []inter
return arr
}
func GenerateByteItemsRand(start byte, end byte, step int, repeat int) []interface{} {
arr := make([]interface{}, 0)
countInRound := int(int(end) - int(start)) / step
total := 0
for i := 0; i < countInRound; {
rand := commonUtils.RandNum(countInRound)
if step < 0 {
rand = rand * -1
}
val := start + byte(rand)
for round := 0; round < repeat; round++ {
arr = append(arr, val)
total++
if total > constant.MaxNumb {
break
}
}
if total > constant.MaxNumb {
break
}
i++
}
return arr
}
\ No newline at end of file
//func GenerateByteItemsRand(start byte, end byte, step int, repeat int) []interface{} {
// arr := make([]interface{}, 0)
//
// countInRound := int(int(end) - int(start)) / step
// total := 0
// for i := 0; i < countInRound; {
// rand := commonUtils.RandNum(countInRound)
// if step < 0 {
// rand = rand * -1
// }
//
// val := start + byte(rand)
//
// for round := 0; round < repeat; round++ {
// arr = append(arr, val)
//
// total++
// if total > constant.MaxNumb {
// break
// }
// }
//
// if total > constant.MaxNumb {
// break
// }
// i++
// }
//
// return arr
//}
\ No newline at end of file
package gen
import (
commonUtils "github.com/easysoft/zendata/src/utils/common"
constant "github.com/easysoft/zendata/src/utils/const"
"strconv"
"strings"
)
func GenerateFloatItems(start float64, end float64, step interface{}, rand bool, repeat int) []interface{} {
if !rand{
return GenerateFloatItemsByStep(start, end, step.(float64), repeat)
} else {
return GenerateFloatItemsRand(start, end, step.(float64), repeat)
}
}
//func GenerateFloatItems(start float64, end float64, step interface{}, rand bool, repeat int) []interface{} {
// if !rand{
// return GenerateFloatItemsByStep(start, end, step.(float64), repeat)
// } else {
// return GenerateFloatItemsRand(start, end, step.(float64), repeat)
// }
//}
func GenerateFloatItemsByStep(start float64, end float64, step interface{}, repeat int) []interface{} {
arr := make([]interface{}, 0)
......@@ -43,36 +42,36 @@ func GenerateFloatItemsByStep(start float64, end float64, step interface{}, repe
return arr
}
func GenerateFloatItemsRand(start float64, end float64, step float64, repeat int) []interface{} {
arr := make([]interface{}, 0)
countInRound := (end - start) / step
total := 0
for i := float64(0); i < countInRound; {
rand := commonUtils.RandNum64(int64(countInRound))
if step < 0 {
rand = rand * -1
}
val := start + float64(rand) * step
for round := 0; round < repeat; round++ {
arr = append(arr, val)
total++
if total > constant.MaxNumb {
break
}
}
if total > constant.MaxNumb {
break
}
i++
}
return arr
}
//func GenerateFloatItemsRand(start float64, end float64, step float64, repeat int) []interface{} {
// arr := make([]interface{}, 0)
//
// countInRound := (end - start) / step
// total := 0
// for i := float64(0); i < countInRound; {
// rand := commonUtils.RandNum64(int64(countInRound))
// if step < 0 {
// rand = rand * -1
// }
//
// val := start + float64(rand) * step
//
// for round := 0; round < repeat; round++ {
// arr = append(arr, val)
//
// total++
// if total > constant.MaxNumb {
// break
// }
// }
//
// if total > constant.MaxNumb {
// break
// }
// i++
// }
//
// return arr
//}
func getPrecision(base float64, step interface{}) int {
val := base + step.(float64)
......
......@@ -73,10 +73,6 @@ func GenerateForField(field *model.DefField, total int, withFix bool) (values []
} else if field.From != "" { // refer to res
if field.Use != "" { // refer to instance
if field.Use == "privateIP" {
fmt.Println("")
}
groupValues := vari.Res[field.From]
groups := strings.Split(field.Use, ",")
for _, group := range groups {
......@@ -109,8 +105,6 @@ func GenerateForField(field *model.DefField, total int, withFix bool) (values []
func GenerateFieldItemsFromDefinition(field *model.DefField) []string {
values := make([]string, 0)
//valuesWithPlaceholder := make([]string, 0)
//vari.RandFieldNameToValuesMap[field.Path] = valuesWithPlaceholder
fieldWithValues := GenerateList(field)
......
package gen
import (
commonUtils "github.com/easysoft/zendata/src/utils/common"
constant "github.com/easysoft/zendata/src/utils/const"
)
//func GenerateIntItems(start int64, end int64, step interface{}, rand bool, limit int) []interface{} {
// if !rand {
// return GenerateIntItemsByStep(start, end, step.(int), limit)
// } else{
// return GenerateIntItemsRand(start, end, step.(int), limit)
// }
//}
func GenerateIntItems(start int64, end int64, step interface{}, rand bool, limit int) []interface{} {
if !rand {
return GenerateIntItemsByStep(start, end, step.(int), limit)
} else{
return GenerateIntItemsRand(start, end, step.(int), limit)
}
}
func GenerateIntItemsByStep(start int64, end int64, step int, repeat int) []interface{} {
arr := make([]interface{}, 0)
......@@ -40,33 +41,33 @@ func GenerateIntItemsByStep(start int64, end int64, step int, repeat int) []inte
return arr
}
//func GenerateIntItemsRand(start int64, end int64, step int, repeat int) []interface{} {
// arr := make([]interface{}, 0)
//
// countInRound := (end - start) / int64(step)
// total := 0
// for i := int64(0); i < countInRound; {
// rand := commonUtils.RandNum64(countInRound)
// if step < 0 {
// rand = rand * -1
// }
//
// val := start + rand
// for round := 0; round < repeat; round++ {
// arr = append(arr, val)
//
// total++
//
// if total > constant.MaxNumb {
// break
// }
// }
//
// if total > constant.MaxNumb {
// break
// }
// i++
// }
//
// return arr
//}
\ No newline at end of file
func GenerateIntItemsRand(start int64, end int64, step int, repeat int) []interface{} {
arr := make([]interface{}, 0)
countInRound := (end - start) / int64(step)
total := 0
for i := int64(0); i < countInRound; {
rand := commonUtils.RandNum64(countInRound)
if step < 0 {
rand = rand * -1
}
val := start + rand
for round := 0; round < repeat; round++ {
arr = append(arr, val)
total++
if total > constant.MaxNumb {
break
}
}
if total > constant.MaxNumb {
break
}
i++
}
return arr
}
\ No newline at end of file
......@@ -2,8 +2,8 @@ package gen
import (
"github.com/easysoft/zendata/src/model"
commonUtils "github.com/easysoft/zendata/src/utils/common"
constant "github.com/easysoft/zendata/src/utils/const"
"github.com/easysoft/zendata/src/utils/vari"
"strconv"
"strings"
)
......@@ -140,19 +140,20 @@ func CheckRangeType(startStr string, endStr string, stepStr string) (string, int
}
func GenerateValuesFromLiteral(field *model.DefField, desc string, stepStr string, repeat int) (items []interface{}) {
elemArr := strings.Split(desc, ",")
step, _ := strconv.Atoi(stepStr)
total := 0
for i := 0; i < len(elemArr); {
val := ""
if stepStr == "r" {
items = append(items, Placeholder(field.Path))
mp := placeholderMapForRandValues("list", elemArr, "", "", "", "")
if stepStr == "r" {
val = elemArr[commonUtils.RandNum(len(elemArr))]
} else {
val = elemArr[i]
}
vari.RandFieldNameToValuesMap[field.Path] = mp
return
}
for i := 0; i < len(elemArr); {
val := elemArr[i]
for round := 0; round < repeat; round++ {
items = append(items, val)
......@@ -180,32 +181,31 @@ func GenerateValuesFromInterval(field *model.DefField, desc string, stepStr stri
dataType, step, precision, rand := CheckRangeType(startStr, endStr, stepStr)
if dataType != "string" && rand {
items = append(items, Placeholder(field.Path))
mp := placeholderMapForRandValues(dataType, []string{}, startStr, endStr, stepStr, "")
vari.RandFieldNameToValuesMap[field.Path] = mp
return
}
if dataType == "int" {
startInt, _ := strconv.ParseInt(startStr, 0, 64)
endInt, _ := strconv.ParseInt(endStr, 0, 64)
if !rand {
items = GenerateIntItemsByStep(startInt, endInt, step.(int), repeat)
} else{
items = append(items, field.Path)
}
items = GenerateIntItemsByStep(startInt, endInt, step.(int), repeat)
} else if dataType == "float" {
startFloat, _ := strconv.ParseFloat(startStr, 64)
endFloat, _ := strconv.ParseFloat(endStr, 64)
field.Precision = precision
if !rand{
items = GenerateFloatItemsByStep(startFloat, endFloat, step.(int), repeat)
} else {
items = append(items, field.Path)
}
items = GenerateFloatItemsByStep(startFloat, endFloat, step.(int), repeat)
} else if dataType == "char" {
if !rand {
items = GenerateByteItemsByStep(startStr[0], endStr[0], step.(int), repeat)
} else {
items = append(items, field.Path)
}
items = GenerateByteItemsByStep(startStr[0], endStr[0], step.(int), repeat)
} else if dataType == "string" {
if repeat == 0 { repeat = 1 }
for i := 0; i < repeat; i++ {
......@@ -215,3 +215,22 @@ func GenerateValuesFromInterval(field *model.DefField, desc string, stepStr stri
return
}
func Placeholder(str string) string {
return "${" + str + "}"
}
func placeholderMapForRandValues(tp string, list []string, start, end, step, precision string) map[string]interface{} {
ret := map[string]interface{}{}
ret["type"] = tp
ret["list"] = list // for literal values
ret["start"] = start // for interval values
ret["end"] = end
ret["step"] = step
ret["precision"] = precision
return ret
}
......@@ -6,54 +6,73 @@ import (
"strings"
)
func GetRandFromList(list []string, repeat int) string {
rand := commonUtils.RandNum(len(list))
val := list[rand]
func GetRandFromList(list []interface{}, repeatStr string, count int) []string {
ret := make([]string, 0)
items := make([]string, 0)
for round := 0; round < repeat; round++ {
items = append(items, val)
for i := 0; i < count; i++ {
rand := commonUtils.RandNum(len(list))
val := list[rand]
repeat, _ := strconv.Atoi(repeatStr)
items := make([]string, 0)
for round := 0; round < repeat; round++ {
items = append(items, val.(string))
}
ret = append(ret, strings.Join(items, ""))
}
return strings.Join(items, "")
return ret
}
func GetRandFromRange(dataType, start, end, step string, repeat, precision int) string {
func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string, count int) []string {
repeat, _ := strconv.Atoi(repeatStr)
precision, _ := strconv.Atoi(precisionStr)
ret := make([]string, 0)
if dataType == "int" {
startInt, _ := strconv.ParseInt(start, 0, 64)
endInt, _ := strconv.ParseInt(end, 0, 64)
stepInt, _ := strconv.ParseInt(step, 0, 64)
countInRound := (startInt - endInt) / stepInt
rand := commonUtils.RandNum64(countInRound)
if stepInt < 0 {
rand = rand * -1
}
val := startInt + rand
countInRound := (endInt - startInt) / stepInt
items := make([]string, 0)
for round := 0; round < repeat; round++ {
items = append(items, string(val))
}
for i := 0; i < count; i++ {
rand := commonUtils.RandNum64(countInRound)
if stepInt < 0 {
rand = rand * -1
}
val := startInt + rand
return strings.Join(items, "")
items := make([]string, 0)
for round := 0; round < repeat; round++ {
items = append(items, strconv.FormatInt(val, 10))
}
temp := strings.Join(items, "")
ret = append(ret, temp)
}
} else if dataType == "char" {
startChar := start[0]
endChar := end[0]
stepInt, _ := strconv.ParseInt(step, 0, 64)
stepInt, _ := strconv.ParseInt(step, 10, 64)
countInRound := int64(int64(endChar) - int64(startChar)) / stepInt
rand := commonUtils.RandNum64(countInRound)
if stepInt < 0 {
rand = rand * -1
}
val := startChar + byte(rand)
items := make([]string, 0)
for round := 0; round < repeat; round++ {
items = append(items, string(val))
for i := 0; i < count; i++ {
rand := commonUtils.RandNum64(countInRound)
if stepInt < 0 {
rand = rand * -1
}
val := startChar + byte(rand)
items := make([]string, 0)
for round := 0; round < repeat; round++ {
items = append(items, string(val))
}
temp := strings.Join(items, "")
ret = append(ret, temp)
}
return strings.Join(items, "")
} else if dataType == "float" {
startFloat, _ := strconv.ParseFloat(start, 64)
......@@ -62,22 +81,24 @@ func GetRandFromRange(dataType, start, end, step string, repeat, precision int)
countInRound := (startFloat - endFloat) / stepFloat
rand := commonUtils.RandNum64(int64(countInRound))
if stepFloat < 0 {
rand = rand * -1
}
val := startFloat + float64(rand) * stepFloat
for i := 0; i < count; i++ {
rand := commonUtils.RandNum64(int64(countInRound))
if stepFloat < 0 {
rand = rand * -1
}
items := make([]string, 0)
for round := 0; round < repeat; round++ {
str := strconv.FormatFloat(val, 'f', precision, 64)
items = append(items, str)
val := startFloat + float64(rand)*stepFloat
return strings.Join(items, "")
items := make([]string, 0)
for round := 0; round < repeat; round++ {
str := strconv.FormatFloat(val, 'f', precision, 64)
items = append(items, str)
}
temp := strings.Join(items, "")
ret = append(ret, temp)
}
}
return "N/A"
return ret
}
\ No newline at end of file
......@@ -32,7 +32,7 @@ var (
Def = model.DefData{}
Res = map[string]map[string][]string{}
RandFieldNameToValuesMap = map[string][]string{}
RandFieldNameToValuesMap = map[string]map[string]interface{}{}
DefaultDir string
ConfigDir string
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册